Home > Net >  View.getDrawingCache() always return Black area in Bitmap for transparent PNG. Can we change Tranpar
View.getDrawingCache() always return Black area in Bitmap for transparent PNG. Can we change Tranpar

Time:11-27

I have a method for screenshot which takes the View from XML and convert to Bitmap object, and I have multiple PNG on layout View. Some PNG have transparent Area which appears as Black color in Bitmap (which i want to change to White) or want to get rid of Black transparent area.

private void takescreenshot(LinearLayout preview) throws IOException
{
    View z  = preview;   // get whole layout view
    z.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(z.getDrawingCache());
    z.destroyDrawingCache();
}

CodePudding user response:

create a new blank bitmap
create canvas object
fill the canvas a background
put old bitmap in your canvas.

Bitmap newBitmap = Bitmap.create(bitmap.width,bitmap.height,ARGB_8888)
Canvas canvas = new Canvas(newBitmap)
canvas.drawColor(Color.white)
canvas.drawBitmap(bitmap,0F,0F,null)

CodePudding user response:

finally i figure out the solution as well

View z  = preview;   // get whole layout view
        z.setDrawingCacheEnabled(true);
        Bitmap bitmap = Bitmap.createBitmap(z.getDrawingCache());
        z.destroyDrawingCache();

Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),ARGB_8888);
        newBitmap.eraseColor(Color.WHITE);
        Canvas canvas2 = new Canvas(newBitmap);
        canvas2.drawBitmap(bitmap,0F,0F,null);
  • Related