如何在imageview中使画布响应触摸?

时间:2015-08-27 22:00:45

标签: android canvas imageview ontouch

我在ImageView中有一个画布。如何让它响应触摸?这里的最终目标是能够在画布内拖动对象。我想知道这是否可能。但就目前而言,我怎样才能让它对触摸做出反应?我有以下代码,当我在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;
}

}

0 个答案:

没有答案