gpt4 book ai didi

android - 使用 getDrawingCache 时是否有最大位图大小?

转载 作者:搜寻专家 更新时间:2023-11-01 08:43:05 25 4
gpt4 key购买 nike

我正在尝试在 TextView 中创建文本的位图。过去,我使用 getDrawingCache 完成此操作。但是,现在我需要创建一个 TextView 的位图,其中的文本比以前长得多。这导致 getDrawingCache 抛出 NullPointerException。

虽然我说的是“更长的文本”,但我并不是在谈论不合理的长。如果我创建一个宽度为 600 像素、字体大小为 24 的 TextView,我会在 53 行文本(但不是 52 行)处得到异常。有解决办法吗?

起初我以为this answer ,其中布局将自身绘制在 canvas 上,这就是解决方案。但是,这对我不起作用,因为我正在以编程方式创建 TextView 并且 the width and height are 0在他们被布置之前。我从未真正在屏幕上布局我的 TextView

引用代码

private Bitmap getBitmap(Context context){

final int NUMBER_OF_LINES = 53; // 53 crashes, 52 doesn't
final int width = 600; // width of TextView in pixels
final int fontSize = 24;

// create string with NUMBER_OF_LINES
StringBuilder testString = new StringBuilder();
for (int i = 0; i < NUMBER_OF_LINES; i++) {
testString.append("\n");
}

// Create TextView
TextView tvText = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tvText.setTextSize(fontSize);
tvText.setWidth(width);
tvText.setLayoutParams(params);
tvText.setBackgroundColor(Color.WHITE); // even setting the background color affects crashing or not
tvText.setText(testString);

// Create bitmap
tvText.setDrawingCacheEnabled(true);
tvText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tvText.layout(0, 0, tvText.getMeasuredWidth(), tvText.getMeasuredHeight());
tvText.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(tvText.getDrawingCache()); // crashes here
tvText.setDrawingCacheEnabled(false);

// This also didn't work because width and height are 0
/*Bitmap bitmap = Bitmap.createBitmap(tvText.getWidth(), tvText.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tvText.draw(canvas);*/

return bitmap;
}

空指针异常

06-21 14:20:24.628    8036-8036/com.example.testsomecode E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testsomecode/com.example.testsomecode.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2092)
...
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:494)
at com.example.testsomecode.MainActivity.getBitmap(MainActivity.java:57) // -> Bitmap bitmap = Bitmap.createBitmap(tvText.getDrawingCache());
at com.example.testsomecode.MainActivity.onCreate(MainActivity.java:25)
...

注意事项

这不是 IllegalArgumentException 或 OutOfMemoryError(至少在外部不是,尽管这可能是内部原因。)

最佳答案

您可以删除 buildDrawingCache 方法,只使用 canvas。由于您是以编程方式构建 View ,因此在获取位图之前还需要调用 measure()layout()

关键行是:

    tvText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tvText.layout(0, 0, tvText.getMeasuredWidth(), tvText.getMeasuredHeight());
Bitmap bitmap = Bitmap.createBitmap(tvText.getWidth(), tvText.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tvText.draw(canvas);

这里是完整的例子:

private Bitmap getBitmap(Context context){

final int NUMBER_OF_LINES = 153;
final int width = 600;
final int fontSize = 24;

// create string with NUMBER_OF_LINES
StringBuilder testString = new StringBuilder();
for (int i = 0; i < NUMBER_OF_LINES; i++) {
testString.append("\n");
}

// Create TextView
TextView tvText = new TextView(context);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
tvText.setTextSize(fontSize);
tvText.setWidth(width);
tvText.setLayoutParams(params);
tvText.setBackgroundColor(Color.WHITE); // even setting the background color affects crashing or not
tvText.setText(testString);
tvText.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
tvText.layout(0, 0, tvText.getMeasuredWidth(), tvText.getMeasuredHeight());


// Create the bitmap from the view
Bitmap bitmap = Bitmap.createBitmap(tvText.getWidth(), tvText.getHeight(),
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
tvText.draw(canvas);

return bitmap;
}

关于android - 使用 getDrawingCache 时是否有最大位图大小?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30961921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com