3

I'm using Android's Animation Drawable to show an animation. However I want it to be as snappy as possible, because I want it to show whenever the screen is touched. The way it is now it is too slow. It takes around 500 ms to show up. Is there a way that I can preload the animation or maybe some alternative?

Here is the code:

public void setYes(){
    ImageView prev_view = (ImageView) ref_container.getChildAt(ref_container.getChildCount()-2);
    prev_view.setImageResource(R.drawable.accept);
    acceptAnimation = (AnimationDrawable) prev_view.getDrawable();
    acceptAnimation.start();
}

And here it is the XML inside drawable folder containing the animation frames:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/accept01" android:duration="33" />
<item android:drawable="@drawable/accept02" android:duration="33" />
<item android:drawable="@drawable/accept03" android:duration="33" />
<item android:drawable="@drawable/accept04" android:duration="33" />
<item android:drawable="@drawable/accept05" android:duration="33" />
<item android:drawable="@drawable/accept06" android:duration="33" />
<item android:drawable="@drawable/accept07" android:duration="33" />
<item android:drawable="@drawable/accept08" android:duration="33" />
<item android:drawable="@drawable/accept09" android:duration="33" />
<item android:drawable="@drawable/accept10" android:duration="33" />
<item android:drawable="@drawable/accept11" android:duration="33" />
<item android:drawable="@drawable/accept12" android:duration="33" />
<item android:drawable="@drawable/accept13" android:duration="33" />
<item android:drawable="@drawable/accept14" android:duration="33" />
<item android:drawable="@drawable/accept15" android:duration="33" />
<item android:drawable="@drawable/accept16" android:duration="33" />
</animation-list>

I call this function when I detect a certain touch pattern. I think I can't load it before because I'm using it in different views ref_container.getChildCount()-2. Do you guys have any solution, suggestion to speed it up?

Joao Dias
  • 83
  • 10
  • 1
    How are you setting your drawable? Could you show us some of your code? Remember that you can set the drawable before you touch your screen and just call drawable.start to start the animation. This way the drawable will be already preloaded but will not animate. – Pedro Oliveira Nov 06 '15 at 11:06
  • I updated the question. You can give a look at the code now. I would set the drawable before but I'm using it dynamically using different views every time I call the function. – Joao Dias Nov 06 '15 at 11:16
  • Can't you just simply set the drawable at onActivityCreated (or similar) in your view and don't call drawable.start? Then on your setYes just call start and the animation will start. Or does the view have another drawable before that you need to replace? – Pedro Oliveira Nov 06 '15 at 11:19
  • I'm afraid I can't. The view that will have the animation will always be different and it is defined by the number of childs in a container, `ref_container.getChildCount()-2` – Joao Dias Nov 06 '15 at 11:29
  • Your call to setImageResource is probably what is taking time loading it from xml, cant see why you cant instantiate it before, just use Drawable.createFromXml() – Nanoc Nov 06 '15 at 11:41
  • You can load the image in `onStart()` method, here is the reference, I suggest you take a look at the 2nd code fence in the doc. https://developer.android.com/guide/topics/graphics/drawable-animation.html –  Apr 14 '18 at 09:11

1 Answers1

0

It's true, there's a delay in the animation showing up the first time it's shown. The solution is to assign the image within onCreate() to avoid the delay.

Here's the code

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.animated_view) AppCompatImageView animatedView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.your_activity);
        ButterKnife.bind(this);

        // assign animation
        animatedView.setBackgroundResource(R.drawable.your_xml_list_of_images);
    }

    @OnClick(R.id.yolo)
    public void onClickYolo() {
        animatedView.setVisibility(View.VISIBLE);
        ((AnimationDrawable) animatedView.getBackground()).start();
    }

}
Someone Somewhere
  • 23,475
  • 11
  • 118
  • 166