Doubt with animations on Android

1

The first doubt is how to stop the animation once it's done, right now it's a loop and I want it to only do the animation and stop once:

    ImageView img_check_guia3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guia_parte3);
        img_check_guia3 = (ImageView) findViewById(R.id.img_check_guia3);

        AnimationDrawable animacion = (AnimationDrawable)img_check_guia3.getDrawable();
        animacion.start();

}

And the other question is, how can I do so that my application does not slow down when I open the class with the animation?

animacion

<animation-list   android:id="@+id/handimation" android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/check_01" android:duration="50" />
    <item android:drawable="@drawable/check_02" android:duration="50" />
    <item android:drawable="@drawable/check_03" android:duration="50" />
    <item android:drawable="@drawable/check_04" android:duration="50" />
    <item android:drawable="@drawable/check_05" android:duration="50" />
    <item android:drawable="@drawable/check_06" android:duration="50" />
    <item android:drawable="@drawable/check_07" android:duration="50" />
    <item android:drawable="@drawable/check_08" android:duration="50" />
    <item android:drawable="@drawable/check_09" android:duration="50" />
    <item android:drawable="@drawable/check_10" android:duration="50" />
    <item android:drawable="@drawable/check_11" android:duration="50" />
    <item android:drawable="@drawable/check_12" android:duration="50" />
    <item android:drawable="@drawable/check_13" android:duration="50" />
    <item android:drawable="@drawable/check_14" android:duration="50" />
    <item android:drawable="@drawable/check_15" android:duration="50" />
    <item android:drawable="@drawable/check_16" android:duration="50" />
    <item android:drawable="@drawable/check_17" android:duration="50" />
    <item android:drawable="@drawable/check_18" android:duration="50" />
    <item android:drawable="@drawable/check_19" android:duration="50" />
    <item android:drawable="@drawable/check_20" android:duration="50" />
    <item android:drawable="@drawable/check_21" android:duration="50" />
    <item android:drawable="@drawable/check_22" android:duration="50" />
    <item android:drawable="@drawable/check_23" android:duration="50" />
    <item android:drawable="@drawable/check_24" android:duration="50" />
    <item android:drawable="@drawable/check_25" android:duration="50" />
    <item android:drawable="@drawable/check_26" android:duration="50" />
    <item android:drawable="@drawable/check_27" android:duration="50" />
    <item android:drawable="@drawable/check_28" android:duration="50" />
    <item android:drawable="@drawable/check_29" android:duration="50" />
    <item android:drawable="@drawable/check_30" android:duration="50" />
    <item android:drawable="@drawable/check_31" android:duration="50" />
    <item android:drawable="@drawable/check_32" android:duration="50" />
    <item android:drawable="@drawable/check_33" android:duration="50" />
    <item android:drawable="@drawable/check_34" android:duration="50" />
    <item android:drawable="@drawable/check_35" android:duration="50" />
    <item android:drawable="@drawable/check_36" android:duration="50" />
    <item android:drawable="@drawable/check_37" android:duration="50" />
    <item android:drawable="@drawable/check_38" android:duration="50" />
    <item android:drawable="@drawable/check_39" android:duration="50" />
    <item android:drawable="@drawable/check_40" android:duration="50" />
    <item android:drawable="@drawable/check_41" android:duration="50" />
    <item android:drawable="@drawable/check_42" android:duration="50" />
    <item android:drawable="@drawable/check_43" android:duration="50" />
    <item android:drawable="@drawable/check_44" android:duration="50" />
    <item android:drawable="@drawable/check_45" android:duration="50" />
    <item android:drawable="@drawable/check_46" android:duration="50" />
    <item android:drawable="@drawable/check_47" android:duration="50" />
</animation-list>

Obviously if I use less images it becomes more fluid, but I need them to be 47 to have a full animation.

  • They are 47 .png
  • 700x700
  • Each png about 16kb
  • In total all images do not reach 1mb

What could I do to keep it from slowing down? I do not think that with 3GB of RAM and 801 Snapdragon can not with that animation.

Thanks

EDITO3: @MarcGV

clase

import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

import otrointento.dos.R;

public class GuiaParte3 extends AppCompatActivity {

    ImageView img_check_guia3;

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

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

        AnimationDrawable animDrawable = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            animDrawable = (AnimationDrawable) getDrawable(R.drawable.movimiento_check_guia3);
        }

        img_check_guia3.setBackgroundDrawable(animDrawable);

        animDrawable.setOneShot(true);
        animDrawable.start();

    }
}
    
asked by UserNameYo 08.03.2017 в 00:59
source

1 answer

1

Regarding how to stop the animation:

  

The first doubt is how to stop the animation once it's done, now   same is a loop and I want you to just do the animation once and stop:

animacion.setOneShot(true);

Concerning

  

And the other question is, how can I do it so that it does not slow down my   application when opening the class with the animation?

To solve this in a case that I found, I put the first frame of the animation in the imageview to show the first frame while loading the animation, then the animation is loaded in background doInBackground() with AsynkTask and one once loaded the first frame is changed by the entire animation in onPostExecute()

Example:

In the ImageView src put the first frame:

<ImageView
     android:id="@+id/grid_alert"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:src="@drawable/check_01"/>

Then you create a AsynkTask where you load the animation and when it's loaded, you put the animation at ImageView

AsyncTask task = new AsyncTask<Void, Void, AnimationDrawable>() {

    @Override
    protected AnimationDrawable doInBackground(Void... params) {
        return ContextCompat.getDrawable(R.drawable.animacion);
    }

    @Override
    protected void onPostExecute(AnimationDrawable animationDrawable) {

        img_check_guia3.setAnimation(animationDrawable);
        animationDrawable.setOneShot(true);
        animationDrawable.start();

        super.onPostExecute(animationDrawable);
     }
};
task.execute();

You can also avoid the AsynkTask and put the animation directly. In this way you will see the first frame and once loaded you will see the animation, although all the work will be done in the main thread.

EDIT

You must use the appropriate getDrawable() method, the ContextCompat method is in case you use AppCompatActivity. For the animation, change the method setAnimation() by setBackgroundDrawable()

//AnimationDrawable animDrawable = ContextCompat.getDrawable(R.drawable.animacion);
AnimationDrawable animDrawable = getDrawable(R.drawable.animacion);

img_check_guia3.setBackgroundDrawable(animDrawable);
animationDrawable.setOneShot(true);
animationDrawable.start();
    
answered by 08.03.2017 в 10:20