Android 2.1 View的getDrawingCache()方法始终返回null

Imp*_*ion 13 null android drawing caching view

我正在使用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);
Run Code Online (Sandbox Code Playgroud)

}

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

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

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

You*_*ave 17

Bitmap screenshot;
view.setDrawingCacheEnabled(true);
screenshot = Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(false);
Run Code Online (Sandbox Code Playgroud)

您需要Bitmap.createBitmap(view.getDrawingCache());通过getDrawingCache()回收来接收引用的位图.

  • 对我不起作用!说:Bitmap.createBitmap()中的source为null (4认同)

Vin*_*nay 5

使用

onLayout(x, y, width, height);
Run Code Online (Sandbox Code Playgroud)

对于前:

onLayout(0, 0, 100, 100);
Run Code Online (Sandbox Code Playgroud)

在调用getDrawingCache之前

  • 它只是:layout.layout(0,0,wt,ht); (3认同)

小智 0

据我最后几天阅读的文档,您应该只调用 setDrawingCacheEnabled 或 buildDrawingChache() 但不能同时调用两者。