onClickListener在画布画布、onClickListener

2023-09-05 10:21:21 作者:你的长相影响了我的健康

即时通讯开发一个应用程序,它具有上选择特定的图像作为索引图像该活动将开始,但我不知道如何设置onClickListener或onTouchListener在画布我的继承人code

Im developing an application which having Images as a Index on selection of particular image that activity will begin but I dont know how to set onClickListener or onTouchListener in Canvas heres my code

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

请,如果有人知道如何添加onClickListener为特定的图像,例如在这里,如果我点击书,然后bookActivity将启动。

Please if anyone knows how to add onClickListener for particular image e.g. here if I click on Book then bookActivity will start.

推荐答案

尝试是这样的:

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;
}
 
精彩推荐
图片推荐