0

我知道这是非常简单的事情,但我没有看到问题所在。

我有一个线性布局:

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 永远不会被触发。

我究竟做错了什么?

4

1 回答 1

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;的操作改变

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

编辑:通过代码制作动画很困难,更简单的方法是通过 XML 文件,首先在 res 文件夹下创建一个文件夹名称 anim 然后创建一个名为 scale.xml 的 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);
于 2012-08-17T16:44:59.957 回答