Android View.getDrawingCache返回null,仅null
有人请尝试向我解释为什么
public void addView(View child) {
child.setDrawingCacheEnabled(true);
child.setWillNotCacheDrawing(false);
child.setWillNotDraw(false);
child.buildDrawingCache();
if(child.getDrawingCache() == null) { //TODO Make this work!
Log.w("View", "View child's drawing cache is null");
}
setImageBitmap(child.getDrawingCache()); //TODO MAKE THIS WORK!!!
}
总是记录绘图缓存为空并将位图设置为空吗?
在设置缓存之前我是否必须实际绘制视图?
谢谢!
Would anyone please try to explain to me why
public void addView(View child) {
child.setDrawingCacheEnabled(true);
child.setWillNotCacheDrawing(false);
child.setWillNotDraw(false);
child.buildDrawingCache();
if(child.getDrawingCache() == null) { //TODO Make this work!
Log.w("View", "View child's drawing cache is null");
}
setImageBitmap(child.getDrawingCache()); //TODO MAKE THIS WORK!!!
}
ALWAYS logs that the drawing cache is null, and sets the bitmap to null?
Do I have to actually draw the view before the cache is set?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
我也遇到了这个问题并找到了答案:
I was having this problem also and found this answer:
如果
getDrawingCache
总是返回 null
伙计们:使用这个:
参考:https://stackoverflow.com/a/6272951/371749
if
getDrawingCache
is alwaysreturning null
guys:use this:
Reference: https://stackoverflow.com/a/6272951/371749
得到空值的基本原因是视图没有被提及。那么,使用 view.getWidth()、view.getLayoutParams().width 等(包括 view.getDrawingCache() 和 view.buildDrawingCache())的所有尝试都是无用的。
因此,您需要首先设置视图的尺寸,例如:(
您已经根据需要设置了“宽度”和“高度”或通过 WindowManager 等获取了它们)
The basic reason one gets nulls is that the view is not dimentioned. All attempts then, using view.getWidth(), view.getLayoutParams().width, etc., including view.getDrawingCache() and view.buildDrawingCache(), are useless.
So, you need first to set dimensions to the view, e.g.:
(You have already set 'width' and 'height' as you like or obtained them with WindowManager, etc.)
我用这个代替。
Im using this instead.
工作最好 4me
Work best 4me
如果你想要捕获的视图确实显示在屏幕上,但它返回 null。这意味着您可以在窗口管理器生成视图之前捕获视图。有些布局非常复杂。如果布局包含嵌套布局、layout_weight等,则会导致多次重新布局才能获得准确的大小。最好的解决方案是等待窗口管理器完成工作,然后获取屏幕截图。尝试将 getDrawingCache() 放入处理程序中。
If the view you want catch really shows on screen, but it return null. That means you catch the view before Window manager generate it. Some layouts are very complicated. If layout includes nested layouts, layout_weight..etc, it causes several times relayout to get exactly size. The best solution is waiting until window manager finish job and then get screen shot. Try to put getDrawingCache() in handler.
@cV2 和@nininho 的答案都对我有用。
我发现的另一个原因是,我所拥有的视图正在生成一个宽度和高度为 0 的视图(即想象有一个带有空字符串的字符串文本的 TextView)。在这种情况下,getDrawingCache 将返回 null,因此请务必检查这一点。希望能帮助一些人。
@cV2 and @nininho's answers both work for me.
One of the other reasons that i found out the hard way was that the view that i had was generating a view that has width and height of 0 (i.e. imagine having a TextView with a string text of an empty string). In this case, getDrawingCache will return null, so just be sure to check for that. Hope that helps some people out there.
该错误可能是因为您的视图太大,无法放入绘图缓存。
我从日志中得到了“View.getDrawingCache 返回 null”问题的解释:
Android 文档还 说:Android 设备的单个应用程序可用内存只有 16MB。这就是为什么您无法加载大位图。
The error may be bacause your View too large to fit into drawing cache.
I got the explanation of my "View.getDrawingCache returns null" problem from my logs:
Android docs also says: Android devices can have as little as 16MB of memory available to a single application. That is why you can not load big bitmap.
我正在尝试根据视图动态创建 n 个图像。
我认为这段代码会对某人有所帮助..
I am trying to creating the n number of images dynamically based on view.
I think this code will help someone..
这是获取位图的简单有效的方法
This the simple and efficient way to get bitmap