-1

I need to write an Android-Application that uses an existing PNG-File, changes it's transparency (let's say 50%) and overwrites that file.

I already tried to open the file as a bitmap, change the paint-alpha there and save it again, but it always looses it's transparency value.

MyBitmap.compress(Bitmap.CompressFormat.PNG, 100, myOutputStream);

I already read that changing the quality-value to 0 might help, but that also didn't do the trick:

MyBitmap.compress(Bitmap.CompressFormat.PNG, 100, myOutputStream);

I can prove that my transparency-funktion works but when I try to save that image as a PNG, the transparency is lost. This is how I make the bitmap transparent:

Paint AlphaPaint = new Paint();
AlphaPaint.setAlpha(Math.round(Opacity * 255));
Canvas canvas = new Canvas(MyImage);
canvas.drawBitmap(..., ..., ..., AlphaPaint);

Any help is appreciated! Performance does not matter in this case, if using a library is the easiest way, that's definately ok. Thanks in advance!

1 Answers1

1

Try this way just iterate through all the pixels and then reduce the alpha by whatever percentage you want and set back the pixel to that bitmap.

int A=0, B=0, C=0, D=0, color=0, ncolor=0;

for(int i=0; i<width; i++)
{
    for(int j=0; j<height; j++)
    {
        color = bitmap.getPixel(i,j);
        A = (color >> 24) & 0xff; // Get Alpha
        R = (color >> 16) & 0xff;
        G = (color >>  8) & 0xff;
        B = (color) & 0xff;
        A = A/2; //REDUCE BY HALF SO DIVIDE BY 2(50%)
        ncolor = (A & 0xff) << 24 | (R & 0xff) << 16 | (G & 0xff) << 8 | (B & 0xff);
        bitmap.setPixel(i,j,ncolor);
    }
}

Its just a basic idea how you can solve your problem. Hope this helps you out.. Thank You.

Paul P Joby
  • 713
  • 7
  • 17
  • Thanks for your reply! But how does that help me, in the end I also need to save the bitmap with the .compress()-function and at this part, the alpha-value gets lost. – Manuel Maurer Aug 08 '19 at 10:24
  • `Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);` and try ` createBitmap(int[] colors, int width, int height, Bitmap.Config config)` to create bitmap from array of colors that can be obtained from an existing bitmap. – Paul P Joby Aug 08 '19 at 12:15
  • please try creating the bitmap with Bitmap Config as ARGB_8888. You need the bitmap with this configuration to preserve alpha value. please read https://developer.android.com/reference/android/graphics/Bitmap and https://developer.android.com/reference/android/graphics/Bitmap.Config.html – Paul P Joby Aug 08 '19 at 12:17
  • https://developer.android.com/reference/android/graphics/BitmapFactory.html this documentation contains almost every method that can be used for minor image processing and is easy to use and supported by android by default as its something built-in. – Paul P Joby Aug 08 '19 at 12:25
  • https://stackoverflow.com/questions/25576869/how-to-set-bitmap-to-argb-8888-in-android this post also can be referred to know how to use the rgba8888 configuration to preserve alpha and how you can copy from an existing bitmap object to new bitmap object with this configuration. – Paul P Joby Aug 08 '19 at 12:30