2012-01-19 110 views
1

我在用户触摸图像视图的地方绘制了一个圆圈。它可以很好地画圆,但不是用户触摸的地方。它总是在左边很多。下面是代码:在用户触摸屏幕的地方绘制圆圈

获取触摸:

image.setOnTouchListener(new OnTouchListener() { 

    public boolean onTouch(View arg0, MotionEvent arg1) { 
     System.out.println("Touch recieved at "+arg1.getX() + " " + arg1.getY()); 
     touchX = (arg1.getX()); 
     touchY = (arg1.getY()); 
     System.out.println("Touch recieved at "+touchX + " " + touchY); 
     image.setImageBitmap(createImage()); 

     return true; 
    } 
}); 

,并在图像上绘图:

public Bitmap createImage(){ 
     Bitmap image = bmp.copy(Bitmap.Config.RGB_565, true); 
     Canvas canvas = new Canvas(image); 
     Paint paint = new Paint(); 
     paint.setStyle(Paint.Style.STROKE); 
     paint.setColor(Color.GREEN); 
     canvas.drawCircle(touchX, touchY, radius, paint); 
     System.out.println("Drew a circle at "+touchX+" " + touchY+" with a radius of "+radius+"."); 
     return image; 
    } 

任何想法?

回答

1

尝试

touchX=touchX- image.getWidth()/2; 
touchY=touchY- image.getHeight()/2; 

此画将绘制图像的中心位置轻触

+0

嗯,使它更接近......但还是关闭?它就像你在屏幕上移动得越远,越远。 – 2012-01-19 03:08:34

1

你必须触摸听者不添加到布局的图像。将setOnTouchListener添加到布局。不需要图像的触摸监听器。

+0

试过这个,没有工作。 – 2012-01-19 03:33:30

+0

您是否删除了图像的触摸监听器?您的布局宽度和高度是否为填充父项?在onTouch方法中添加Action down条件。如果(arg1.getAction()== ActionEvent.ACTION_DOWN)。如果你想拖动使用ACTION_MOVE – 2012-01-19 04:20:58

+0

我用一种不同的方法解决了这个问题。谢谢。 – 2012-01-19 13:49:48