开发者

Detect touch on bitmap

开发者 https://www.devze.com 2022-12-22 19:24 出处:网络
Gree开发者_如何学Gots, Does anyone how I go about detecting when a user presses on a bitmap which is inside a canvas?

Gree开发者_如何学Gots,

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

Thanks


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.


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 :-)


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

精彩评论

暂无评论...
验证码 换一张
取 消