android - 如何使用 Canvas 圆形图像获取特定裁剪图像?

标签 android imageview android-canvas

enter image description here我正在创建一个自定义 ImageView,它带有捏合缩放和圆形裁剪图像。捏合输出效果很好,但是当我尝试裁剪图像时,无法获得特定的圆形图像。我正在使用基于 onTuchListener 的 Pinch in-out 工作和基于 canvas 类的 Circular Cropping。

我已经使用下面提到的代码进行捏合和圆形裁剪图像:

@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;
    }

感谢提前...

最佳答案

试试这个

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

关于android - 如何使用 Canvas 圆形图像获取特定裁剪图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41099656/

相关文章:

Android EditText 在 Canvas 上

android - 我可以在 Cordova Android 应用程序和嵌入式设备(棋盘)之间传输数据吗?

android - 方向改变后,针对 "layout-ldltr"加载 "layout-ldrtl"布局中的 android 错误吗?

android - 按下按钮时`java.lang.UnsatisfiedLinkError: Native method not found: `

android 从 MySql 数据库中检索 blob 图像

android - 在 Touch Android 上具有拖动、滚动和缩放功能的三角形 ImageView

android - canvas.drawBitmap 没有绘制任何东西

android - 如何在 Android 模拟器中更改屏幕方向?

Android ImageView ScaleType 和项目高度

android - 无法在新 Activity 中打开图像