从TextView中(getDrawingCache)位图总是空 [英] Bitmap from TextView (getDrawingCache) always null

查看:897
本文介绍了从TextView中(getDrawingCache)位图总是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取相关的显示TextView的实例的位图,但它总是返回null值。我究竟做错了什么?我应该使用textview.draw(画布)呢?

 的TextView的TextView =(的TextView)findViewById(R.id.text_title);
    textview.setDrawingCacheEnabled(真);
    textview.buildDrawingCache();
    BMP位图= textview.getDrawingCache();


解决方案

做到这一点得到绘制缓存它会解决这个问题之前,

view.measure(MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED)
            MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
view.layout(0,0,view.getMeasuredWidth(),view.getMeasuredHeight());

然后 getDrawingCache()将返回一个位图和画在画布上的位图。

如果你正在使用你的应用程序preFER位图从内存中清除它们通过调用它们回收()方法,这样得到的位图从内存中清除您的安全方面,以避免OutOfMemoryException异常

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

解决方案

do this before getting drawing cache it will solve the problem

view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());

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

这篇关于从TextView中(getDrawingCache)位图总是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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