2

我需要使用旋轉功能進行圖像視圖。所以我看着Android開發者site。並使用他們的代碼。但不知何故,我得到一個錯誤。ImageView在Android中的旋轉

錯誤:java.lang.ClassCastException: android.graphics.drawable.StateListDrawable cannot be cast to android.graphics.drawable.AnimationDrawable

我有這些代碼:

ImageView refresh = (ImageView)findViewById(R.id.refresh_devices_button); 
refresh.setBackgroundResource(R.drawable.spin_animation); // The IDE says that it may produce null pointer exception 
AnimationDrawable frameAnimation = (AnimationDrawable) refresh.getBackground(); 
frameAnimation.start(); 

在spin_animation.xml:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
<animation-list android:id="@+id/selected" android:oneshot="false"> 
    <item android:drawable="@drawable/scan_1" android:duration="50" /> 
    <item android:drawable="@drawable/scan_2" android:duration="50" /> 
    <item android:drawable="@drawable/scan_3" android:duration="50" /> 
    <item android:drawable="@drawable/scan_4" android:duration="50" /> 
    <item android:drawable="@drawable/scan_5" android:duration="50" /> 
</animation-list> 
</selector> 

請幫助我。從android的網站我得到的代碼,但他們的代碼不起作用。也許問題出在我的spin_animation.xml文件中。

refresh.getBackground返回StateListDrawable我想。

回答

0

我發現android網站沒問題。問題出在我的xml文件上。 我有這個代碼

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <animation-list android:id="@+id/selected" android:oneshot="false"> 
     <item android:drawable="@drawable/scan_1" android:duration="50" /> 
     <item android:drawable="@drawable/scan_2" android:duration="50" /> 
     <item android:drawable="@drawable/scan_3" android:duration="50" /> 
     <item android:drawable="@drawable/scan_4" android:duration="50" /> 
     <item android:drawable="@drawable/scan_5" android:duration="50" /> 
    </animation-list> 
</selector> 

而不是我用下面的代碼上面的代碼。我不得不刪除<selector>標籤。

<?xml version="1.0" encoding="utf-8"?> 
<animation-list 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/selected" android:oneshot="false"> 
    <item android:drawable="@drawable/scan_1" android:duration="50" /> 
    <item android:drawable="@drawable/scan_2" android:duration="50" /> 
    <item android:drawable="@drawable/scan_3" android:duration="50" /> 
    <item android:drawable="@drawable/scan_4" android:duration="50" /> 
    <item android:drawable="@drawable/scan_5" android:duration="50" /> 
</animation-list> 
0

旋轉動畫,以imageview的

`RotateAnimation anim = new RotateAnimation(0f, 350f, 15f, 15f); 
    anim.setInterpolator(new LinearInterpolator()); 
    anim.setRepeatCount(Animation.INFINITE); 
    anim.setDuration(700); 

    // Start animating the image 
    final ImageView splash = (ImageView) findViewById(R.id.splash); 
    splash.startAnimation(anim); 

    // Later.. stop the animation 
splash.setAnimation(null);` 
+0

對不起,我編輯了我的答案.. – KrishnaJ

0

我有我的ImageView的旋轉動畫。您可以使用此代碼嘗試: 首先創建您的旋轉動畫XML在動畫目錄: rotation_animation.xml:

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

後,在您的Java類(活動或片段)補充一點:

Animation rotationAnimation = AnimationUtils.loadAnimation(this, R.anim.rotation_animation); 
yourImageView.startAnimation(rotationAnimation); 

現在你已經準備好了。快樂作弄:)

+0

我沒有anim目錄。我應該直接添加到res文件夾下或應用程序文件夾下。 – gunescelil

+0

你需要創建一個新文件夾,右鍵單擊res文件夾,創建新文件夾命名爲anim。你可以把你的動畫xmls放進去。 –

1

您可以使用類似下面的代碼和進口下面這個類

 ImageView splash = (ImageView) view.findViewById(R.id.imageView); 

     RotateAnimation anim = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
     anim.setInterpolator(new LinearInterpolator()); 
     anim.setRepeatCount(Animation.INFINITE); 
     anim.setDuration(500); 

     // Start animating the image 

     splash.startAnimation(anim); 
0

使用爲好。

import android.graphics.drawable.AnimationDrawable; 



private AnimationDrawable mAnimation; 
private ImageView mAnimLogo; 

mAnimLogo = (ImageView) findViewById(R.id.loading_image); 
mAnimation = (AnimationDrawable) mAnimLogo.getDrawable(); 


mAnimation.start(); 
0

這一行:

AnimationDrawable frameAnimation = (AnimationDrawable) refresh.getBackground(); 

你想投一個StateListDrawable到引發異常的AnimationDrawable。

尼斯和簡單:

refresh.animate().rotation(360).setDuration(...); 

順時針旋轉視圖360度的...毫秒。

或者

旋轉BY 360度視圖

檢查出ViewPropertyAnimator您可以在一行中編碼的各種動畫。

+0

我的持續時間未知。我正在掃描一些東西,所以它必須繼續,直到完成搜索。 – gunescelil