TextView (getDrawingCache) 中的位图始终为 null
我试图提取与显示的 TextView 实例关联的位图,但它总是返回空值。我做错了什么?我应该使用 textview.draw(canvas) 代替吗?
TextView textview = (TextView) findViewById(R.id.text_title);
textview.setDrawingCacheEnabled(true);
textview.buildDrawingCache();
Bitmap bmp = textview.getDrawingCache();
I am trying to extract the bitmap associated to the displayed TextView instance, but it always returns a null value. What Am I doing wrong? Should I use textview.draw(canvas) instead?
TextView textview = (TextView) findViewById(R.id.text_title);
textview.setDrawingCacheEnabled(true);
textview.buildDrawingCache();
Bitmap bmp = textview.getDrawingCache();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在获取绘图缓存之前执行此操作将解决问题
,然后 getDrawingCache() 将返回一个位图并将该位图绘制在画布上。
如果您在应用程序中使用位图,则更愿意通过调用它们的 recycle() 方法来从内存中清除它们,以便从内存中清除位图以确保安全,以避免 outOfMemoryException
Do this before getting drawing cache it will solve the problem
and then
getDrawingCache()
will return a bitmap and draw that bitmap on your canvas.and if you are using bitmaps in your app prefer to clear them from memory by calling recycle() method on them so that bitmap get cleared from the memory for your safe side to avoid outOfMemoryException
Android 有最大绘图缓存大小。如果绘图缓存大于该值,则 getDrawingCache() 返回 null。请参阅此问题的答案。
您可以在 这个问题。
Android has a maximum drawing cache size. If the drawing cache would be bigger than that, getDrawingCache() returns null. See the answer to this question.
You can find a workaround in the answer to this question.
应该是:
should be: