绘制用户触摸屏幕的圆圈

时间:2012-01-19 00:55:54

标签: java android graphics

我绘制一个用户触摸图像视图的圆圈。它绘制圆圈很好,但它不是用户触摸的地方。它总是在左边很多。这是代码:

获得触摸:

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

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

尝试绘制

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

这会将图像的中心绘制到您触摸的位置

答案 1 :(得分:1)

您必须在布局中添加触摸侦听器而不是图像。将setOnTouchListener添加到布局中。无需触摸侦听器。

答案 2 :(得分:0)

查看Hello circle示例

相关问题