Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spinning wheel in Android

Tags:

android

How can I spin a image wheel in an activity on android with the help of touch event? I need some guideline or link of any tutorial.

like image 616
Shahjalal Avatar asked Feb 26 '26 04:02

Shahjalal


1 Answers

This is typically done with a couple pieces. This is how I do it in one of my apps. *Note: This is not a smooth wheel, so much as it starts and stops at the top (which was intentional). You can lookup more about Animation on the dev site.

Main XML that has an image:

<ImageView
    android:id="@+id/anim_example"
    android:src="@drawable/loading_circle"
    android:layout_width="30sp"
    android:layout_height="30sp"
    android:onClick="RunAnimation" />

Here are the parts in code that run the animation

public void RunAnimation(View v)
{
    //The onClick method has to be present and must take the above parameter.
    StartLoading();

    //This will delay the stop for 5 seconds
    //Normally you would want to actually have this run based on some other input/data.
    Handler handler = new Handler(); 
    handler.postDelayed(new Runnable() { 
        public void run() {
          StopLoading(); 
        } 
    }, 5000); 
}

public void StartLoading() {
    ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
    refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
    Animation rotateLoading = AnimationUtils.loadAnimation(this, R.anim.rotate);
    refreshImage.clearAnimation();
    refreshImage.setAnimation(rotateLoading);
}

public void StopLoading() {
    ImageView refreshImage = (ImageView) this.findViewById(R.id.anim_example);
    if (refreshImage.getAnimation() != null)
    {
        refreshImage.clearAnimation();
        refreshImage.setImageDrawable(getResources().getDrawable(R.drawable.loading_circle));
    }
}

anim.rotate:

<?xml version="1.0" encoding="utf-8"?>
<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="359"
    android:duration="2000"
    android:repeatMode="restart"
    android:repeatCount="-1"
    android:pivotX="50%"
    android:pivotY="50%">
</rotate>
like image 177
gtcompscientist Avatar answered Feb 27 '26 17:02

gtcompscientist



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!