Mask shape working but not appliying transparency

754 Views Asked by At

I am trying to use a mask to hide a part of a picture, depending on where the user touch the screen. Todo so, I followed a code sample provided by Cyril Mottier. What I did until now actually work : while clicking a part of my ImageView, all what I got above is hidden. The problem is it is hidden by black color, preventing what there is behind my ImageView from displaying.

Could anybody please provide me tips or tell me what I do wrong?

Here a screen of what I got at the time

Here is the main activity :

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

    final MaskedImageView iv = (MaskedImageView) findViewById(R.id.picture_on);
    iv.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d(MainActivity.class.getSimpleName(), "on touch called");

            Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay();
            Point point = new Point();
            display.getSize(point);

            Rect bounds = iv.getDrawable().getBounds();
            bounds.top = point.y
                    - (int) event.getY()
                    ;
            iv.setMask(bounds);
            iv.invalidate();
            return false;
        }
    });
}

And here the custom ImageView :

public class MaskedImageView extends ImageView {

    private final Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    private Bitmap mMask;

    public MaskedImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

        // Prepares the paint that will be used to draw our icon mask. Using
        // PorterDuff.Mode.DST_IN means the image that will be drawn will
        // mask the already drawn image.
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

    public MaskedImageView(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Prepares the paint that will be used to draw our icon mask. Using
        // PorterDuff.Mode.DST_IN means the image that will be drawn will
        // mask the already drawn image.
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

    public MaskedImageView(Context context) {
        super(context);

        // Prepares the paint that will be used to draw our icon mask. Using
        // PorterDuff.Mode.DST_IN means the image that will be drawn will
        // mask the already drawn image.
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    }

    public void setMask(Rect rect) {
        mMask = Bitmap.createBitmap(Math.abs(rect.right - rect.left),
                Math.abs(rect.bottom - rect.top), Bitmap.Config.ALPHA_8);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.save();
        BitmapDrawable bd = (BitmapDrawable) getDrawable();
        canvas.drawBitmap(bd.getBitmap(), 0, 0, null);

        if (mMask != null) {
            canvas.drawBitmap(mMask, 0, 0, mPaint);
        }
        canvas.restore();
    }


}

Thank you for your answers

0

There are 0 best solutions below