Android 2.1 View 的 getDrawingCache() 方法总是返回 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2817166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:30:40  来源:igfitidea点击:

Android 2.1 View's getDrawingCache() method always returns null

androidcachingnullviewdrawing

提问by Impression

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.

我正在使用 Android 2.1 并遇到以下问题:使用方法 View.getDrawingCache() 总是返回 null。getDrawingCache() 应该返回一个 Bitmap,它是 View 内容的表示。

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.

我已经尝试了不同的方法来配置 View 对象以生成绘图缓存(例如View.setWillNotDraw(boolean)View.setWillNotCacheDrawing(boolean)),但没有任何效果。

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?

PS:在实际代码中,我想在像 RelativeLayout 这样的 ViewGroup 上应用 getDrawingCache()。使用 ViewGroup 时的行为是否相同?

回答by Youyougoslave

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.

您需要Bitmap.createBitmap(view.getDrawingCache());作为从中接收引用的位图getDrawingCache()被回收。

回答by Vinay

use

onLayout(x, y, width, height);

for ex:

例如:

onLayout(0, 0, 100, 100);

before calling getDrawingCache

在调用 getDrawingCache 之前

回答by Batdude

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.

正如其他开发人员阅读本文一样,如果您真正想做的是访问自定义视图的内容,请从 ImageView 派生您的自定义视图。然后创建一个您要控制/绘制/读取的画布和位图,并使用 setImageBitmap() 将您的位图设置到视图对象中。从那时起,您可以忽略在 onDraw(canvas) 方法中传递给您的画布,而只需将其绘制到您的本地画布/位图中即可。

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.

你为什么需要这个?我认为 Canvas 类中有一个疏忽。Canvas 类使您无法读取画布内容,也无法获取其背后的位图。因此,如果您需要从位图读取,例如获取视图内容的图像转储,除了上述方法之外,我没有办法做到这一点。

This won't work for the RelativeView problem originally described here in this thread but this might be of interest to new Android developers.

这不适用于本线程中最初在此处描述的 RelativeView 问题,但这可能对新的 Android 开发人员感兴趣。

回答by Leox

Never do these things in onCreate! You can do it in a View.onClickListener or other place.

永远不要在 onCreate 中做这些事情!您可以在 View.onClickListener 或其他地方执行此操作。

回答by Tobias

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 ...

唯一的问题是,我无法获得我视图的全部尺寸。view.getwidth 返回 null ...

回答by Paulina D.

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

检查您的视图是否不大于屏幕尺寸。我找到了这个并切换了我的视图的尺寸,现在它可以工作了 =) Android 将完整视图作为位图

回答by Paulina D.

As far as I read the last days in the documentation you should onlycall setDrawingCacheEnabled or buildDrawingChache() but not both.

就我在文档中阅读的最后几天而言,您应该只调用 setDrawingCacheEnabled 或 buildDrawingChache() 而不是两者。