Android Crop Bitmap Canvas

与世无争的帅哥 提交于 2020-01-06 03:08:35

问题


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

    }

来源:https://stackoverflow.com/questions/36943983/android-crop-bitmap-canvas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!