gpt4 book ai didi

android - 来自 TextView (getDrawingCache) 的位图始终为 null

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:26:54 25 4
gpt4 key购买 nike

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

    TextView textview = (TextView) findViewById(R.id.text_title);
textview.setDrawingCacheEnabled(true);
textview.buildDrawingCache();
Bitmap bmp = textview.getDrawingCache();

最佳答案

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

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

然后 getDrawingCache() 将返回一个位图并在 Canvas 上绘制该位图。

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

关于android - 来自 TextView (getDrawingCache) 的位图始终为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5566758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com