Joao Dias
Joao Dias

Reputation: 93

Can I preload an Animation Drawable?

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?

Upvotes: 3

Views: 815

Answers (1)

Someone Somewhere
Someone Somewhere

Reputation: 23797

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();
    }

}

Upvotes: 0

Related Questions