2012-04-03 148 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 }); 
    } 
}