Android 2.1 View 的 getDrawingCache() 方法总是返回 null [英] Android 2.1 View's getDrawingCache() method always returns null

查看:38
本文介绍了Android 2.1 View 的 getDrawingCache() 方法总是返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

示例代码:

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)),但没有任何效果.

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:在实际代码中,我想在像 RelativeLayout 这样的 ViewGroup 上应用 getDrawingCache().使用 ViewGroup 时的行为是否相同?

PS: In real code I want to apply getDrawingCache() on a ViewGroup like RelativeLayout. Is the behaviour the same when using a ViewGroup?

推荐答案

Bitmap screenshot;
view.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

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

You need Bitmap.createBitmap(view.getDrawingCache()); as the Bitmap from which the reference is received by getDrawingCache() gets recycled.

这篇关于Android 2.1 View 的 getDrawingCache() 方法总是返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆