2012-09-24 11 views
3

내 앱에서 원을 그려야하고 사용자가 화면의 아무 곳으로나 드래그 할 수 있습니다.원을 그리며 이동/Android에서 터치로 드래그하는 방법

이렇게하려면 onDraw 방법으로 원을 그려야합니다. onTouchEvent을 사용하여 움직임을 추적하려고했습니다. 그러나 화면을 터치 할 때마다 새로운 서클을 만드는 대신 서클을 드래그하려고하면 서클이 실제로 움직이지 않습니다.

내가 누락 된 부분을 누군가 도와주세요.

여기 난 당신이 movingLine, movingEnd 및 movingStart 플래그와 함께 달성하기 위해 무엇을 의미하는지 전혀 모르겠어요 내 코드

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; 
} 


} 
+0

코드를 공유 하시겠습니까? –

답변

2

나는 잘 모르겠어요하지만 당신은 변수 아래 mannage 경우 다음이 문제 해결할 수 있습니다 :

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

부울 변수와 마우스의 움직임을 관리하세요.

0

이다; 그러나 movingEnd 및 movingStart 플래그는 절대로 true로 설정되지 않습니다. 결과적으로 해당 onTouch() 코드 블록에 도달하지 못합니다.

관련 문제