我在 ImageView 中有一个画布。我怎样才能让它响应触摸?这里的最终目标是能够在画布内拖动对象。我想知道这是否可能。但是现在,我怎样才能让它响应触摸呢?我有以下代码,当我在图像视图内单击时它没有做任何事情(尽管它确实响应了按钮)。我究竟做错了什么?
公共类 MainActivity 扩展 ActionBarActivity 实现 View.OnTouchListener{
ImageView ourFrame;
Button ourButton;
Bitmap ourBitmap = Bitmap.createBitmap(700,700, Bitmap.Config.ARGB_8888);
Canvas ourCanvas = new Canvas(ourBitmap);
Paint paint = new Paint();
Bitmap ball;
float x, y;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ourFrame = (ImageView) findViewById(R.id.iv);
ourButton = (Button) findViewById(R.id.bDrawLine);
ourCanvas.drawColor(Color.BLACK);
ourFrame.setImageBitmap(ourBitmap);
ball = BitmapFactory.decodeResource(getResources(), R.drawable.ball);
x = 0;
y = 0;
ourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
paint.setColor(Color.CYAN);
ourCanvas.drawLine(10, 50, 200, 50, paint);
ourFrame.setImageBitmap(ourBitmap);
}
});
}
@Override
public boolean onTouch(View v, MotionEvent me) {
x = me.getX();
y = me.getY();
ourCanvas.drawBitmap(ball, x - (ball.getWidth() / 2), y - (ball.getHeight() / 2), null);
ourFrame.setImageBitmap(ourBitmap);
return true;
}
}