Random animation on android

1

Good I'm trying to make an animation using an XML and animation-list, I get everything right but the problem I have is that I want the order of the animation to be always different every time it starts it's activity

<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android= "http://schemas.android.com/apk/res/android"
android:oneshot= "false">
<item android:drawable="@drawable/dice1"
    android:duration="200" />
<item android:drawable="@drawable/dice2"
    android:duration="200" />
<item android:drawable="@drawable/dice3"
    android:duration="200" />
<item android:drawable="@drawable/dice4"
    android:duration="200" />
<item android:drawable="@drawable/dice5"
    android:duration="200" />
<item android:drawable="@drawable/dice6"
    android:duration="200" />
</animation-list>

That would be the xml that I use, only the 6 images I use, and then I'll start the animation in the activity

private AnimationDrawable animacion;
private AnimationDrawable animacion2;
 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pantalla_partida);


    dado = (ImageView) findViewById(R.id.Dado);
    dado2 = (ImageView) findViewById(R.id.Dado2);

    animacion = (AnimationDrawable) ContextCompat.getDrawable(this,R.drawable.animacion);
    animacion2 = (AnimationDrawable) ContextCompat.getDrawable(this,R.drawable.animacion);
    dado.setImageDrawable(animacion);
    dado2.setImageDrawable(animacion2);

This would be the entire code, if possible also want to know if it is possible to know what current frame has the animation.

    
asked by Alejandro Moreno 08.08.2018 в 02:45
source

1 answer

0

Hello, you can use this example.

You create an xml with your arrays.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<integer-array name="imagenesArray">
    <item>@drawable/uno</item>
    <item>@drawable/dos</item>
    <item>@drawable/tres</item>
</integer-array>
</resources>

Then in the main you do the following:

   private TypedArray miArray;
   private int numeroAleatorio;
   private ImageView imagen;
   private Drawable drawable;


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

    imagen = (ImageView) findViewById(R.id.imagen);

    //Le digo cual es el array donde tiene que buscar.
    miArray = getResources().obtainTypedArray(R.array.imagenesArray);

    //Creamos una instancia de la clase Random.
    final Random random = new Random();

    //Usamos la clase math para encontrar un numero aleatorio, de nuestro    array.
    numeroAleatorio = (int) (Math.random() * miArray.length());

    //Obtengo el objeto drawable.
    drawable = miArray.getDrawable(numeroAleatorio);

    //Finalmente asignamos la imagen o el recurso.
    imagen.setBackground(drawable);

    //Mostramos el numero de la imagen en un toat.
    Toast toast = Toast.makeText(this, "La imagen es la : " + numeroAleatorio, Toast.LENGTH_SHORT);
    toast.show();
 }
}

If you want that every time you touch the image, make a change, add an onClickListener to your image.

   imagen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            cambiaimagen();
        }
    });
}

And inside copies this part, if you do this do not forget to give an initial image to the image on the onCreate or you will not see anything.

private void cambiaimagen() {
    //Usamos la clase math para encontrar un numero aleactorio, de nuestro array.
    numeroAleatorio = (int) (Math.random() * miArray.length());

    //Obtengo el objeto drawable.
    drawable = miArray.getDrawable(numeroAleatorio);

    //Finalmente asignamos la imagen o el recurso.
    imagen.setBackground(drawable);

    //Mostramos el numero de la imagen en un toat.
    Toast toast = Toast.makeText(this, "La imagen es la : " + numeroAleatorio, Toast.LENGTH_SHORT);
    toast.show();
    }

Thanks and regards I hope to help you.

    
answered by 13.08.2018 / 11:59
source