Tint / dim drawable on touch

前端 未结 8 1476
别跟我提以往
别跟我提以往 2021-01-31 11:24

The app I\'m currently working on uses a lot of ImageViews as buttons. The graphics on these buttons use the alpha channel to fade out the edges of the button and make them look

8条回答
  •  日久生厌
    2021-01-31 12:07

    Your answer was also excellent ;)

    I am modifying little bit, it will give you result like this:

    button_normal button_pressed

    Rect rect;
    Drawable background;
    boolean hasTouchEventCompleted = false;
    
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_DOWN) {
            rect = new Rect(v.getLeft(), v.getTop(), v.getRight(),
                    v.getBottom());
            hasTouchEventCompleted = false;
            background = v.getBackground();
            background.setColorFilter(0x99c7c7c7,
                    android.graphics.PorterDuff.Mode.MULTIPLY);
            v.setBackgroundDrawable(background);
        } else if (event.getAction() == MotionEvent.ACTION_UP
                && !hasTouchEventCompleted) {
            background.setColorFilter(null);
            v.setBackgroundDrawable(background);
    
        } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
            if (!rect.contains(v.getLeft() + (int) event.getX(), v.getTop()
                    + (int) event.getY())) {
                // User moved outside bounds
                background.setColorFilter(null);
                v.setBackgroundDrawable(background);
                hasTouchEventCompleted = true;
            }
        }
    
    
        //Must return false, otherwise you need to handle Click events yourself.  
        return false;
    }
    

提交回复
热议问题