Android 2.1的视图的getDrawingCache()方法总是返回null视图、方法、Android、getDrawingCache

2023-09-12 21:42:23 作者:最后的疼爱是手放开

我的工作与Android 2.1,并具有以下问题: 使用方法View.getDrawingCache()始终返回null。 getDrawingCache()应该返回一个位图,这是查看的内容presentation。

例如code:

 公共无效的onCreate(最终捆绑savedInstanceState){
  super.onCreate(savedInstanceState);

  的setContentView(R.layout.main);

  最终的视图中查看= findViewById(R.id.ImageView01);
  view.setDrawingCacheEnabled(真正的);
  view.buildDrawingCache();
  最后的BMP位= view.getDrawingCache();
  的System.out.println(BMP);
 

}

我已经尝试了不同的方式来配置View对象生成图纸的高速缓存(如 View.setWillNotDraw(布尔)查看。 setWillNotCacheDrawing(布尔)),但没有任何工程。

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

PS:在现实code我想申请getDrawingCache()像RelativeLayout的一个ViewGroup中。使用的ViewGroup时的行为一样?

解决方案

 位图的截图;
view.setDrawingCacheEnabled(真正的);
截图= Bitmap.createBitmap(view.getDrawingCache());
view.setDrawingCacheEnabled(假);
 

您需要 Bitmap.createBitmap(view.getDrawingCache()); 为getDrawingCache()被回收。

opencv for android 教程 环境搭建篇

I'm working with Android 2.1 and have the following problem: Using the method View.getDrawingCache() always returns null. getDrawingCache() should return a Bitmap, which is the presentation of View's content.

Example code:

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

}

I've already tried different ways to configure the View object for generating the drawing cache (e.g. View.setWillNotDraw(boolean) and View.setWillNotCacheDrawing(boolean)), but nothing works.

What is the right way, or what I'm doing wrong?

PS: In real code I want to apply getDrawingCache() on a ViewGroup like RelativeLayout. Is the behaviour the same when using a ViewGroup?

解决方案

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

You need Bitmap.createBitmap(view.getDrawingCache()); as the Bitmap from which the reference is received by getDrawingCache() gets recycled.