2010-09-21 164 views
28

我想創建一個旋轉的進度圖像,並想知道最佳的處理方式。我可以使它與動畫列表一起工作,例如每100ms更改12張圖像。這工作得很好,但它是相當繁瑣的創建12個圖片或每大小和分辨率:旋轉圖像。動畫列表還是動畫旋轉? (Android)

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> 
<item android:drawable="@drawable/ic_loading_grey_on_black_01" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_02" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_03" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_04" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_05" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_06" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_07" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_08" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_09" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_10" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_11" android:duration="100" /> 
<item android:drawable="@drawable/ic_loading_grey_on_black_12" android:duration="100" /> 

我想,一個更簡單的解決方案是每分辨率使用一個圖像,而是將其旋轉爲每幀。在平臺資源(android-sdk-windows/platforms ...)中,我在文件drawable/search_spinner.xml中發現了一個名爲動畫旋轉的東西,但是如果我複製代碼,請獲取編譯器錯誤,抱怨android:framesCount和android: frameDuration(谷歌的API 2.2在Eclipse):

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:drawable="@drawable/spinner_black_20" 
android:pivotX="50%" 
android:pivotY="50%" 
android:framesCount="12" 
android:frameDuration="100" /> 

我已經使用重複旋轉動畫(使用動畫資源文件夾)也嘗試過,但其實我更喜歡的動畫列表版本的外觀。

解決此問題的建議方法是什麼?

回答

13

你必須創建一個可繪製的XML文件,如下圖所示:

代碼:

<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android" 
android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" 
android:toDegrees="360" android:drawable="@drawable/imagefile_to_rotate" /> 
+2

它怎麼不旋轉? – 2014-06-19 16:43:18

+0

它不適用於某些設備。請使用rotage代替 – vuhung3990 2015-07-22 09:07:15

2

在這裏看到的例子 http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/view/index.html

明確: 進度條

  1. 增量 演示大小旋轉進度指示器,可以單位遞增或遞減。
  2. 平滑 演示大型和小型連續旋轉進度指示器,用於指示通用「繁忙」消息。
  3. 對話框 演示ProgressDialog,一個託管進度條的彈出對話框。這個例子演示了確定和不確定的進度指標。
  4. 標題欄 通過設置WindowPolicy的進度指示器功能演示一個帶有進度指示器的活動屏幕。
0

SACPK的解決方案絕對有效。另一種解決方案可以像使用問題那樣使用<animated-rotate>,並且爲編譯器所抱怨的那些人刪除android:framesCount="12" android:frameDuration="100"屬性。它甚至可以用於我的8幅圖像。

但是,我沒有帶想通了如何控制動畫:(速度。

55

Rotate drawable由普利文認爲不會給你幀數的控制。讓我們假設你想實現一個自定義的加載它由8個部分:

gif_icon

使用animation-list方法,你需要創建通過手動45*frameNumber度旋轉8幀。另外,您也可以使用第一幀,將旋轉動畫吧:

progress_icon

文件res/anim/progress_anim.xml

<?xml version="1.0" encoding="utf-8"?> 
<rotate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromDegrees="0" 
    android:toDegrees="360" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:repeatCount="infinite" /> 

文件MainActivity.java

Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.progress_anim); 
a.setDuration(1000); 
imageView.startAnimation(a); 

這將給你流暢的動畫,而不是8階梯。爲了解決這個問題,我們需要實現自定義插值:

a.setInterpolator(new Interpolator() { 
    private final int frameCount = 8; 

    @Override 
    public float getInterpolation(float input) { 
     return (float)Math.floor(input*frameCount)/frameCount; 
    } 
}); 

您也可以創建一個自定義窗口小部件:

文件res/values/attrs.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <declare-styleable name="ProgressView"> 
     <attr name="frameCount" format="integer"/> 
     <attr name="duration" format="integer" /> 
    </declare-styleable> 
</resources> 

文件ProgressView.java

public class ProgressView extends ImageView { 

    public ProgressView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     setAnimation(attrs); 
    } 

    public ProgressView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     setAnimation(attrs); 
    } 

    public ProgressView(Context context) { 
     super(context); 
    } 

    private void setAnimation(AttributeSet attrs) { 
     TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ProgressView); 
     int frameCount = a.getInt(R.styleable.ProgressView_frameCount, 12); 
     int duration = a.getInt(R.styleable.ProgressView_duration, 1000); 
     a.recycle(); 

     setAnimation(frameCount, duration); 
    } 

    public void setAnimation(final int frameCount, final int duration) { 
     Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.progress_anim); 
     a.setDuration(duration); 
     a.setInterpolator(new Interpolator() { 

      @Override 
      public float getInterpolation(float input) { 
       return (float)Math.floor(input*frameCount)/frameCount; 
      } 
     }); 
     startAnimation(a); 
    } 
} 

文件activity_main.xml

<com.example.widget.ProgressView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_progress" 
    app:frameCount="8" 
    app:duration="1000"/> 

文件res/anim/progress_anim.xml:以上

+0

太棒了,非常感謝你 – 2013-02-25 14:27:27

+3

「謝謝!」,「我也是!」, – Cephron 2014-04-02 21:46:20

+0

我無法通過使用android:visibility =「invisible」隱藏progressview嗎? – Prateek 2014-06-09 11:04:57

6

我發現vokilam的答案是創建一個不錯的階梯/交錯動畫最好的一家上市。我去了他的最終建議,並做了一個自定義小部件,我遇到的唯一問題是設置可見性將無法工作,因爲它是動畫,因此將始終可見...

我調整了他的代碼(ProgressView.java我改名StaggeredProgress.java)是這樣的:

public class StaggeredProgress extends ImageView { 

private Animation staggered; 

public StaggeredProgress(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    setAnimation(attrs); 
} 

public StaggeredProgress(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    setAnimation(attrs); 
} 

public StaggeredProgress(Context context) { 
    super(context); 
} 

private void setAnimation(AttributeSet attrs) { 
    TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.StaggeredProgress); 
    int frameCount = a.getInt(R.styleable.StaggeredProgress_frameCount, 12); 
    int duration = a.getInt(R.styleable.StaggeredProgress_duration, 1000); 
    a.recycle(); 

    setAnimation(frameCount, duration); 
} 

public void setAnimation(final int frameCount, final int duration) { 
    Animation a = AnimationUtils.loadAnimation(getContext(), R.anim.progress_anim); 
    a.setDuration(duration); 
    a.setInterpolator(new Interpolator() { 

     @Override 
     public float getInterpolation(float input) { 
      return (float)Math.floor(input*frameCount)/frameCount; 
     } 
    }); 
    staggered = a; 
    //startAnimation(a); 
} 

@Override 
public void setVisibility(int visibility) { 
    super.setVisibility(visibility); 
    if(visibility == View.VISIBLE) 
     startAnimation(staggered); 
    else 
     clearAnimation(); 

} 


} 

這種方式設置視圖的可視性啓動和停止動畫嘀......再次感謝給vokilam!