Android Crop Bitmap Canvas
Asked Answered
M

0

1

I want to create the crop bitmap functionality and have referred Android: Free Croping of Image but this sets the bitmap in another imageView.Now I know I can set the cropped bitmap in the canvas but I want to retain the original bitmap and want to do the cropping in onDraw() and not make a different bitmap and then set it in canvas.I have tried to implement the below code in onDraw but there is no cropping and bitmap remains as it is.Pleas help so that I can code this in onDraw() method of the CustomView.

compositeImageView = (ImageView) findViewById(R.id.our_imageview);

        Bitmap bitmap2 = BitmapFactory.decodeResource(getResources(),
                R.drawable.gallery_12);

        Bitmap resultingImage = Bitmap.createBitmap(widthOfscreen,
                heightOfScreen, bitmap2.getConfig());

        Canvas canvas = new Canvas(resultingImage);
        Paint paint = new Paint();
        paint.setAntiAlias(true);

        Path path = new Path();
        for (int i = 0; i < SomeView.points.size(); i++) {
            path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
        }
        canvas.drawPath(path, paint);
            paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

        canvas.drawBitmap(bitmap2, 0, 0, paint);
        compositeImageView.setImageBitmap(resultingImage);

my onDraw() method

@Override
    protected void onDraw(Canvas canvas)
    {
            Bitmap bitmap1 = bitmap.copy(bitmap.getConfig(), true);

            canvas.drawBitmap(bitmap1, 0,0, null);
            Paint paint = new Paint();
            paint.setAntiAlias(true);

            Path path = new Path();
            for (int i = 0; i < SomeView.points.size(); i++) {
                path.lineTo(SomeView.points.get(i).x, SomeView.points.get(i).y);
            }
            canvas.drawPath(path, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

            canvas.drawBitmap(bitmap,0, 0, paint);

    }
Mcwhirter answered 29/4, 2016 at 17:10 Comment(3)
just call Canvas#drawPath with the Paint object which has set the BitmapShader, no need for Bitmap#copy, xfer modes and Canvas#drawBitmapEarthly
so your onDraw will contain only one line of code: canvas.drawPath(path, paint); everything else (setting the Shader, Path etc) can be done in your custom View constructorEarthly
and this is how it (more or less) worksEarthly

© 2022 - 2024 — McMap. All rights reserved.