可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I'm working with Android 2.1 and have the following problem:
Using the method View.getDrawingCache() always returns null. getDrawingCache() should return a Bitmap, which is the presentation of View's content.
Example code:
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final View view = findViewById(R.id.ImageView01);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
final Bitmap bmp = view.getDrawingCache();
System.out.println(bmp);
}
I've already tried different ways to configure the View object for generating the drawing cache (e.g. View.setWillNotDraw(boolean)
and View.setWillNotCacheDrawing(boolean)
), but nothing works.
What is the right way, or what I'm doing wrong?
PS: In real code I want to apply getDrawingCache() on a ViewGroup like RelativeLayout. Is the behaviour the same when using a ViewGroup?
回答1:
Bitmap screenshot;
view.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
You need Bitmap.createBitmap(view.getDrawingCache());
as the Bitmap from which the reference is received by getDrawingCache()
gets recycled.
回答2:
use
onLayout(x, y, width, height);
for ex:
onLayout(0, 0, 100, 100);
before calling getDrawingCache
回答3:
Never do these things in onCreate!
You can do it in a View.onClickListener or other place.
回答4:
I know this doesn't answer your question, but....
Just as an aside for other developers reading this, if what you are really trying to do is have access to a custom view's contents, derive your custom view from an ImageView. And then create a canvas and bitmap that you are going to control/draw into/read and use setImageBitmap() to set your bitmap into the view object. From there on in you can ignore the canvas passed to you in the onDraw(canvas) method and just draw into your local canvas/bitmap instead.
Why would you need this? There is an oversight in the Canvas class in my opinion. The Canvas class gives you no way to read the canvas contents nor to get at the bitmap behind it. So if you ever need to read from a bitmap, for example to acquire an image dump of the view contents, there's no way I know of to do it, other than the method described above.
This won't work for the RelativeView problem originally described here in this thread but this might be of interest to new Android developers.
回答5:
As far as I read the last days in the documentation you should onlycall setDrawingCacheEnabled or buildDrawingChache() but not both.
回答6:
had the same problem and finally this partly works for me:
view.layout(0, 0, 1000, 100);
Bitmap b = view.getDrawingCache();
try {
b.compress(CompressFormat.JPEG, 95,
new FileOutputStream( Environment.getExternalStorageDirectory() + "/folder/name.jpg"));
catch (FileNotFoundException e) {
Log.e(LOGTAG, "error:" + e.getMessage());
}
the only problem is, that I can't get the full dimensions of my view.
view.getwidth returns null ...
回答7:
Check out if your view is not bigger than the screen size. I found this and switched the dimensions of my view so, now it works =)
Android get full View as Bitmap