2016-11-10 138 views
0

我已經嘗試了幾種基於這裏的答案,如animation.setRepeatCount(Animation.INFINITE)在我的動畫循環;或者android:repeatcount =「infinite」在anim.xml文件中.even在java上嘗試了while(true)循環,但似乎沒有任何工作。Android繼續循環動畫

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:shareInterpolator="false"> 

<alpha 
    android:fromAlpha="0.0" 
    android:toAlpha="1.0" 
    android:duration="3000" /> 

<translate 
    android:fromXDelta="0.0" 
    android:toXDelta="0.0" 
    android:fromYDelta="0.0" 
    android:toYDelta="100.0" 
    android:startOffset="3000" 
    android:duration="3000" 
    android:repeatCount="infinite"/> 

</set> 

和我的Java是:

@Override 
protected void onCreate(Bundle savedInstanceState){ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    img = (ImageView) findViewById(R.id.img); 
    animation = AnimationUtils.loadAnimation(this, R.anim.halloweenanim); 

    img.startAnimation(animation); 
    animation.setRepeatCount(Animation.INFINITE); 

    //hallo.setBackgroundResource(R.drawable.halloweenimg); 
    //animation2 = (AnimationDrawable) hallo.getBackground(); 
    //animation2 = (AnimationDrawable) img.getBackground(); 
    //animation.start(); 
}; 
+0

我沒有得到什麼似乎沒有工作? –

+0

我的意思是連續循環。它只有一次動畫,然後沒有任何反應。我想保持它動畫.. – hpa

回答

0

其實AnimationSet有一個錯誤代碼。即使文檔here

repeatCount,fillEnabled:這些屬性爲 AnimationSet忽略。

你可以做的是去除animation.setRepeatCount(Animation.INFINITE); 並設置android:repeatCount="infinite"每個動畫的標籤,取而代之的父親集標籤。

0

能否請您試試這個,看看它是否能幫助你。

animation.addListener(new AnimatorListenerAdapter() { 

    @Override 
    public void onAnimationEnd(Animator animation) { 
     super.onAnimationEnd(animation); 
Animation animation = AnimationUtils.loadAnimation(this, R.anim.halloweenanim); 
animation.setAnimationListener(this); 
       img.startAnimation(animation); 
    } 

}); 
img.startAnimation(animation); 
0

沒關係。我找到了解決方案。 android:repeatCount =「infinite」應該放在alpha哈哈里面。並且不需要在java代碼中添加.setRepeatCount。 XD

+0

是的,你必須在他們每個人中分別添加屬性。 –