当用户双击图像时,我构建了一个在屏幕上绘制圆圈的应用。我希望圆圈出现在用户点击屏幕的精确x和y坐标处。现在,它会在屏幕的右上角绘制图像。我是否必须实现onTouchEvent,我该怎么做?
以下是On Touch Listener :(触摸是图像视图)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_display);
Intent intent = getIntent();
String path = getIntent().getStringExtra(ImageDisplayActivity.KEY_PATH);
try {
java.io.FileInputStream in = this.openFileInput(path);
bitmap = BitmapFactory.decodeStream(in);
bitmap = bitmap.copy(bitmap.getConfig(), true);
touch = (ZoomInZoomOut)findViewById(R.id.IMAGEID);
touch = arrangeImageView(touch);
touch.setImageBitmap(bitmap);
in.close();
touch.setOnTouchListener(new View.OnTouchListener() {
GestureDetector gestureDetector = new
GestureDetector(new MyGestureDetector(context));
@Override
public boolean onTouch(View v, MotionEvent event) {
return gestureDetector.onTouchEvent(event);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
这是应该绘制圆圈的类
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
public Context context;
public MyGestureDetector(Context con)
{
this.context=con;
}
@Override
public boolean onDoubleTap(MotionEvent event) {
Bitmap bmOverlay = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(),
bitmap.getConfig());
Canvas canvas = new Canvas(bmOverlay);
Paint p = new Paint();
p.setAntiAlias(true);
p.setColor(Color.RED);
p.setStrokeWidth(2);
p.setStyle(Paint.Style.STROKE);
canvas.drawBitmap(bitmap,new Matrix(),null);
canvas.drawCircle(event.getX(),event.getY(),
100, p);
touch.setImageBitmap(bmOverlay);
return false;
}
}