2012-04-03 123 views
0

我是MonoDroid的新手。如何在Android應用程序中使用C#在運行時繪製橢圓?MonoDroid - 在運行時繪製橢圓

+0

你是什麼意思?你想在自定義控件/曲面上畫一個橢圓嗎?或者你想在某處佈局某個部分添加一個橢圓? – Matthew 2012-04-04 08:54:00

+0

我的意思是在Android Sdk中有.net類的Shape類嗎?或者我必須使用算法繪製橢圓? – 2012-04-05 04:28:09

回答

2

要繪製橢圓或其他幾何形狀,可以使用畫布對象。這是一個非常基本的代碼,將繪製一個橢圓(橢圓形)。我基本上只是創建了一個視圖,並覆蓋了OnDraw方法來繪製橢圓。您定義了一個RectF對象,它定義了橢圓的矩形邊界。一個很好的參考是Android SDK:

http://developer.android.com/reference/android/graphics/Canvas.html

[Activity(Label = "MonoAndroidApplication1", MainLauncher = true, Icon = "@drawable/icon")] 
public class Activity1 : Activity 
{ 
    protected override void OnCreate(Bundle bundle) 
    { 
     base.OnCreate(bundle); 
     var targetView = new OvalView(this); 
     SetContentView(targetView); 
    } 
} 

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

    protected override void OnDraw(Canvas canvas) 
    { 
     RectF rect = new RectF(0,0, 300, 300); 
     canvas.DrawOval(rect, new Paint() { Color = Color.CornflowerBlue }); 
    } 
}