How to add an alpha channel to a bitmap

My code is below. This is the same code as in the solution for this question: Make transparent transparent area transparent area

And like many others, I have the same problem with this code: the circle comes out black.

I am using a PNG file as my overlay and this file has no transparent areas. But as soon as I add an arbitrary transparent area to PNG in Photoshop, the code starts to work and the circle appears as transparent.

Apparently there is something with the image and how its opacity is set, but I don't know what. I need to use PNG without any transparent areas as my overlay.

any advice?

EDIT: good code shouldn't depend on the transparency of the overlay image or not, I am looking for a way to treat any image as my overlay, be it JPG, PNG or whatever.

EDIT 2: If I use Config.ARGB_4444

when copying my bitmap, an alpha channel is created, but this format reduces the quality of the image. There is a Bitmap.setHasAlpha () API for API level 11 and up, but I'm still using level 10.


public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new TouchView(this));
    }

    class TouchView extends View {
        Bitmap bgr;
        Bitmap overlayDefault;
        Bitmap overlay;
        Paint pTouch;
        int X = 100;
        int Y = 100;
        Canvas c2;

        public TouchView(Context context) {
            super(context);
            bgr = BitmapFactory.decodeResource(
                    getResources(),
                    R.drawable.background);
            overlay = BitmapFactory.decodeResource(
                    getResources(),
                    R.drawable.foreground)
                      .copy(Config.ARGB_8888, true);
            c2 = new Canvas(overlay);
            pTouch = new Paint(Paint.ANTI_ALIAS_FLAG);
            pTouch.setXfermode(
                  new PorterDuffXfermode(Mode.SRC_OUT));
            pTouch.setColor(Color.TRANSPARENT);
        }

        @Override
        public void onDraw(Canvas canvas){
            super.onDraw(canvas);
            //draw background
            canvas.drawBitmap(bgr, 0, 0, null);
            //copy the default overlay
            // into temporary overlay and punch a hole in it                          
            //c2.drawBitmap(overlayDefault, 0, 0, null);
            c2.drawCircle(X, Y, 80, pTouch);
            //draw the overlay over the background  
            canvas.drawBitmap(overlay, 0, 0, null);
        }
    }
}

      

+3


source to share


1 answer


From http://blog.uncommons.org/2011/01/12/adjusting-the-opacity-of-an-android-bitmap/

**
 * @param bitmap The source bitmap.
 * @param opacity a value between 0 (completely transparent) and 255 (completely opaque).
 * @return The opacity-adjusted bitmap.  If the source bitmap is mutable it will be
 * adjusted and returned, otherwise a new bitmap is created.
 */
private Bitmap adjustOpacity(Bitmap bitmap, int opacity)
{
    Bitmap mutableBitmap = bitmap.isMutable()
                           ? bitmap
                           : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int colour = (opacity & 0xFF) << 24;
    canvas.drawColor(colour, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

      



Note that the Bitmap.Config documentation for ARGB_4444 says:

This field was deprecated in API level 13. Due to the poor quality of this configuration, it is recommended to use ARGB_8888.

+1


source







All Articles