无法从列表中绘制点
Can't draw points from List
我正在使用 Xamarin.Android,我想从 List
中绘制圆圈,该圆圈由 TouchEvent
填充。
这是我的代码:
public class Ceiling : View
{
private List<PointF> _points;
private Canvas _canvas;
private Paint paint;
protected Ceiling(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public Ceiling(Context context) : base(context)
{
_points = new List<PointF>();
paint = new Paint
{
Color = Color.Red
};
paint.SetStyle(Paint.Style.Fill);
Touch += OnTouch;
}
public Ceiling(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
if (_canvas == null)
{
_canvas = canvas;
Background = new PaintDrawable(Color.AliceBlue);
canvas.SetViewport(Width, Height);
}
base.OnDraw(canvas);
if (_points.Count > 0)
{
foreach (var point in _points)
canvas.DrawCircle(point.X, point.Y, 10, paint);
}
}
private void OnTouch(object sender, TouchEventArgs e)
{
switch (e.Event.Action)
{
case MotionEventActions.Down:
{
var point = new PointF(e.Event.GetX(), e.Event.GetY());
if (!_points.Contains(point))
{
_points.Add(point);
_canvas.DrawCircle(point.X, point.Y, 10, paint);
}
break;
}
}
}
}
只有在_points
已经定义的情况下才有效。我怎样才能使这项工作?
您有多个构造函数,其中只有一个构造函数初始化私有变量。您确定调用的是构造函数吗(我假设这是从布局初始化的)?
在任何情况下,您最好将初始化移动到一个单独的方法中,这样您就可以从所有构造函数中调用它:
protected Ceiling(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
// need to call init here as well?
}
public Ceiling(Context context) : base(context)
{
Init();
}
public Ceiling(Context context, IAttributeSet attrs) : base(context, attrs)
{
Init();
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
Init();
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
Init();
}
private void Init()
{
_points = new List<PointF>();
this.paint = new Paint
{
Color = Color.Red
};
this.paint.SetStyle(Paint.Style.Fill);
Touch += OnTouch;
}
我正在使用 Xamarin.Android,我想从 List
中绘制圆圈,该圆圈由 TouchEvent
填充。
这是我的代码:
public class Ceiling : View
{
private List<PointF> _points;
private Canvas _canvas;
private Paint paint;
protected Ceiling(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
}
public Ceiling(Context context) : base(context)
{
_points = new List<PointF>();
paint = new Paint
{
Color = Color.Red
};
paint.SetStyle(Paint.Style.Fill);
Touch += OnTouch;
}
public Ceiling(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
}
protected override void OnDraw(Canvas canvas)
{
base.OnDraw(canvas);
if (_canvas == null)
{
_canvas = canvas;
Background = new PaintDrawable(Color.AliceBlue);
canvas.SetViewport(Width, Height);
}
base.OnDraw(canvas);
if (_points.Count > 0)
{
foreach (var point in _points)
canvas.DrawCircle(point.X, point.Y, 10, paint);
}
}
private void OnTouch(object sender, TouchEventArgs e)
{
switch (e.Event.Action)
{
case MotionEventActions.Down:
{
var point = new PointF(e.Event.GetX(), e.Event.GetY());
if (!_points.Contains(point))
{
_points.Add(point);
_canvas.DrawCircle(point.X, point.Y, 10, paint);
}
break;
}
}
}
}
只有在_points
已经定义的情况下才有效。我怎样才能使这项工作?
您有多个构造函数,其中只有一个构造函数初始化私有变量。您确定调用的是构造函数吗(我假设这是从布局初始化的)?
在任何情况下,您最好将初始化移动到一个单独的方法中,这样您就可以从所有构造函数中调用它:
protected Ceiling(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
{
// need to call init here as well?
}
public Ceiling(Context context) : base(context)
{
Init();
}
public Ceiling(Context context, IAttributeSet attrs) : base(context, attrs)
{
Init();
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr) : base(context, attrs, defStyleAttr)
{
Init();
}
public Ceiling(Context context, IAttributeSet attrs, int defStyleAttr, int defStyleRes) : base(context, attrs, defStyleAttr, defStyleRes)
{
Init();
}
private void Init()
{
_points = new List<PointF>();
this.paint = new Paint
{
Color = Color.Red
};
this.paint.SetStyle(Paint.Style.Fill);
Touch += OnTouch;
}