填充对象的相交区域 [英] Fill intersection area of objects
本文介绍了填充对象的相交区域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用Android Canvas填充矩形和圆形的相交区域,如下图所示:
I want to fill the intersection area of a rectangle and a circle using Android Canvas like in the image below:
我该如何实现?
更新: 这是我的代码
public static class MyView extends View {
private Paint paint;
public MyView(Context context) {
super(context);
init();
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public MyView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
init();
}
private void init() {
paint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setColor(Color.BLUE);
canvas.drawRect(getRect(), paint);
paint.setColor(Color.GRAY);
canvas.drawCircle(250,150, 100, paint);
}
public Rect getRect() {
return new Rect(100,100,400,200);
}
}
现在:
在这种情况下,通缉结果是这样的:
in this case, The Wanted result is this :
感谢您的帮助
推荐答案
您需要查看android.graphics.Path
类.
如果可以使用Path
定义形状,则可以使用canvas.drawPath()
绘制形状.
If you can define your shapes with Path
s, then you can draw them using canvas.drawPath()
.
Path
有一个称为op()
的方法,可用于获取两条路径的交点,如下所示:
Path
has a method called op()
that you can use to get the intersection of two paths, like this:
Path square = ...
Path circle = ...
Path intersect = circle.op(square, Op.INTERSECT);
在Paint.Style
为FILL
的情况下,您可以为两个形状的交点着色.
With a Paint.Style
of FILL
, you can color the intersection of the two shapes.
这篇关于填充对象的相交区域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文