TextView(getDrawingCache)中的位图始终为null

ran*_*ser 6 android view

我试图提取与显示的TextView实例关联的位图,但它总是返回一个空值.我究竟做错了什么?我应该使用textview.draw(canvas)吗?

    TextView textview = (TextView) findViewById(R.id.text_title);
    textview.setDrawingCacheEnabled(true);
    textview.buildDrawingCache();        
    Bitmap bmp = textview.getDrawingCache();
Run Code Online (Sandbox Code Playgroud)

var*_*waj 9

在获得绘图缓存之前执行此操作将解决问题

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

然后getDrawingCache()将返回一个位图并在画布上绘制该位图.

如果你在你的应用程序中使用位图,则更喜欢通过调用它们上的recycle()方法从内存中清除它们,以便从内存中清除位图以保证安全,避免outOfMemoryException


Ell*_*tus 7

Android具有最大绘图缓存大小.如果绘图缓存大于此值,则getDrawingCache()将返回null.看到这个问题的答案.

您可以在此问题的答案中找到解决方法.