简体   繁体   中英

How to detect single tap on canvas drawings or bitmaps using onTouchListener?

The objective of this game is similar to that of a bubble popping game. Each time a bubble is touched, an interaction/event happens.

So far this is what i have. It's kinda messy since i am new at this, but my professor told me to use onTouch(view v, motionevent event) and in that method, i will use something like event.getSource(); for me to be able to create an interaction with methods serving the purpose of a touchable drawing/bitmap image.

Any advice or help would be great! Many thanks.

public class MainActivity extends Activity implements OnTouchListener {

OurView v;
Bitmap icon1, icon2, icon3;
float x,y;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    v = new OurView(this);
    v.setOnTouchListener(this);
    icon1 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    icon2 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    icon3 = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    x = y = 0;
    setContentView(v);
}

@Override
protected void onPause() {
    super.onPause();
    v.pause();
}

@Override
protected void onResume() {
    super.onResume();
    v.resume();
}


public class OurView extends SurfaceView implements Runnable {

    Thread t = null;
    SurfaceHolder holder;
    boolean isItOK = false;

    public OurView(Context context) {
        super(context);
        holder = getHolder();
    }

    @Override
    public void run() {
        while (isItOK == true){
            if (!holder.getSurface().isValid()){
                continue;
            }
            Canvas c = holder.lockCanvas();
            c.drawARGB(255, 100, 120, 10);
            c.drawBitmap(icon1, x=50, y=100, null);
            c.drawBitmap(icon2, x=180, y=100, null);
            c.drawBitmap(icon3, x=310, y=100, null);
            holder.unlockCanvasAndPost(c);
        }
    }

    public void pause() {
        isItOK = false;
        while(true){
            try{
                t.join();
            }catch (InterruptedException e){    
                e.printStackTrace();
                }
            break;
            }
        t = null;
    }

    public void resume(){
        isItOK = true;
        t = new Thread(this);
        t.start();
    }
}


@Override
public boolean onTouch(View v, MotionEvent me) {

    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    me.getSource();


    switch(me.getAction()){
    case MotionEvent.ACTION_DOWN:
        x = me.getX();
        y = me.getY();
        break;
    case MotionEvent.ACTION_UP:
        break;
    case MotionEvent.ACTION_MOVE:

        break;
    }
    return true;
}

}

尝试使init检查xy是否协调位图的矩形是否在循环内,是否在位图的矩形内?

You are drawing your bitmaps at known coordinates so why not just do a range check? Eg if x > 50 and x < 100 and y > 100 and y < 150 then they tapped icon 1 (assuming your bitmap is 50x50) A few seconds ago via mob

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM