gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-28 23:31:44 29 4
gpt4 key购买 nike

我正在尝试将 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com