如何在Android中绘制圆圈并移动/拖动它

时间:2012-09-24 11:27:34

标签: android canvas view

在我的应用中,我需要绘制一个圆圈,用户可以将其拖动到屏幕上的任何位置。

为此,我创建了一个视图,用onDraw方法绘制圆圈。我尝试使用onTouchEvent跟踪动作。但是当我试图拖动它而不是在触摸屏幕的任何地方创建一个新的圆圈时,圆圈并没有真正移动。

请有人帮我解决这个问题。

这是我的代码

public class Circle extends View {

Paint paint, paintSmall;
private Point start = null;
private Point cursorAtMouseDown = null;
private Point startAtMouseDown = null;
private Point endAtMouseDown = null;
private boolean movingStart = false;
private boolean movingEnd = false;
private boolean movingLine = false;

public Circle(Context context) {
    super(context);
    init();
}

public Circle(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public Circle(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    paint = new Paint();
    start = new Point(100, 100);
    paint.setColor(Color.BLUE);
    paint.setStrokeWidth(10);
    paint.setStyle(Paint.Style.STROKE);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    paint.setStyle(Paint.Style.STROKE);
    canvas.drawCircle(start.x, start.y, 80, paint);

    canvas.drawCircle(start.x, start.y, 10, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    Log.d("Inside On Touch", "");
    switch (event.getAction()) {
    case MotionEvent.ACTION_UP:

        if (movingStart || movingEnd || movingLine) {
            invalidate();
        }

        movingStart = false;
        movingEnd = false;
        movingLine = false;
        break;
    case MotionEvent.ACTION_OUTSIDE:
        if (movingStart || movingEnd || movingLine) {
            invalidate();
        }

        movingStart = false;
        movingEnd = false;
        movingLine = false;
        break;
    case MotionEvent.ACTION_MOVE:

        Log.d("Inside On Touch", "ACTION_MOVE");

        if (movingStart) {
            start.x = (int) event.getX();
            start.y = (int) event.getY();
            invalidate();
            Log.d("Inside On Touch", "--movingStart=" + movingStart);
            return true;
        } else if (movingEnd) {
            start.x = (int) event.getX();
            start.y = (int) event.getY();
            invalidate();
            Log.d("Inside On Touch", "--movingEnd=" + movingEnd);
            return true;
        } else if (movingLine) {
            Log.d("Inside On Touch", "--movingLine=" + movingLine);
            if (cursorAtMouseDown != null) {
                // double diffX = event.getX() - cursorAtMouseDown.x;
                // double diffY = event.getY() - cursorAtMouseDown.y;
                // start.x = (int) (startAtMouseDown.x + diffX);
                // start.y = (int) (startAtMouseDown.y + diffY);

                start = cursorAtMouseDown;

                invalidate();
                return true;
            }

        }
        return false;

    case MotionEvent.ACTION_DOWN:
        cursorAtMouseDown = new Point((int) event.getX(),
                (int) event.getY());

        if (cursorAtMouseDown.equals(start)) {

        }

        if (isCircleCenterChaged(cursorAtMouseDown)) {
            movingLine = true;
        }

        return true;

    default:
        return super.onTouchEvent(event);

    }
    return false;
}


}

2 个答案:

答案 0 :(得分:2)

我不确定,但如果你在变量下方管理,那么它可以解决你的问题:

private Point cursorAtMouseDown = null; 
private Point startAtMouseDown = null; 
private Point endAtMouseDown = null; 
private boolean movingStart = false; 
private boolean movingEnd = false; 
private boolean movingLine = false; 

请管理布尔变量和鼠标移动。

答案 1 :(得分:0)

我不完全确定你使用movingLine,movingEnd和movingStart标志完成什么意思;但是看起来moveEnd和movingStart标志永远不会设置为true。因此,永远不会到达相应的onTouch()代码块。