Android 2.1 View 的 getDrawingCache() 方法始终返回 null

发布于 2024-09-01 22:33:33 字数 754 浏览 3 评论 0原文

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

示例代码:

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);

}

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

什么是正确的方法,或者我做错了什么?

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

冷夜 2024-09-08 22:33:33
Bitmap screenshot;
view.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

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

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.

多孤肩上扛 2024-09-08 22:33:33

用于

onLayout(x, y, width, height);

ex:

onLayout(0, 0, 100, 100);

在调用 getDrawingCache 之前

use

onLayout(x, y, width, height);

for ex:

onLayout(0, 0, 100, 100);

before calling getDrawingCache

爱你是孤单的心事 2024-09-08 22:33:33

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

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

赴月观长安 2024-09-08 22:33:33

我知道这并不能回答您的问题,但是...

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

你为什么需要这个?我认为 Canvas 类存在疏忽。 Canvas 类无法让您读取画布内容或获取其后面的位图。因此,如果您需要读取位图,例如获取视图内容的图像转储,除了上述方法之外,我没有其他方法可以做到这一点。

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

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.

生生漫 2024-09-08 22:33:33

据我最后几天阅读的文档,您应该只调用 setDrawingCacheEnabled 或 buildDrawingChache() 但不能同时调用两者。

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

白馒头 2024-09-08 22:33:33

遇到了同样的问题,最后这对我来说部分有效:

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());
    }

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

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

青衫负雪 2024-09-08 22:33:33

检查您的视图是否不大于屏幕尺寸。我发现了这个并切换了我的视图尺寸,所以现在它可以工作了 =)
Android 以位图形式获取完整视图

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文