2017-06-16 64 views
0

我目前使用的代碼:如何從固定位置上下移動圖像,而不是從底部開始?

TranslateAnimation mAnimation = new TranslateAnimation(
      TranslateAnimation.ABSOLUTE, 0f, 
      TranslateAnimation.ABSOLUTE, 0f, 
      TranslateAnimation.RELATIVE_TO_PARENT, 0f, 
      TranslateAnimation.RELATIVE_TO_PARENT, 1.0f); 
    mAnimation.setDuration(2000); 
    mAnimation.setRepeatCount(-1); 
    mAnimation.setRepeatMode(Animation.REVERSE); 
    mAnimation.setInterpolator(new LinearInterpolator()); 
    imageView.setAnimation(mAnimation); 

這裏是result.(忽略圖像質量,我會改變它)。

我已經將圖像放置在水平中心,垂直與浮動動作按鈕一致。我希望它從那裏開始。

另外,可選地,我創建了淡入淡出AlphaAnimation。我如何同步它們兩個?

代碼AlphaAnimation:

AlphaAnimation blinkAnimation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible 
    blinkAnimation.setDuration(1000); // duration - half a second 
    blinkAnimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate 
    blinkAnimation.setRepeatCount(5000); // Repeat animation infinitely 
    blinkAnimation.setRepeatMode(Animation.REVERSE); 

回答

2

試試這個:

TranslateAnimation mAnimation = new TranslateAnimation(
      TranslateAnimation.RELATIVE_TO_SELF, 0f, 
      TranslateAnimation.RELATIVE_TO_SELF, 0f, 
      TranslateAnimation.RELATIVE_TO_SELF, 0f, 
      TranslateAnimation.RELATIVE_TO_SELF, -1.0f); 
//Less up :  TranslateAnimation.RELATIVE_TO_SELF, -0.5f); 

要與alphaAnimation結合:

AlphaAnimation blinkAnimation = new AlphaAnimation(1, 0); // Change alpha from fully visible to invisible 
    blinkAnimation.setDuration(1000); // duration - half a second 
    blinkAnimation.setInterpolator(new LinearInterpolator()); // do not alter animation rate 
    blinkAnimation.setRepeatCount(5000); // Repeat animation infinitely 
    blinkAnimation.setRepeatMode(Animation.REVERSE); 

AnimationSet set = new AnimationSet(true); 
set.addAnimation(trAnimation); 
set.addAnimation(mAnimation); 
imageView.startAnimation(set) 
+0

感謝,這也幫助,但現在的圖像有點太高了,以前如何阻止它?或者,如果你可以在'TranslateAnimation'上解釋參數,我可以嘗試自己。 –

+0

如果您想停止之前,請更改TranslateAnimation.RELATIVE_TO_SELF,-1.0f);到TranslateAnimation.RELATIVE_TO_SELF,-.0.5f); –

+0

這工作,謝謝。 –

0

下面是詳細代碼淡入|漸現出去並且也用於上移|下移

移動式和移動向下的圖像查看代碼:

TranslateAnimation anim = new TranslateAnimation(
      TranslateAnimation.RELATIVE_TO_SELF, 0f, 
      TranslateAnimation.RELATIVE_TO_SELF, 0f, 
      TranslateAnimation.RELATIVE_TO_SELF, 0f, 
      TranslateAnimation.RELATIVE_TO_SELF, -1.0f); // this is distance of top and bottom form current positiong 

      anim.setDuration(2000); 
      anim.setRepeatCount(3); 
      anim.setRepeatMode(Animation.REVERSE); 
      downloadIV.startAnimation(anim); 

淡入和淡出:

AlphaAnimation anim = new AlphaAnimation(1.0f, 0.0f); 
    anim.setDuration(500); 
    anim.setRepeatCount(4); 
    anim.setRepeatMode(Animation.REVERSE); 

    //anim.setFillAfter(true); 
    downloadIV.startAnimation(anim); 
相關問題