2017-03-14 24 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; 
     } 
    }