I'm trying to erase an image from another image , but i have a problem with (Width & Height) of image which i want to erase , in my code below i tried to getWidth() & getHeight() of the fixed Image and set them in Bitmap.createBitmap() but i get this :

enter image description here

XML

<com.xiaopo.flying.sticker.StickerView>
    <ImageView ........./>  /*Fixed Image*/
    <LinearLayout ........./> /*Erased Image*/
</com.xiaopo.flying.sticker.StickerView>

I want to crop the erased image ,to fit the fixed image like this :

enter image description here

MainActivity.java :

   linear = (LinearLayout) findViewById(R.id.linear);
        try {
            drawImg = new DrawView(this, MediaStore.Images.Media.getBitmap(this.getContentResolver(), uriPhoto));
        } catch (IOException e) {
            e.printStackTrace();
        }
        linear.addView(drawImg); 

DrawView.java

public class DrawView extends View implements View.OnTouchListener {

        private int x = 0;
        private int y = 0;
        Bitmap bitmap;
        Path circlePath;
        Paint circlePaint;
        private final Paint paint = new Paint();
        private final Paint eraserPaint = new Paint();

        @SuppressLint("ClickableViewAccessibility")
        public DrawView(Context context, Bitmap bitmapHelper) {
            super(context);
            setFocusable(true);
            setFocusableInTouchMode(true);
            this.setOnTouchListener(this);
            this.setBackgroundColor(Color.TRANSPARENT);
            bp = BitmapFactory.decodeResource(getResources(), R.drawable.hearto);
            bitmap = Bitmap.createBitmap(bitmapHelper.getWidth(), bitmapHelper.getHeight(), Bitmap.Config.ARGB_8888);
            bitmapCanvas = new Canvas();
            bitmapCanvas.setBitmap(bitmap);
            bitmapCanvas.drawColor(Color.TRANSPARENT);
            bitmapCanvas.drawBitmap(bp, 0, 0, null);
            circlePath = new Path();
            circlePaint = new Paint();
            circlePaint.setAntiAlias(true);
            circlePaint.setColor(Color.TRANSPARENT);
            circlePaint.setStyle(Paint.Style.STROKE);
            circlePaint.setStrokeJoin(Paint.Join.MITER);
            circlePaint.setStrokeWidth(4f);
            eraserPaint.setAlpha(0);
            eraserPaint.setStrokeJoin(Paint.Join.ROUND);
            eraserPaint.setStrokeCap(Paint.Cap.ROUND);
            eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            eraserPaint.setAntiAlias(true);

        }

        @Override
        public void onDraw(Canvas canvas) {

            canvas.drawBitmap(bitmap, 0, 0, paint);
            bitmapCanvas.drawCircle(x, y, 30, eraserPaint);

            canvas.drawPath(circlePath, circlePaint);
        }

        public boolean onTouch(View view, MotionEvent event) {
            x = (int) event.getX();
            y = (int) event.getY();
            bitmapCanvas.drawCircle(x, y, 30, eraserPaint);
            circlePath.reset();
            circlePath.addCircle(x, y, 30, Path.Direction.CW);
            int ac = event.getAction();
            if (ac == MotionEvent.ACTION_UP) {
                circlePath.reset();
            }
            invalidate();
            return true;
        }
    }

0 Answers