마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

paint action move가 먹히지 않습니다.

0 추천
    private Paint paint;

   private float m_x = 100;
   private float m_y = 100;

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

      paint = new Paint();
      paint.setColor(Color.BLUE);
      paint.setStrokeWidth(10);
      paint.setStyle(Paint.Style.STROKE);
   }

   protected void onDraw(Canvas canvas) {
      super.onDraw(canvas);

      canvas.drawRect(m_x-50, m_y-50, m_x+50, m_y+50, paint);

   }

   @Override
   public boolean onTouchEvent(MotionEvent event) {

      Toast.makeText(super.getContext(), "MotionEvent : " + event.getAction() + " / " + event.getX() + ", " + event.getY(), Toast.LENGTH_SHORT).show();

      if (event.getAction() == MotionEvent.ACTION_DOWN) {
         Toast.makeText(super.getContext(), "MotionEvent.ACTION_DOWN : " + event.getX() + ", " + event.getY(), Toast.LENGTH_LONG).show();
         m_x = event.getX();
         m_y = event.getY();

      }

      else if(event.getAction() == MotionEvent.ACTION_MOVE) {
         Toast.makeText(super.getContext(), "ACTION_MOVE: " + event.getX() + ", " + event.getY(), Toast.LENGTH_LONG).show();
         m_x = event.getX();
         m_y = event.getY();
      }

      return super.onTouchEvent(event);
   }

}
위에서 다른 액션들은 실행 되는데 move 는 되지 않습니다. 어떻게 바꿔야 할까요
joy178 (200 포인트) 님이 2016년 9월 26일 질문

1개의 답변

0 추천

view관련 작업한지 너무 오래되서 잘 기억나지는 않지만...

move 이벤트 도중에 자동으로 onDraw가 콜백 안되지 않나요?

move 이벤트 탐지로 인한 x, y값을 갱신 하셨으면, onDraw가 콜백될 수 있도록 invalidate 호출해 주어야 할듯 한데요.

 

Drawing

Drawing is handled by walking the tree and recording the drawing commands of any View that needs to update. After this, the drawing commands of the entire tree are issued to screen, clipped to the newly damaged area.

The tree is largely recorded and drawn in order, with parents drawn before (i.e., behind) their children, with siblings drawn in the order they appear in the tree. If you set a background drawable for a View, then the View will draw it before calling back to its onDraw() method. The child drawing order can be overridden with custom child drawing order in a ViewGroup, and with setZ(float) custom Z values} set on Views.

To force a view to draw, call invalidate().

칠리님 (10,910 포인트) 님이 2016년 9월 26일 답변
매 move 이벤트가 16ms(1000ms/60f) 보다 너무 작은 값이라면 invalidate() 호출이 화면 그리기 작업에 무리를 줄 수 있습니다.
move 이벤트 탐지에서는 x, y 값만 업데이트 하시고, 실제로 그리기를 호출하는 스레드는 다른 스레드에서 16ms 마다 호출해주는게 좋을듯 합니다.
물론 실제 invalidate 메소드를 호출하는 곳은 main thread여야 합니다.
다른 스레드에서는 16ms 마다 트리거만 발동 시켜야 겠죠.

AsyncTask 을 통하면 publishProgress를 호출해서 onProgressUpdate가 콜백되도록 하여 처리할 수 있습니다. onProgressUpdate는 메인스레드에서 호출됩니다.
...