2016-01-23 30 views
1

我正在嘗試創建一個類似於instagram雙擊動畫的動畫,其中心漸暗時從中心向上縮放,然後保持可見狀態,然後縮小到中心而淡出。Instagram的心動畫模仿 - FadeScale動畫

我用這個動畫:

public void animateHeart(final ImageView view) { 
AnimationSet animation = new AnimationSet(true); 
animation.addAnimation(new AlphaAnimation(0.0f, 1.0f)); 
animation.addAnimation(new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, 
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)); 
animation.setDuration(700); 
animation.setRepeatMode(Animation.REVERSE); 
view.startAnimation(animation); 
} 

它非常適用出現動畫,但動畫不會逆轉。 另外,我希望它只動畫一次。

可能有人告訴我我做錯了什麼? 在此先感謝。

回答

4

您只能使用您的代碼啓動一個縮放和Alpha動畫。

這條線:

animation.setRepeatMode(Animation.REVERSE); 

顯然不能很好地在AnimationSet工作,所以你必須將其分別應用於每個動畫。我建議是這樣的:

public void animateHeart(final ImageView view) { 
    ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, 
      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    prepareAnimation(scaleAnimation); 

    AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f); 
    prepareAnimation(alphaAnimation); 

    AnimationSet animation = new AnimationSet(true); 
    animation.addAnimation(alphaAnimation); 
    animation.addAnimation(scaleAnimation); 
    animation.setDuration(700); 
    animation.setFillAfter(true); 

    view.startAnimation(animation); 

} 

private Animation prepareAnimation(Animation animation){ 
    animation.setRepeatCount(1); 
    animation.setRepeatMode(Animation.REVERSE); 
    return animation; 
} 

不要忘記

animation.setFillAfter(true); 

或當動畫完成你的心臟會再次出現。

+0

謝謝你的幫助 –

-1

我對昨天提出的問題有一個答案,那是標記爲重複的。如果你想它只是在這裏回覆我。

+0

謝謝。其他人做了答案,它的工作。儘管我正在尋求更多的幫助,但是你還是介意幫助嗎? –

+0

取決於它是什麼,我可以嘗試。你會提出一個問題,或者你想怎麼做? – Dave

+0

我可以寄給你一封郵件嗎?如果是這樣,請留下評論電子郵件。謝謝 –