我知道这很简单,但我没有看到问题。
我有一个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永远不会被触发。
我做错了什么?
答案 0 :(得分: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 ms,即使动画发生,您也无法看到它尝试将其更改为500等。
在, 1,2,3,4 更改
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>
然后在你的Activity中简单地调用
Animation a = AnimationUtils.loadAnimation(this, R.anim.scale);
((LinearLayout) findViewById(R.id.yourlayoutID)).startAnimation(a);