Canvas.clipPath(路径)不削波如预期 [英] Canvas.clipPath(Path) not clipping as expected

查看:575
本文介绍了Canvas.clipPath(路径)不削波如预期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想剪辑画布绘制操作弧形的楔形。不过,我没有收到剪切路径设置到画布后的预期结果。

有关说明,这里是我在做什么:

  path.reset();

//移动到点#1
path.moveTo(rect.centerX(),rect.centerY());

//每页文档,这将绘制的连接线从目前的
//位置到弧(在0度)的开始位置,添加弧
//我现在的位置现在位于在#2。
path.arcTo(矩形,0,-30);

//这应该然后关闭路径,精加工回在中心点(#3)
path.close();
 

这工作,当我简单地画这条道路( canvas.drawPath(路径,涂料))它绘制楔形,如上图所示。然而,当我把这个路径作为画布的剪辑路径,并绘制成的:

  //我已经尝试过了有和没有Region.Op参数
canvas.clipPath(路径,Region.Op.REPLACE);
canvas.drawColor(Color.BLUE);
 

我得到以下的结果,而不是(楔形留给只是为了显示参考):

所以,反而好像夹到路径的边界RECT,而不是路径本身。任何想法这里发生了什么?

修改只是作为一个更新,我发现一个更有效的方式这样做,允许硬件加速。首先,绘制整个图像(你会被裁剪)到屏幕外的位图。做一个 BitmapShader 使用该位图,设置着色器到油漆,然后利用得出的楔形路径油漆目标:

  drawMyBitmap(位);
着色的着色器=新BitmapShader(位图,Shader.TileMode.CLAMP,Shader.TileMode.CLAMP);
涂料粉刷=新的油漆(Paint.ANTI_ALIAS_FLAG);
paint.setShader(着色);

@覆盖
公共无效的OnDraw(帆布油画){
    canvas.drawArc(矩形,//该矩形边界圆
                   startAngle开始,//角度(CW从3点)开始
                   sweepAngle,//弧的角度(​​CW从3点钟方向)
                   如此,是否绘制填充圆弧//布尔(楔)
                   //油漆涂料附着着色器
    );
}
 

解决方案

您使用HC或以上或以其它方式使用硬件加速?

如果是这样,clipPath是不支持的和有问题的。

<一个href="http://developer.android.com/guide/topics/graphics/hardware-accel.html">developer.android.com/guide/topics/graphics/hardware-accel.html.

I'm trying to clip a canvas drawing operation to an arc-shaped wedge. However, I'm not getting the intended result after setting the clipping path to the canvas.

For illustration, here is what I'm doing:

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();

This works, and when I simply draw this path (canvas.drawPath(path, paint)) it draws the wedge as shown above. However, when I set this path as the canvas's clipping path and draw into it:

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

I get the following result instead (the wedge is left just to show reference):

So it instead seems to clip to the bounding rect of the Path, and not the Path itself. Any ideas what's happening here?

EDIT Just as an update, I've found a much more efficient way of doing this that allows for hardware acceleration. First, draw the entire image (that you'd be clipping) into an offscreen bitmap. Make a BitmapShader using this Bitmap, set that shader to a Paint, then draw the wedge path using that Paint object:

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

解决方案

Are you using HC or above or otherwise using hardware acceleration?

If so, clipPath is unsupported and problematic.

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

这篇关于Canvas.clipPath(路径)不削波如预期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆