2014-09-11 126 views
0

我在Android中遇到動畫問題。在這個動畫的結尾和開始之間,在屏幕上閃爍整個圖像,沒有alpha效果。動畫開頭字母0,用α-0結束得我有這樣的代碼:Android動畫重複

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

<alpha 
    android:duration="4000" 
    android:fromAlpha="0.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="1.0" /> 

<scale 
    android:duration="12000" 
    android:fromXScale="0.5" 
    android:fromYScale="0.5" 
    android:pivotX="50%" 
    android:pivotY="10%" 
    android:toXScale="3" 
    android:toYScale="2" 
    android:interpolator="@android:anim/linear_interpolator"/> 

<alpha 
    android:startOffset="10000" 
    android:duration="1500" 
    android:fromAlpha="1.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="0" /> 
</set> 

這個代碼在java中

imageView.startAnimation(animace); 

animace.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { 
     imageView.setVisibility(View.VISIBLE); 
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     imageView.setVisibility(View.INVISIBLE); 
     imageView.startAnimation(animace); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
    } 
}); 
+0

你的標題是說一些關於**的run()**,但它不是在代碼中提到片斷你提供,請張貼完整代碼 – nobalG 2014-09-11 09:34:55

+0

你是否在調用'imageView.setVisibility(View.VISIBLE)'''之前嘗試調用'imageView.setAlpha(0f);'onAnimationStart''? – Stanislav 2014-09-11 09:35:41

+0

不,這不起作用,當設置alpha到imageview,然後imageview保持透明和動畫顯示什麼都沒有。 – 2014-09-11 09:51:14

回答

1

這是因爲XML文件中的動畫在同一拼命地跑時間。你同時擁有一個將Alpha從0改變爲1的動畫,以及一個從1改變爲0的動畫。我假設你想要在一個序列中運行它們,所以你應該把它們分成不同的文件。一個解決辦法是,像這樣:(未測試)

fade_in.xml

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

<alpha 
    android:duration="4000" 
    android:fromAlpha="0.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="1.0" /> 

</set> 

fade_out.xml

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

<alpha 
    android:startOffset="10000" 
    android:duration="1500" 
    android:fromAlpha="1.0" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:toAlpha="0" /> 

</set> 

scale.xml

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

<scale 
    android:duration="12000" 
    android:fromXScale="0.5" 
    android:fromYScale="0.5" 
    android:pivotX="50%" 
    android:pivotY="10%" 
    android:toXScale="3" 
    android:toYScale="2" 
    android:interpolator="@android:anim/linear_interpolator"/> 

</set> 

,然後在你的代碼,同時啓動fade_in和縮放,並在fade_in完成後運行fade_out:

imageView.startAnimation(fadeInAnimation); 
imageView.startAnimation(scaleAnimation); 

fadeInAmination.setAnimationListener(new Animation.AnimationListener() { 
    @Override 
    public void onAnimationStart(Animation animation) { } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     imageView.startAnimation(fadeOutAnimation); 
    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { } 
}); 

我不確定您需要在哪裏設置可見和不可見,但您可以爲每個對象附加一個AnimationListener,並根據您的需要放置它們。

希望這有助於:)

+0

我有第二個阿爾法開始設置10000,這個動畫是好的。現在,我通過@Stanislav找到了解決方案。我不知道爲什麼,但設置INVISIBLE或VISIBLE的可見性不工作(Android Studio不顯示任何錯誤),但是當我改變這個設置在animationEnd和1f在動畫開始時,現在是它的工作。 – 2014-09-11 10:16:33

+0

的確,我錯過了那一個。對不起:) – tudor 2014-09-11 10:59:42

0

這項工作:

animace.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 
      imageView.setAlpha(1f); 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      imageView.setAlpha(0f); 
      imageView.startAnimation(animace); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    });