MonoDroid-在运行时绘制椭圆

本文关键字:绘制 运行时 MonoDroid- | 更新日期: 2023-09-27 18:30:05

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

MonoDroid-在运行时绘制椭圆

要绘制椭圆或其他几何形状,可以使用画布对象。这里有一个非常基本的代码,可以绘制一个椭圆。我基本上只是创建了一个视图,并覆盖了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 });
    }
}