Animate images in a ListView

4

I want to animate (rotate) an image that I put in a ListView when the user clicks on a certain item in the List. How do I get the image and apply the animation when I click on the list?

public class ListaConImag extends AppCompatActivity {

miAdaptador adaptador;
ArrayList<ContenidoVista> datos;
android.widget.ListView lista;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_con_imag);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    lista = (android.widget.ListView)findViewById(R.id.Lista_imag);
    datos = new ArrayList<ContenidoVista>();
    rellenardatos();
    adaptador = new miAdaptador(this,datos);

    lista.setAdapter(adaptador);
    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            switch (position){
                case(0):



                    break;


                case(1):
                    break;


                case(2):
                    break;


                case(3):
                    break;
            }

        }
    });


}

private void rellenardatos(){

    datos.add(new ContenidoVista("Titulo1","SubTitulo1",R.drawable.perro));
    datos.add(new ContenidoVista("Titulo2","SubTitulo2",R.drawable.perro1));
    datos.add(new ContenidoVista("Titulo3","SubTitulo3",R.drawable.perro2));
    datos.add(new ContenidoVista("Titulo4","SubTitulo4",R.drawable.persona));
  }

}

layout rotate

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:toDegrees="-360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"
android:startOffset="0"
/>
    
asked by Jorge Gonzalez 05.04.2016 в 16:43
source

1 answer

3

Save your animation within res/anim/

You upload the animation using the AnimationUtils class:

Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.mi_animacion);

and you assign it to the view:

view.startAnimation(myRotation);

with that you will turn your view when clicking:

   @Override
   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            @Override
            public void onItemClick(AdapterView<?> parent, final View view,
                                    int position, long id) {

                Animation myRotation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.mi_animacion);
                view.startAnimation(myRotation);
...
...
...

This is an example of the animation when you click on the views:

    
answered by 06.04.2016 / 06:10
source