-1

i have an activity to display an image, i am able to zoom the image and move it, i have a rectangle in the center of the screen which doesnt move, i want all what it is inside this rectangle to be able to crop the image , how can i do it ?

I have a custom class only for rectangle's draw here is my code :

@Override
    public void onDraw(Canvas canvas) {
        super.onDraw(canvas);
Rect mCropRectangle = new Rect();
        mCropRectangle.set(
                getLeft(),
                getTop()+(getBottom()+getTop())/6,
                getRight(),
                getBottom()-(getBottom()+getTop())/6
                );
}
Blerim Blerii
  • 223
  • 3
  • 16

1 Answers1

1

You should be able to use PorterDuff to achieve this. I've pasted an example which will crop a big yellow background using a blue mask.

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //create a second canvas
        Bitmap mask = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(mask);

        Rect cropRect = new Rect(100, 100, 400, 400);

        Paint p = new Paint();
        p.setAntiAlias(true);
        p.setColor(Color.BLUE); //color doesn't matter
        c.drawRect(cropRect, p); //draw the crop rect first

        p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); //change transfer mode

        p.setColor(Color.YELLOW);//draw your original image/content here, pretty much whatever you wanted to draw
        c.drawRect(0, 0, getWidth(), getHeight(), p); 

        canvas.drawBitmap(mask, 0, 0, null); //draw the result back onto the canvas

    }
WindsurferOak
  • 4,861
  • 1
  • 31
  • 36