Android Crop Bitmap Canvas

2019-07-30 08:53发布

问题:

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);

    }