Tint / dim drawable on touch

前端 未结 8 1471
别跟我提以往
别跟我提以往 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:05

    I too have irregular looking buttons and needed the tint. In the end, I just resorted to having a state colorlist as my ImageButton background. Gives the impression that the button color is changing on press and is very straightforward and less compute intensive. I know that this isn't exactly a tint, but it does give it does give necessary visual feedback to the user in what usually is a fleeting moment.

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
        <item
            android:state_pressed="true"
            android:drawable="@android:color/darker_gray" />
        <item android:drawable="@android:color/transparent" />
    </selector>
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
提交回复
热议问题