2011-08-08 92 views
3

如何在觸摸事件的幫助下在Android上的活動中旋轉圖像輪?我需要一些指南或任何教程的鏈接。Android中的紡紗輪

+0

你需要證明它作爲動畫還是進步? – Hanry

+0

檢查http://stackoverflow.com/questions/2140023/android-hourglass – Fortega

回答

3

這通常是用幾件完成的。這是我在我的一個應用程序中做到的。 *注意:這不是一個光滑的輪子,而是從頂部開始和停止(這是故意的)。您可以在dev site上查找更多關於Animation的信息。

具有圖像主要XML:

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

下面是代碼,運行動畫部分

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>