Canvas 中的 onClickListener

Abh*_*nde 2 android android-canvas

我正在开发一个应用程序,该应用程序以图像作为索引来选择活动将开始的特定图像,但我不知道如何在 Canvas 中设置 onClickListener 或 onTouchListener 这是我的代码

public class DrawView extends View implements OnTouchListener {

LinearLayout mLayout;
Bitmap index;
Bitmap book;
Bitmap bird;
Bitmap game;
Bitmap mail;
Bitmap music;
Bitmap torch;
Paint paint;

public DrawView(Context context) {
    super(context);

    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    index = BitmapFactory.decodeResource(getResources(), R.drawable.photo1);
    book = BitmapFactory.decodeResource(getResources(), R.drawable.book);
    game = BitmapFactory.decodeResource(getResources(), R.drawable.game);
    music = BitmapFactory.decodeResource(getResources(), R.drawable.music);
}

public void onDraw(Canvas canvas){
    paint = new Paint();
    Bitmap indexcanvas = Bitmap.createScaledBitmap(index, canvas.getWidth(),
                                                   canvas.getHeight(), true);
    canvas.drawBitmap(indexcanvas, 0, 0, paint);
    canvas.drawBitmap(book, 160, 100, paint);
    canvas.drawBitmap(game, 30, 10, paint);
    canvas.drawBitmap(music, 80, 50, paint);
} 

public boolean onTouch(View v, MotionEvent event) {
    return false;
}
Run Code Online (Sandbox Code Playgroud)

如果有人知道如何为特定图像添加 onClickListener,例如,如果我单击 Book,则 bookActivity 将启动。

Vla*_*mir 5

尝试这样的事情:

public boolean onTouch(View v, MotionEvent event) {
   if((event.getX(0)>=160) && 
      (event.getY(0)>=100) && 
     ( event.getX(0)<=160+BOOK_IMG_WIDTH) && 
      (event.getY(0)<=100+BOOK_IMG_HEIGHT))
      {
          //book selected
      }
   return true;
}
Run Code Online (Sandbox Code Playgroud)