Animation when you touch the button

Asked

Viewed 1,218 times

3

I have a registration screen, where a standard photo is already set. If I press on top of the photo it opens 2 options to change the photo. Either grab by the gallery or take a photo.

By clicking on the button I would like to put an effect. Please, can someone give me a hint ?

Imagem sem botões Imagem com botões

1 answer

3

Below is a very basic and simple implementation possible. Remembering that to create an animation, depends a lot on creativity. So if you want to do amazing things, you’ll have to do a lot of research. In the code example below, a ImageView in which after the click will appear another two ImageButton in the sequence, which would be a btnFotoGaleria and another btnFotoCamera.

public class ActivityMain extends Activity{

    ImageView btnFotoPerfil;
    ImageButton btnFotoGaleria;
    ImageButton btnFotoCamera;

    // Animação
    Animation animFadein;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnFotoGaleria= (ImageButton) findViewById(R.id.btnFotoGaleria);
        btnFotoCamera= (ImageButton) findViewById(R.id.btnFotoCamera);

        // load the animation
        animFadein = AnimationUtils.loadAnimation(getApplicationContext(),
                R.anim.fade_in); 

        btnFotoPerfil= (ImageView) findViewById(R.id.btnFotoPerfil);
        // evento onclick no botao
        btnFotoPerfil.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                btnFotoGaleria.setVisibility(View.VISIBLE);
                btnFotoCamera.setVisibility(View.VISIBLE);

                // start the animation
                btnFotoGaleria.startAnimation(animFadein);
                btnFotoCamera.startAnimation(animFadein);
            }
        });       
    }
}

It is important to remember to configure in the parameters of ImageButton defining how android:visibility="gone" so that they remain invisible until the user’s click.

Within the res it is necessary to create a directory with name anim and a .xml for your animation, for example fade_in.xml with the following code below:

fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true" >

    <alpha
        android:duration="1000"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />

</set>

Details

Browser other questions tagged

You are not signed in. Login or sign up in order to post.