Muhammad Ahmed AbuTalib
Muhammad Ahmed AbuTalib

Reputation: 4292

Spin animation in android

I am trying to spin an imageview , and I believe that the way I am doing is rather ugly . I am using a recursive method , to scale to max from min , when the min has been consequently reached.

Is there a better way to perform the spin ?

 private void spinLogo(boolean scaleUp)
{

    if(scaleUp) {
        mLogo.animate().scaleX(1f).setInterpolator(new OvershootInterpolator()).setDuration(200).withEndAction(new Runnable() {
            @Override
            public void run() {
                spinLogo(false);
            }
        });
    }
    else
    {
        mMolLogo.animate().scaleX(0f).setInterpolator(new OvershootInterpolator()).setDuration(1000).withEndAction(new Runnable() {
            @Override
            public void run() {
                spinLogo(true);
            }
        });
    }
}

The termination of this animation is not of my concern at the moment . This animation would run for maximum 5 seconds. This is actually attached to a launcher activity . And as soon as the sync webservice returns , i switch on to the main activity by destroying the launcher activity.

SO yes . any better ways to achieve the spin ?

Upvotes: 0

Views: 1241

Answers (1)

Outofdate
Outofdate

Reputation: 695

Create an xml file in anim resource folder.

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="0"
    android:fromYScale="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:toXScale="1.0"
    android:toYScale="1.0" />

Than in your activity or wherever you want create field Animation scaling

Initialize it with scaling = AnimationUtils.loadAnimation(getContext(), R.anim.scaling);

Set animation with view.startAnimation(scaling);. To stop animation call view.clearAnimation();

Upvotes: 2

Related Questions