Detect touch on bitmap

前端 未结 3 1333
温柔的废话
温柔的废话 2020-11-29 06:03

Greets,

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

Thanks

相关标签:
3条回答
  • 2020-11-29 06:46

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

    0 讨论(0)
  • 2020-11-29 06:54

    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.

    0 讨论(0)
  • 2020-11-29 06:57

    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 讨论(0)
提交回复
热议问题