2017-10-09 113 views
2

我想在另一個橢圓形狀內繪製一個橢圓形狀,但第二個應該在到達第一個橢圓形的邊界時切斷。Android:在另一個形狀內繪製形狀

這是期望的結果:

如何才能實現這一目標?

+2

看到\t'android.graphics.PorterDuffXfermode' ,更多的是:http://ssp.impulsetrain.com/porterduff.html – pskink

回答

1

我想繪製一個橢圓形的橢圓形在另一個橢圓形狀,但第二個應該被切斷,當它到達第一個的邊界。

正如pskink說,你可以使用PorterDuffXfermode來實現此功能,這裏是一個簡單的:

public class DrawView : View 
{ 
    public DrawView(Context context):base(context) 
    { 
    } 

    protected override void OnDraw(Canvas canvas) 
    { 
     base.OnDraw(canvas); 

     Paint paint = new Paint(); 
     paint.SetARGB(255, 255, 0, 0); 
     RectF oval2 = new RectF(60, 100, 300, 200); 
     canvas.DrawOval(oval2, paint); 

     //PorterDuff.Mode.SrcAtop means Discards the source pixels that do not cover destination pixels. Draws remaining source pixels over destination pixels 
     paint.SetXfermode(new PorterDuffXfermode(PorterDuff.Mode.SrcAtop)); 

     paint.Color = Color.Black; 
     RectF oval3 = new RectF(110, 150, 350, 250); 
     canvas.DrawOval(oval3, paint); 

     this.SetLayerType(LayerType.Software, null); 
     paint.SetXfermode(null); 
    } 
} 

效果:

enter image description here

+0

它的工作原理!謝謝! –