gpt4 book ai didi

Android 2.1 View 的 getDrawingCache() 方法总是返回 null

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

我正在使用 Android 2.1 并遇到以下问题:使用 View.getDrawingCache() 方法总是返回 null。 getDrawingCache() 应该返回一个 Bitmap,它是 View 内容的呈现。

示例代码:

public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

final View view = findViewById(R.id.ImageView01);
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
final Bitmap bmp = view.getDrawingCache();
System.out.println(bmp);

我已经尝试过不同的方法来配置 View 对象以生成绘图缓存(例如 View.setWillNotDraw(boolean)View.setWillNotCacheDrawing(boolean)) , 但没有任何效果。

什么是正确的方法,或者我做错了什么?

PS:在实际代码中,我想在 RelativeLayout 之类的 ViewGroup 上应用 getDrawingCache()。使用 ViewGroup 时行为是否相同?

最佳答案

Bitmap screenshot;
view.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);

您需要 Bitmap.createBitmap(view.getDrawingCache()); 因为 getDrawingCache() 从中接收引用的位图被回收。

关于Android 2.1 View 的 getDrawingCache() 方法总是返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2817166/

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