android - Canvas.clipPath(Path) 未按预期剪切

标签 android drawing android-canvas image-clipping

我正在尝试将 Canvas 绘图操作剪辑为弧形楔形。但是,在将剪切路径设置为 Canvas 后,我没有得到预期的结果。

为了说明,这是我正在做的事情:

enter image description here

path.reset();

//Move to point #1
path.moveTo(rect.centerX(), rect.centerY());

//Per the documentation, this will draw a connecting line from the current
//position to the starting position of the arc (at 0 degrees), add the arc
//and my current position now lies at #2.
path.arcTo(rect, 0, -30);

//This should then close the path, finishing back at the center point (#3)
path.close();

这行得通,当我简单地绘制这条路径(canvas.drawPath(path,paint))时,它会绘制如上所示的楔形。但是,当我将此路径设置为 Canvas 的剪切路径并绘制到其中时:

//I've tried it with and without the Region.Op parameter
canvas.clipPath(path, Region.Op.REPLACE);
canvas.drawColor(Color.BLUE);

我得到以下结果(留下楔形只是为了显示引用):

enter image description here

因此,它似乎被剪辑到 Path 的边界矩形,而不是 Path 本身。有什么想法吗?

编辑 作为更新,我发现了一种更有效的方法,它允许硬件加速。首先,将整个图像(您将要剪裁的)绘制到屏幕外位图中。使用此 Bitmap 制作一个 BitmapShader,将该着色器设置为 Paint,然后使用该 Paint 绘制楔形路径对象:

drawMyBitmap(bitmap);
Shader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setShader(shader);

@Override
public void onDraw(Canvas canvas) {
    canvas.drawArc(rect,         //The rectangle bounding the circle
                   startAngle,   //The angle (CW from 3 o'clock) to start
                   sweepAngle,   //The angle (CW from 3 o'clock) of the arc
                   true,         //Boolean of whether to draw a filled arc (wedge)
                   paint         //The paint with the shader attached
    );
}

最佳答案

您是使用 HC 或更高版本还是使用硬件加速?

如果是这样,clipPath 不受支持且存在问题。

developer.android.com/guide/topics/graphics/hardware-accel.html .

关于android - Canvas.clipPath(Path) 未按预期剪切,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13672802/

相关文章:

Android:使用 DrawPoint() 的 onDraw 方法导致 Canvas 上的抖动和失真

android - 是否可以通过调用 Canvas.drawText() 来显示多色文本?

Android ADB 在当前选项卡中打开 URL/关闭以前的选项卡

android - 运行时异常 : Width (0) and height (0) cannot be <= 0

java - 如何阻止通知面板隐藏?

javascript - 围绕一条线创建一个多边形

java - JPanel 绘图 - 为什么我必须重写 paintComponent 方法?

java - GridView 前面的绘图 View - Android

java - 使用 Lambda 设置 OnCheckedChangeListener

android - 如何像 Google Apps 一样在谷歌地图中同时显示 MarkerIcon 和标题?