Android 2.1 View 的 getDrawingCache() 方法始终返回 null
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要 Bitmap.createBitmap(view.getDrawingCache());,因为
getDrawingCache()
从中接收引用的位图会被回收。You need
Bitmap.createBitmap(view.getDrawingCache());
as the Bitmap from which the reference is received bygetDrawingCache()
gets recycled.用于
ex:
在调用 getDrawingCache 之前
use
for ex:
before calling getDrawingCache
永远不要在 onCreate 中做这些事情!
您可以在 View.onClickListener 或其他地方执行此操作。
Never do these things in onCreate!
You can do it in a View.onClickListener or other place.
我知道这并不能回答您的问题,但是...
正如其他开发人员阅读本文的旁白,如果您真正想要做的是访问自定义视图的内容,请从 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.
据我最后几天阅读的文档,您应该只调用 setDrawingCacheEnabled 或 buildDrawingChache() 但不能同时调用两者。
As far as I read the last days in the documentation you should onlycall setDrawingCacheEnabled or buildDrawingChache() but not both.
遇到了同样的问题,最后这对我来说部分有效:
唯一的问题是,我无法获得视图的完整尺寸。
view.getwidth 返回 null ...
had the same problem and finally this partly works for me:
the only problem is, that I can't get the full dimensions of my view.
view.getwidth returns null ...
检查您的视图是否不大于屏幕尺寸。我发现了这个并切换了我的视图尺寸,所以现在它可以工作了 =)
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