开发者

Spinning wheel in Android

开发者 https://www.devze.com 2023-03-26 09:51 出处:网络
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.This is typically done with a couple pieces.This is

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.


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>
0

精彩评论

暂无评论...
验证码 换一张
取 消