2017-06-06 72 views
0

我有一個非常簡單的動畫:imageview必須一直向上然後向下。起初,我試着用XML:Android:通過代碼生成動畫但不通過xml

<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:shareInterpolator="false" 
android:fillAfter="true"> 
<set 
    android:interpolator="@android:anim/linear_interpolator" 
    android:repeatCount="-1" 
    android:repeatMode="reverse"> 
    <translate 
     android:fromYDelta="0" 
     android:toYDelta="10" 
     android:duration="300"> 
    </translate> 
</set> 

Animation mAnimation= AnimationUtils.loadAnimation(this, R.anim.myanimation); 
immagineNemico.startAnimation(mAnimation); 

但它沒有工作:我的ImageView沒上去,只能下來。所以,我讀了一問題的回答和我的代碼做動畫:

immagineNemico.setVisibility(View.VISIBLE); 
    TranslateAnimation mAnimation = new TranslateAnimation(
      TranslateAnimation.ABSOLUTE, 0f, 
      TranslateAnimation.ABSOLUTE, 0f, 
      TranslateAnimation.RELATIVE_TO_PARENT, 0f, 
      TranslateAnimation.RELATIVE_TO_PARENT, 0.02f); 
    mAnimation.setDuration(300); 
    mAnimation.setRepeatCount(-1); 
    mAnimation.setRepeatMode(Animation.REVERSE); 
    mAnimation.setInterpolator(new LinearInterpolator()); 
    immagineNemico.setAnimation(mAnimation); 

和它的工作。但爲什麼沒有XML動畫的工作?他們幾乎是一樣的!錯誤在哪裏?

+0

檢查日誌如果發生錯誤,請將其粘貼到此處 –

+0

@EliasFazel沒有錯誤 – Curio

回答

1

我遇到過同樣的問題:動畫工作時,由代碼設置,但不是在XML中。簡單的答案是有些bug /有意混淆了Android動畫中的設計,其中一些XML元素被忽略。

這回答一個相關的問題是什麼第一放倒我了會有事情,對通過XML編程完成後工作:https://stackoverflow.com/a/6351812/8003750

我知道這是不是最滿意的答案,但在短期 - 甚至如果XML是完美的,有些情況下它不起作用。