2017-03-14 25 views
0

我是Android開發中的新手,我在這裏有個問題,因爲我還沒有在Stackoverflow上找到任何答案。Android - 真正的繪圖和手指運動之間的距離

我已經寫了一個簡單的代碼,讓用戶用屏幕上的一個手指畫一個邊界框(從左上角到右下角)。

類drawView函數看起來是這樣的:

public class DrawView extends View { 
Paint paint = new Paint(); 

public DrawView(Context context) { 
    super(context); 
} 
public Point startPoint = new Point(); 
public Point endPoint = new Point(); 

@Override 
public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 

    paint.setColor(Color.RED); 
    paint.setStrokeWidth(3); 
    paint.setStyle(Paint.Style.STROKE); 
    canvas.drawRect(startPoint.x,startPoint.y,endPoint.x,endPoint.y,paint); 
}} 

,並在MainActivity看起來是這樣的:

public class MainActivity extends AppCompatActivity { 

DrawView drawView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    drawView = new DrawView(this); 
    drawView.setBackgroundColor(Color.WHITE); 
    drawView.startPoint.set(0,0); 
    drawView.endPoint.set(0,0); 
    setContentView(drawView); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    int eventAction = event.getAction(); 

    // you may need the x/y location 
    int x = (int)event.getX(); 
    int y = (int)event.getY(); 

    // put your code in here to handle the event 
    switch (eventAction) { 
     case MotionEvent.ACTION_DOWN: 
      drawView.startPoint.set(x,y); 
      break; 
     case MotionEvent.ACTION_UP: 
      drawView.endPoint.set(x,y); 
      drawView.invalidate(); 
      break; 
     case MotionEvent.ACTION_MOVE: 
      drawView.endPoint.set(x,y); 
      drawView.invalidate(); 
      break; 
    } 

    // tell the View that we handled the event 
    return true; 
}} 

,結果是我的手指點在實際繪圖點之間的顯著距離屏幕如下所示:

on screen result

藍線是我的手指,紅色邊界框是屏幕上的真實繪圖。有誰知道這個的原因?非常感謝!

+0

這是因爲'View'的座標不同於'Activity'。在「視圖」本身處理觸摸事件。也就是說,將'onTouchEvent()'方法移動到'DrawView'。 –

回答

0

活動&子視圖沒有相同的座標原點。我做了一些更正:

public class MainActivity extends AppCompatActivity implements View.OnTouchListener{ 

     DrawView drawView;   

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 


      drawView = new DrawView(this); 
      drawView.setBackgroundColor(Color.WHITE); 
      drawView.startPoint.set(0, 0); 
      drawView.endPoint.set(0, 0); 
      setContentView(drawView); 
      drawView.setOnTouchListener(this); 
     } 



     @Override 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
      int eventAction = motionEvent.getAction(); 


      // you may need the x/y location 
      int x = (int) motionEvent.getX(); 
      int y = (int) motionEvent.getY(); 

      // put your code in here to handle the event 
      switch (eventAction) { 
       case MotionEvent.ACTION_DOWN: 
        drawView.startPoint.set(x, y); 
        break; 
       case MotionEvent.ACTION_UP: 
        drawView.endPoint.set(x, y); 
        drawView.invalidate(); 
        break; 
       case MotionEvent.ACTION_MOVE: 
        drawView.endPoint.set(x, y); 
        drawView.invalidate(); 
        break; 
      } 

      // tell the View that we handled the event 
      return true; 
     } 
    }