views:

582

answers:

2

Hello,

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?

A: 

As far as I read the last days in the documentation you should onlycall setDrawingCacheEnabled or buildDrawingChache() but not both.

Roflcoptr
I know, that setDrawingCacheEnabled and buildDrawingChache is redundant. setDrawingCacheEnabled forces to build the drawing cache on demand and buildDrawingChache creates the cache without setting the the enable-flag. but it doesn't work for me - neither only setting the flag, nor the using the explicit call buildDrawingChache without setting the flag.
Impression
+1  A: 

use

onLayout(x, y, width, height);

for ex:

onLayout(0, 0, 100, 100);

before calling getDrawingCache

Vinay
thx... that works
Impression