2012-08-17 119 views
0

我知道這是非常簡單的事情,但我沒有看到問題。Android動畫無法啓動

我有一個的LinearLayout:

LinearLayout menuSlide = (LinearLayout) findViewById(R.id.menuSlide); 
menuSlide.startAnimation(new ExpandAnimation(menuSlide, 0, (int) (screenWidth*0.7), 20)); 

而且ExpandAnimation類:

public class ExpandAnimation extends Animation implements Animation.AnimationListener{ 

    private View view; 
    private static int ANIMATION_DURATION; 
    private static final String LOG_CAT = "ExpandAnimation"; 
    private int lastWidth; 
    private int fromWidth; 
    private int toWidth; 
    private static int STEP_SIZE=30; 

    public ExpandAnimation(View v,int fromWidth, int toWidth, int duration){ 
     Log.v(LOG_CAT, "Entramos en el constructor del ExpandAnimation"); 
     this.view = v; 
     ANIMATION_DURATION = 1; 
     this.fromWidth = fromWidth; 
     this.toWidth = toWidth; 

     setDuration(ANIMATION_DURATION); 
     setRepeatCount(20); 
     setFillAfter(false); 
     setInterpolator(new AccelerateInterpolator()); 
     setAnimationListener(this); 
     startNow(); 
    } 

    @Override 
    public void onAnimationEnd(Animation animation) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onAnimationRepeat(Animation animation) { 
     // TODO Auto-generated method stub 
     Log.v(LOG_CAT, "Entra en el onAnimationRepeat"); 
     LayoutParams lyp = view.getLayoutParams(); 
     lyp.width = lastWidth += toWidth/20; 
     view.setLayoutParams(lyp); 

     Log.v(LOG_CAT,"El objeto: " + view.getId() + " tiene ahora de ancho: " + view.getWidth()); 
    } 

    @Override 
    public void onAnimationStart(Animation animation) { 
     // TODO Auto-generated method stub 
     Log.v(LOG_CAT, "Entra en el onAnimationStart"); 
     LayoutParams lyp = view.getLayoutParams(); 
     lyp.width = 0; 
     view.setLayoutParams(lyp); 
     lastWidth=0; 
    } 

} 

確定,程序達到ExpandAnimation的構造,但沒有別的,onAnimationStart永遠不會被解僱。

我在做什麼錯了?

回答

2

我沒有運行您的代碼,但在

public ExpandAnimation(View v,int fromWidth, int toWidth, int duration){ 
     Log.v(LOG_CAT, "Entramos en el constructor del ExpandAnimation"); 
     1. this.view = v; 
     2. ANIMATION_DURATION = 1; 
     3. this.fromWidth = fromWidth; 
     4. this.toWidth = toWidth; 

     setDuration(ANIMATION_DURATION); 
     setRepeatCount(20); 
     setFillAfter(false); 
     setInterpolator(new AccelerateInterpolator()); 
     setAnimationListener(this); 
     startNow(); 
    } 

您沒有設置時間爲變量的值,你可以不喜歡ANIMATION_DURATION = duration;也ANIMATION_DURATION只有1毫秒,即使動畫,你發生將無法看到它嘗試將其更改到500等 在,1,2,3,4變化

view = v; 
ANIMATION_DURATION = duration; 
fromWidth = this.fromWidth; 
toWidth = this.toWidth; 

編輯:動畫v IA代碼是困難的,更簡單的方法來做到這一點是通過一個XML文件,首先創建一個文件夾名阿尼姆res文件夾下再創建一個XML文件命名爲scale.xml並執行以下操作...

<set xmlns:android="http://schemas.android.com/apk/res/android" 
     android:interpolator="@android:anim/accelerate_interpolator"> 
    <scale 
     android:fromXScale="1" 
     android:toXScale="1" 
     android:fromYScale="0.1" 
     android:toYScale="1.0" 
     android:duration="500" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:startOffset="100" /> 
</set> 

然後在您的活動中只需致電

Animation a = AnimationUtils.loadAnimation(this, R.anim.scale); 
((LinearLayout) findViewById(R.id.yourlayoutID)).startAnimation(a); 
+0

它仍然不會啓動動畫開始 – 2012-08-17 17:20:19