MonoDroid - 在运行时绘制椭圆

时间:2012-04-03 18:25:32

标签: c# android xamarin.android

我是MonoDroid的新手。如何在Android应用程序中使用C#在运行时绘制椭圆?

1 个答案:

答案 0 :(得分: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 });
    }
}