用手指移动绘制圆圈

时间:2018-04-15 15:44:03

标签: java android

我希望实现在我的android程序中移动已创建的onDraw个圈子的功能。这是我的view,我希望能够通过屏幕触摸移动每个圆圈。下面是我当前的 CustomView 类,用于绘制圆圈和网格。我知道我需要在这个类中添加一个onTouch监听器,但是在添加监听器之后我对如何处理它感到困惑。

public class CustomView extends View {

    // private  fields  that    are necessary   for rendering   the view
    private boolean touches[]; //   which   fingers providing   input
    private float touchx[]; //  x   position    of  each    touch
    private float touchy[]; //  y   position    of  each    touch
    private int first; //   the first   touch   to  be  rendered
    private boolean touch; //   do  we  have    at  least   on

    private static final int DEFAULT_PAINT_COLOR = Color.WHITE;
    private static final int DEFAULT_NUMBER_OF_ROWS = 8;
    private static final int DEFAULT_NUMBER_OF_COLUMNS = 8;

    private int numRows = DEFAULT_NUMBER_OF_ROWS, numColumns = DEFAULT_NUMBER_OF_COLUMNS;

    public CustomView(Context context) {
        super(context);

    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public CustomView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

    }

    public boolean onTouchEvent(MotionEvent event)  {

        return super.onTouchEvent(event);
    }

    @Override
    protected void onDraw(Canvas canvas) {


    super.onDraw(canvas);
    Paint redPaint = new Paint();
    redPaint.setColor(Color.RED);
    Paint blackPaint = new Paint();
    blackPaint.setColor(Color.BLACK);
    redPaint.setStrokeWidth(10);

    int width = getMeasuredWidth();
    int height = getMeasuredHeight();

    canvas.drawLine(0, 0, getMeasuredWidth(), 0, redPaint);
    canvas.drawLine(getMeasuredWidth(), 0, getMeasuredWidth(), getMeasuredHeight(), redPaint);
    canvas.drawLine(getMeasuredWidth(), getMeasuredHeight(), 0, getMeasuredHeight(), redPaint);
    canvas.drawLine(0, getMeasuredHeight(), 0, 0, redPaint);


    for (int i = 1; i < numColumns; i++) {
        canvas.drawLine(width * i / numColumns, 0, width * i / numColumns, height, redPaint);
    }

    // Horizontal lines
    for (int i = 1; i < numRows; i++) {
        canvas.drawLine(0, height * i / numRows, width, height * i / numRows, redPaint);
    }


    canvas.drawCircle(60, 220, 40, blackPaint);
    canvas.drawCircle(190, 220, 40, blackPaint);
    canvas.drawCircle(320, 220, 40, blackPaint);
    canvas.drawCircle(450, 220, 40, blackPaint);
    canvas.drawCircle(580, 220, 40, blackPaint);
    canvas.drawCircle(710, 220, 40, blackPaint);
    canvas.drawCircle(840, 220, 40, blackPaint);
    canvas.drawCircle(970, 220, 40, blackPaint);


    canvas.drawCircle(60, 70, 40, blackPaint);
    canvas.drawCircle(190, 70, 40, blackPaint);
    canvas.drawCircle(320, 70, 40, blackPaint);
    canvas.drawCircle(450, 70, 40, blackPaint);
    canvas.drawCircle(580, 70, 40, blackPaint);
    canvas.drawCircle(710, 70, 40, blackPaint);
    canvas.drawCircle(840, 70, 40, blackPaint);
    canvas.drawCircle(970, 70, 40, blackPaint);

    canvas.drawCircle(60, 980, 40, blackPaint);
    canvas.drawCircle(190, 980, 40, blackPaint);
    canvas.drawCircle(320, 980, 40, blackPaint);
    canvas.drawCircle(450, 980, 40, blackPaint);
    canvas.drawCircle(580, 980, 40, blackPaint);
    canvas.drawCircle(710, 980, 40, blackPaint);
    canvas.drawCircle(840, 980, 40, blackPaint);
    canvas.drawCircle(970, 980, 40, blackPaint);

    canvas.drawCircle(60, 1130, 40, blackPaint);
    canvas.drawCircle(190, 1130, 40, blackPaint);
    canvas.drawCircle(320, 1130, 40, blackPaint);
    canvas.drawCircle(450, 1130, 40, blackPaint);
    canvas.drawCircle(580, 1130, 40, blackPaint);
    canvas.drawCircle(710, 1130, 40, blackPaint);
    canvas.drawCircle(840, 1130, 40, blackPaint);
    canvas.drawCircle(970, 1130, 40, blackPaint);

  }
}

0 个答案:

没有答案