2014-02-22 63 views
1

我想對我的圖像做一個多動畫(出現 - >旋轉 - >消失)。我有這樣的代碼:動畫出現,旋轉和消失

fade_in.xml

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

<alpha 
    android:duration="1" 
    android:fromAlpha="0" 
    android:toAlpha="100" /> 

</set> 

fade_out.xml

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

<alpha 
    android:duration="1" 
    android:fromAlpha="100" 
    android:toAlpha="0" /> 

</set> 

image_rotate.xml

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

<rotate 
    android:duration="2500" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:toDegrees="120" /> 

</set> 
在我的Java代碼

另外:

animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate); 
animRotate.setDuration((long) duration); 
fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in); 
fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out); 

AnimationSet s = new AnimationSet(false); 
s.addAnimation(fade_in); 
s.addAnimation(animRotate); 
s.addAnimation(fade_out); 

image.startAnimation(s); 

但不幸的是它不能正常工作...

+0

不correcrly工作? – pskink

回答

3

你有你的動畫XML文件severals錯誤:

  • 時間屬性是毫秒,因此1ms的是太短了顯着淡入/淡出動畫
  • alpha屬性是0和1之間的浮點數,100的方式太多了。
  • 你不需要在XML文件中的一組,如果只有一個動畫:只需添加字母或旋轉標籤作爲根

所以,你現在應該有這些文件:

fade_in.xml

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1000" 
    android:fromAlpha="0" 
    android:toAlpha="1" /> 

fade_out.xml

<?xml version="1.0" encoding="utf-8"?> 
<alpha xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="1000" 
    android:fromAlpha="1" 
    android:toAlpha="0" /> 

image_rotate.xm l

<?xml version="1.0" encoding="utf-8"?> 
<rotate xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="2500" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:toDegrees="120" /> 

然後,在您的代碼中,您需要在每個動畫之間添加一個偏移量。否則,所有動畫將同時觸發。此外,fillAfter標誌必須根動畫對象上設置(在這裏,你AnimationSet

Animation animRotate= AnimationUtils.loadAnimation(context, R.anim.image_rotate); 
Animation fade_in = AnimationUtils.loadAnimation(context, R.anim.fade_in); 
Animation fade_out = AnimationUtils.loadAnimation(context, R.anim.fade_out); 

AnimationSet s = new AnimationSet(false); 
s.addAnimation(fade_in); 

animRotate.setDuration((long) duration); 
animRotate.setStartOffset(fade_in.getDuration()); 
s.addAnimation(animRotate); 

fade_out.setStartOffset(fade_in.getDuration() + animRotate.getDuration()); 
s.addAnimation(fade_out); 

s.setFillAfter(true); 

image.startAnimation(s);