如何使用圆形画布图像获取特定裁剪图像?

4

enter image description here我创建了一个带有缩放和圆形裁剪图片功能的自定义ImageView。缩放功能正常,但是当我尝试裁剪图片时,无法得到特定的圆形图片。我使用的是基于onTuchListener的Pinch in-out功能和基于canvas类的圆形裁剪功能。

我使用了以下代码进行Pinch in-out和圆形裁剪图片:

@Override
    protected void onDraw(Canvas canvas) {
        onDrawReady = true;
        imageRenderedAtLeastOnce = true;
        if (delayedZoomVariables != null) {
            setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType);
            delayedZoomVariables = null;
        }
        super.onDraw(canvas);

        if (bitmap == null) {
            circleWindowFrame(); //Creating circle view
        }
        canvas.drawBitmap(bitmap, 0, 0, null);
    }

    protected void circleWindowFrame() {
        bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        Canvas osCanvas = new Canvas(bitmap);

        RectF outerRectangle = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(getResources().getColor(R.color.overlay));
        paint.setAlpha(99);
        osCanvas.drawRect(outerRectangle, paint);

        paint.setColor(Color.TRANSPARENT);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
        float centerX = getWidth() / 2;
        float centerY = getHeight() / 2;

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        int width = metrics.widthPixels;
        float radius = width / 2;
        osCanvas.drawCircle(centerX, centerY, radius, paint);
    }

这段代码用于裁剪图片:

public static Bitmap getCrop() {
        Bitmap circleBitmap;
        circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
        Paint paint = new Paint();
        paint.setShader(shader);
        paint.setAntiAlias(true);
        Canvas c = new Canvas(circleBitmap);
        c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
        return bitmap;
    }

感谢您的提前阅读。
谢谢。

请使用此库 https://github.com/vinc3m1/RoundedImageView 来实现圆形图片。 - Aman Verma
不,我需要圆形裁剪图像。请参见附图。 - Shanmugavel GK
你有检查我的答案吗? - Aman Verma
1个回答

1
尝试这个。
 public static Bitmap toOvalBitmap(@NonNull Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

        Canvas canvas = new Canvas(output);

        int color = 0xff424242;
        Paint paint = new Paint();

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);

        RectF rect = new RectF(0, 0, width, height);
        canvas.drawOval(rect, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, 0, 0, paint);

        bitmap.recycle();

        return output;
    }

我需要裁剪所选的覆盖层圆形区域。但是你的示例正在裁剪整个图像。 - Shanmugavel GK
你有没有使用我的方法而不是你自己的getCrop()方法? - Aman Verma
在你的问题中,你说它无法正确裁剪,我所做的是一种裁剪方法,所以我认为它应该适用于你,考虑到你的方法正在裁剪图像的所需覆盖区域。 - Aman Verma
去看看这个库https://github.com/ArthurHub/Android-Image-Cropper,虽然你需要更改一些参数才能得到所需的结果,但这肯定会帮助你的。要有耐心。 - Aman Verma
兄弟,我已经添加了缩放功能。所以你的方法会裁剪整张图片。但是我需要在图片缩放后裁剪高亮区域。我知道 getCrop() 方法不能正常工作。 - Shanmugavel GK
请查看我在上一条评论中提到的 Github 项目,它肯定有缩放功能。 - Aman Verma

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接