首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Canvas.clipPath(路径)未按预期进行裁剪

Canvas.clipPath(路径)未按预期进行裁剪
EN

Stack Overflow用户
提问于 2012-12-03 03:19:26
回答 2查看 21.9K关注 0票数 21

我正在尝试将画布绘制操作裁剪到一个弧形的楔子上。然而,在设置画布的裁剪路径后,我没有得到预期的结果。

为了说明,我是这样做的:

代码语言:javascript
复制
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))时,它将绘制如上所示的楔形。但是,当我将此路径设置为画布的剪切路径并绘制到其中时:

代码语言:javascript
复制
//I've tried it with and without the Region.Op parameter
canvas.clipPath(path, Region.Op.REPLACE);
canvas.drawColor(Color.BLUE);

我得到了以下结果(按钮留下来只是为了显示引用):

因此,它似乎裁剪到Path的边界矩形,而不是Path本身。你知道这里发生了什么吗?

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

代码语言:javascript
复制
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
    );
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-03 03:42:29

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

如果是这样的话,clipPath是不受支持且有问题的。

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

票数 13
EN

Stack Overflow用户

发布于 2013-09-05 23:56:26

OP的问题是关于使用裁剪区域的,已经由@Simon回答了。但是,请记住,有一种更直接的方法来绘制一条完整的弧线:

创建Paint

代码语言:javascript
复制
mPaint = new Paint();
mPaint.setColor(Color.BLUE);
mPaint.setStyle(Style.FILL);
mPaint.setAntiAlias(true);

绘制时,只需绘制路径:

代码语言:javascript
复制
canvas.drawPath(path, mPaint);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13672802

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档