GetDrawingCache () returns null

I am working on a simple drawing application and am trying to implement functionality that provides more space for drawing when the user asks for what, in my opinion, can be done by simply turning on the scrolling of my CustomView class (which is contained in LinearLayout then in the ScrollView class). If I didn't resize the CustomView class by running Chunk 1, Chunk 2 works fine (it just saves one drawing screen. There is no scrolling at this time.) Bummer! Chunk 2 failed (view.getDrawingCache () returns null) when starting Chunk 1 (scrolling enabled at this time). I want to keep the whole view, including the rest, due to scrolling.

Part 1:

CustomView view = (CustomView) findViewById(R.id.customViewId); height = 2 * height; view.setLayoutParams(new LinearLayout.LayoutParams(width, height)); 

Part 2:

 CustomView view = (CustomView) findViewById(R.id.customViewId); view.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache()); view.setDrawingCacheEnabled(false); 

These two pieces of code are independent and small parts in two different methods.

Edited: SOLVED

After billions of Google searches, the problem is now resolved;)
I referred to this topic, https://groups.google.com/forum/?fromgroups=#!topic/android-developers/VQM5WmxPilM .
I did not understand that the getDrawingCache () method has a limit on the size of the View class (width and height). If the View class is too large, getDrawingCache () simply returns null. Therefore, the solution was not to use this method, but instead do it as shown below.

 CustomView view = (CustomView) findViewById(R.id.customViewId); Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas bitmapHolder = new Canvas(bitmap); view.draw(bitmapHolder); // bitmap now contains the data we need! Do whatever with it! 
+7
source share
1 answer

You need to call buildDrawingCache() before using the bitmap.

The setDrawingCache(true) object simply sets the flag and waits for the next drawing pass to create a cache bitmap.

Also remember to call destroyDrawingCache() when you no longer need it.

+3
source

All Articles