我正在制作一个响应触摸事件的简单绘图应用。实际的"绘画"发生在自定义视图中,内部具有所有内部逻辑。完成此操作后,我想在操作栏中添加Clear
按钮,但是在确定如何将自定义视图链接到菜单时遇到了一些困难。
public class MyView extends View {
private Paint paint;
ArrayList<TouchPosition> touchedPositions = new ArrayList();
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.MAGENTA);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
touchedPositions.add(new TouchPosition(event.getX(), event.getY()));
invalidate();
return true;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for(Iterator<TouchPosition> touchedList = touchedPositions.iterator(); touchedList.hasNext(); ) {
TouchPosition eachTouch = touchedList.next();
System.out.println(eachTouch.xPosition);
canvas.drawCircle(eachTouch.xPosition, eachTouch.yPosition, 10, paint);
}
}
}