How to make view.getDrawingCache () preserve transparency

If I have a view with a transparent background, and I do bitmap = view.getDrawingCache(); , This bitmap is unfortunately no more transparent . The background is set to black.

I even tried

view.setDrawingCacheBackgroundColor(Color.TRANSPARENT);

without success.

Actually this method allows you to set the background color without alpha support, Color.TRANSPARENT , which 0x00000000 is actually black, if you don't need the alpha part ... If I use Color.RED , the background is really very red.

Any idea to make this work? Is this a limitation of the current Android API? Is it possible to use draw () instead? but is it less indicative that this view.getDrawingCache() I suppose (without a cache)?

thanks

+5
source share
2 answers

Painting cropping is something like an Android relic with acceleration to HW, so some things might be a little confusing / not as well documented.

Transparency should work just fine until you exit View#setDrawingCacheBackgroundColor(int) , as this can cause the cache to drop to 16-bit color space (see Viewing # mDrawingCacheBackgroundColor ).

This code

 view.setDrawingCacheEnabled(true); // wait for first layout ... Bitmap b = view.getDrawingCache(); 

should provide you a bitmap ARGB_8888 with transparent background. (You can also do this in the Android Studio debugger by setting a breakpoint after calling the call and "View bitmap" for the variable.

0
source

Try this code after setting the background color of the layout transparent in the XML file

  layout.setDrawingCacheEnabled(true); Bitmap bmp = layout.getDrawingCache(); File mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "smiley1.png"); FileOutputStream outStream; outStream = new FileOutputStream(mFile); bmp.compress(Bitmap.CompressFormat.PNG, 100, outStream); 
0
source

Source: https://habr.com/ru/post/1236950/


All Articles