C#Draw不能绘图

时间:2016-11-30 20:18:47

标签: c# android

我正试着用眼睛跟随你的手指制作应用程序。 但是,当我加载我的应用程序时,图形不会出现。 它加载紫色背景很好,但似乎停在那里。 谁能告诉我为什么会这样?

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Graphics;
using Android.Views;
using Android.Content;
using Android.Content.PM;
using System;


namespace BewegendeOgen
{
[Activity(Label = "BewegendeOgen", MainLauncher = true, Icon = "@drawable/icon", ScreenOrientation = ScreenOrientation.Landscape)]
public class MainActivity : Activity
{
    OgenView ogen;


    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        ogen = new OgenView(this);


        this.SetContentView(ogen);
    }
}

public class OgenView : View

{
    public OgenView(Context c) : base(c)
    { this.Touch += RaakAan;
      this.SetBackgroundColor(Color.Purple);

    }

    PointF punt;
    public void RaakAan(object o, TouchEventArgs tea)
    {
     float xas = tea.Event.GetX();
     float yas = tea.Event.GetY();
        this.punt = new PointF(xas, yas);
        this.Invalidate();

        this.Invalidate();
    }


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

        this.tekenoog(canvas, 200);
        this.tekenoog(canvas, -200);


    }

    public void tekenoog(Canvas canvas, int a)
    {    //this is the drawing
        base.OnDraw(canvas);
        float radius, x1, x2, y;
        radius = 200;
        x1 = this.Width / 3;
        y = this.Height / 2;
        x2 = x1 * 2;

        Paint verf;
        verf = new Paint();

        verf.Color = Color.White;
        canvas.DrawCircle(x1 + a, y, radius, verf);


        float dx, g, e, f, d, ex, ey, radiusiris, irisx1, irisy1;
        dx = punt.X - x1;
        g = punt.Y - y;
        radiusiris = 20;
        e = radius - radiusiris;
        f = (float)Math.Sqrt(dx * dx + g * g);
        d = f - e;
        ex = dx * e / d;
        ey = (float)Math.Sqrt(e * e - ex * ex);
        irisx1 = x1 + ex;
        irisy1 = y - ey;

        verf.Color = Color.CornflowerBlue;
        canvas.DrawCircle(irisx1, irisy1, radiusiris, verf);
    }
}
}

1 个答案:

答案 0 :(得分:0)

您从未初始化punt,因此当它首次尝试在tekenoog中访问时会抛出异常。

我在我的机器上尝试了这个,并且在我将punt声明修改为:

之后它运行了
private PointF punt = new PointF();

希望这有帮助!

enter image description here