有没有人能给我解释一下为什么
public void addView(View child) {
child.setDrawingCacheEnabled(true);
child.setWillNotCacheDrawing(false);
child.setWillNotDraw(false);
child.buildDrawingCache();
if(child.getDrawingCache() == null) { //TODO Make this work!
Log.w("View", "View child's drawing cache is null");
}
setImageBitmap(child.getDrawingCache()); //TODO MAKE THIS WORK!!!
}
是否始终记录图形缓存为空,并将位图设置为空?
在设置缓存之前,我真的需要绘制视图吗?
谢谢!
发布于 2011-01-07 01:58:13
我也遇到了这个问题,我找到了这个答案:
v.setDrawingCacheEnabled(true);
// this is the important code :)
// Without it the view will have a dimension of 0,0 and the bitmap will be null
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false); // clear drawing cache
发布于 2012-08-13 23:25:15
如果getDrawingCache
总是returning null
guys:使用这个:
public static Bitmap loadBitmapFromView(View v) {
Bitmap b = Bitmap.createBitmap( v.getLayoutParams().width, v.getLayoutParams().height, Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.layout(0, 0, v.getLayoutParams().width, v.getLayoutParams().height);
v.draw(c);
return b;
}
发布于 2016-03-13 18:21:40
获取空值的基本原因是视图没有被忽略。那么,所有使用view.getWidth()、view.getLayoutParams()、.width等的尝试,包括view.getDrawingCache()和view.buildDrawingCache(),都是无用的。因此,首先需要设置视图的尺寸,例如:
view.layout(0, 0, width, height);
(您已经根据自己的喜好设置了“width”和“height”,或者使用WindowManager等工具获取了它们。)
https://stackoverflow.com/questions/2339429
复制相似问题