我知道这是非常简单的事情,但我看不出有什么问题。
我有一个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永远不会被触发。
我做错了什么?
发布于 2012-08-18 00:45:00
我没有运行你的代码,但是在
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等。In,1,2,3,4ANIMATION_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);https://stackoverflow.com/questions/12009727
复制相似问题