Home » Android » android – Detect touch on bitmap

android – Detect touch on bitmap

Posted by: admin May 14, 2020 Leave a comment

Questions:

Greets,

Does anyone how I go about detecting when a user presses on a bitmap which is inside a canvas?

Thanks

How to&Answers:

You should work with something like so:

public boolean onTouchEvent(MotionEvent event){
    int action = event.getAction();
    int x = event.getX()  // or getRawX();
    int y = event.getY();

    switch(action){
    case MotionEvent.ACTION_DOWN:
        if (x >= xOfYourBitmap && x < (xOfYourBitmap + yourBitmap.getWidth())
                && y >= yOfYourBitmap && y < (yOfYourBitmap + yourBitmap.getHeight())) {
            //tada, if this is true, you've started your click inside your bitmap
        }
        break;
    }
}

That’s a start, but you need to handle case MotionEvent.ACTION_MOVE and case MotionEvent.ACTION_UP to make sure you properly deal with the user input. The onTouchEvent method gets called every time the user: puts a finger down, moves a finger already on the screen or lifts a finger. Each time the event carries the X and Y coordinates of where the finger is. For example if you want to check for someone tapping inside your bitmap, you should do something like set a boolean that the touch started inside the bitmap on ACTION_DOWN, and then check on ACTION_UP that you’re still inside the bitmap.

Answer:

Steve,
Google has a great article and tutorial for handling UI Events @ http://developer.android.com/guide/topics/ui/ui-events.html. This is what got me started and it was great for me 🙂

Answer:

This will detect a touch and check if it is not transparent. You need this if your bitmaps are not rectangles. myBitmap is just a simple container class I use.

private boolean clickOnBitmap(MyBitmap myBitmap, MotionEvent event) {
    float xEnd = myBitmap.originX() + myBitmap.width();
    float yEnd = myBitmap.originY() + myBitmap.height();;


    if ((event.getX() >= myBitmap.originX() && event.getX() <= xEnd) 
    && (event.getY() >= myBitmap.originY() && event.getY() <= yEnd) ) {
        int pixX = (int) (event.getX() - myBitmap.originX());
        int pixY = (int) (event.getY() - myBitmap.originY());
        if (!(myBitmap.getBitmap().getPixel(pixX, pixY) == 0)) {
            return true;
        } else {
            return false;
        }
    }
    return false;
}

This is the on touch code for completeness

    @Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (clickOnBitmap(dog, event)) {
                Toast.makeText(getContext(), "dog", Toast.LENGTH_SHORT).show();
            } else if (clickOnBitmap(mouse,event)) {
                Toast.makeText(getContext(), "mouse", Toast.LENGTH_SHORT).show();
            }
        return true;
        case MotionEvent.ACTION_OUTSIDE:
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            return true;
    }
    return false;
}