似乎无法从视图中的机器人获得的位图 [英] cannot seem to get bitmap from view in android
问题描述
我的问题是一个类似于在<一href="http://stackoverflow.com/questions/2339429/android-view-getdrawingcache-returns-null-only-null">Android View.getDrawingCache返回null,只有空。
使用的方法来工作,我的code
私人位图getBitmapFromView(视图v){
v.setDrawingCacheEnabled(真正的);
v.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
v.layout(0,0,v.getMeasuredWidth(),v.getMeasuredHeight());
v.buildDrawingCache(真正的);
位图B = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(假); //清晰的绘图缓存
返回b;
}
但今天我改变了XML布局文件。由于改变我已经越来越NullPointerException异常的行位图B = Bitmap.createBitmap(v.getDrawingCache())
。所以,我试图改变方法 getBitmapFromView
到
公共静态位图loadBitmapFromView(视图v){
位图B = Bitmap.createBitmap(v.getLayoutParams()宽度,v.getLayoutParams()高度,Bitmap.Config.ARGB_8888);
帆布C =新的Canvas(B);
v.layout(0,0,v.getLayoutParams()宽度,v.getLayoutParams()高度。);
v.draw(C);
返回b;
}
但是,这并没有帮助。我得到的第一行空。
的情况是这样的:我想视图转换成位图。但我这样做并没有先在屏幕上显示的视图。同样,这一切都用来工作,直到我换了XML布局文件的内容。这种变化甚至没有那么大。我刚搬的东西四周,删除 - 是去掉 - 一些子视图
在任何情况下,我知道肯定是没有问题,我的布局文件,因为我可以在屏幕上显示出来,如果我想要的;我已经做故障排除的一部分。
使用一个处理程序来调用有人建议 getDrawingCache()
。如果是这样的回答,我怎么会写code?
更新
查看视图= activity.getLayoutInflater()膨胀(R.layout.my_view,空,假的)。
findViewsByIds(视图); //我的充气子视图,并添加内容到他们。
既然你不显示视图它更容易只是画在画布上的看法。 你已经尝试这样做,但我会合并这两种方法为一个没有使用绘图缓存。
私人位图getBitmapFromView(视图v){
v.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
v.layout(0,0,v.getMeasuredWidth(),v.getMeasuredWidth());
位图B = Bitmap.createBitmap(v.getMeasuredWidth(),v.getMeasuredWidth(),Bitmap.Config.ARGB_8888);
帆布C =新的Canvas(B);
v.draw(C);
返回b;
}
此外,检查 MeasureSpec.UNSPECIFIED
,未经布局视图的工作,所以你可能需要指定一个宽度和高度。
My problem is similar to the one in Android View.getDrawingCache returns null, only null.
My code used to work with the method
private Bitmap getBitmapFromView(View v) {
v.setDrawingCacheEnabled(true);
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
return b;
}
But today I changed the xml layout file. Since the change I have been getting NullPointerException at the line Bitmap b = Bitmap.createBitmap(v.getDrawingCache())
. So I tried changing the method getBitmapFromView
to
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;
}
But that didn't help. I get the null in the very first line.
The situation is this: I am trying to convert the view into a bitmap. But I am doing this without first displaying the view on screen. Again, this all used to work until I changed the content of the xml layout file. The change was not even that big. I just moved stuff around and removed -- yes removed -- some of the subviews.
In any case, I know for certain there is no problem with my layout file because I can display it on screen if I want; which I have done as part of troubleshooting.
Someone suggested using a handler to call getDrawingCache()
. If that's the answer, how would I write that code?
UPDATE
View view = activity.getLayoutInflater().inflate(R.layout.my_view, null, false);
findViewsByIds(view);//where I inflate subviews and add content to them.
Since you are not showing the view it's easier to just draw the view on a canvas. You are already trying to do that but I would merge both methods into one without using drawing cache.
private Bitmap getBitmapFromView(View v) {
v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredWidth());
Bitmap b = Bitmap.createBitmap(v.getMeasuredWidth(), v.getMeasuredWidth(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
v.draw(c);
return b;
}
Also check that MeasureSpec.UNSPECIFIED
may not work well without the view in layout so you may need to specify a width and height.
这篇关于似乎无法从视图中的机器人获得的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!