How to put a visual effect when clicking on an ImageButton on Android

3

I would like to get the same effect as a "Button" by clicking on an "ImageButton" thanks in advance.

    
asked by MrCode 02.08.2016 в 01:16
source

2 answers

1

It can be achieved by having images that simulate the effect of the button when it is selected and when it is not, these images are configured in a selector that your ImageButton would use:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"   
        android:drawable="@drawable/img_boton_seleccionado" />
    <item android:state_selected="false"   
        android:drawable="@drawable/img_boton_no_seleccionado" />
</selector>

something similar to this image:

    
answered by 02.08.2016 в 02:00
0

You can use this animation to simulate a click on an image:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha = "1.0"
        android:toAlpha = "0.5"
        android:duration = "300">
    </alpha>
    <scale
        android:fromXScale = "1"
        android:toXScale = "0.9"
        android:fromYScale = "1"
        android:toYScale = "0.9"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration = "50">
    </scale>
</set>

In the onClick event loads the animation to be able to visualize the effect:

public void onClick(View view) {
    view.startAnimation(AnimationUtils.loadAnimation(Context, R.anim.imagen_click));

}
    
answered by 05.10.2017 в 14:37