How to make button with custom background image show click animation in Android

前端 未结 5 1462
無奈伤痛
無奈伤痛 2020-12-05 16:27

How to make button show it is clicked (by setting it go down/some change) for buttons using custom background image in Android. I do not want to include more images and set

5条回答
  •  暖寄归人
    2020-12-05 16:56

    If you want the default button click effect, you can put your background drawable inside a `ripple' like below.

    The example will produce a button with a border, transparent background and the button default click animation like this

    create res/drawable/image_btn_border.xml

    
    
        
            
                
                    
                    
                
            
        
        
            
                
                    
                    
                
            
        
    
    

    add to res/values/dimens.xml

    
        4dp
        5dp
    
    

    set the drawable as the background of an imagebutton

    
    

    Details

    1. The first item which is marked with android:id="@android:id/mask" is used by ripple to transition color from @android:color/background_light to ?attr/colorControlHighlight when the button is pressed.
    2. The second item is the actual background we want for button in normal (unpressed) state.
    3. android:inset* sets background margins.

提交回复
热议问题