是否有一个最大的位图大小使用getDrawingCache什么时候? [英] Is there a maximum bitmap size when using getDrawingCache?
问题描述
我想创建一个的TextView
文本的位图。在过去,我已经做到了这一点使用 getDrawingCache
。不过,现在我有一个需要创建一个的TextView
的位图与更长的文本比以前。这是造成 getDrawingCache
来抛出NullPointerException。
I'm trying to create a bitmap of the text in a TextView
. In the past I have done this using getDrawingCache
. However, now I have a need to create a bitmap of a TextView
with much longer text than before. This is causing getDrawingCache
to throw a NullPointerException.
虽然我说:要长得多的文字,我不是在谈论过长。如果我创建一个TextView是600像素宽的大小24的字体,我在53行文本(但不是在52线)得到的异常。是否有解决方法吗?
Although I say "much longer text," I am not talking about unreasonably long. If I create a TextView that is 600 pixels wide at size 24 font, I get the exception at 53 lines of text (but not at 52 lines). Is there a workaround for this?
起初我还以为这个答案,在这种布局绘制本身画布上
,是解决办法。但是,这并没有为我工作,因为我编程方式创建的TextView和的宽度和高度都为0 他们得到奠定之前。我从来没有真正的布局我的的TextView
在屏幕上。
At first I thought this answer, in which the layout draws itself on a canvas
, was the solution. However, that didn't work for me because I am creating the TextView programmatically and the width and height are 0 before they get laid out. I never actually layout my TextView
on screen.
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;
}
NullPointerException异常
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异常(至少不是外部,但也许这是内部的原因。)
Note
This is not an IllegalArgumentException or OutOfMemoryError (At least not externally, though maybe this is the reason internally.)
- <一个href="http://stackoverflow.com/questions/9370145/mysterious-stacktrace-in-android-developer-console-bitmap-size-exceeds-32bits">Mysterious堆栈跟踪在Android开发者控制台(位图的大小超过32位)
- <一个href="http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object">Strange内存不足的问题,同时加载图像为位图对象
- Mysterious stacktrace in Android developer console (bitmap size exceeds 32bits)
- Strange out of memory issue while loading an image to a Bitmap object
推荐答案
您可以删除 buildDrawingCache
的方法,只是使用画布
。既然你正在构建的视图编程,你还需要调用措施()
和布局()
之前,你可以得到一个位图。
You can drop the buildDrawingCache
method and just use canvas
. Since you are building the view programmatically you also need to call measure()
and layout()
before you can get a bitmap.
关键线路有:
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;
}
这篇关于是否有一个最大的位图大小使用getDrawingCache什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!