我试图写一个漂亮的动画,但有困难。
在下面的代码中,它可以工作但不是我需要的那么多。第一个按钮显示动画,但第二个和第三个按钮显示动画。我做错了什么?
动画:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<alpha
android:duration="1000"
android:fromAlpha="0.0"
android:startOffset="0"
android:toAlpha="1.0" >
</alpha>
<translate
android:fromXDelta="-100%"
android:duration="1000"
android:toXDelta="0" />
</set>
我的代码:
public class PurchaseMenu extends Fragment implements View.OnClickListener {
private int mAnimationsFinished = 0;
private Button kitchenBtn;
private Button hotelBtn;
private Button engenerBtn;
private Animation fallingAnimation;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.purchase_menu, null);
fallingAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.falling);
kitchenBtn = (Button) v.findViewById(R.id.buttonKitchen);
hotelBtn = (Button) v.findViewById(R.id.buttonHotel);
engenerBtn = (Button) v.findViewById(R.id.buttonEngener);
fallingAnimation .setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
Log.d("mylognah", "start" + " " + mAnimationsFinished);
}
@Override
public void onAnimationEnd(Animation animation) {
if (mAnimationsFinished == 0) { //kitchenBtn animation ended
hotelBtn.setAnimation(fallingAnimation);
} else if (mAnimationsFinished == 1) { //hotelBtn animation ended
engenerBtn.setAnimation(fallingAnimation);
}
mAnimationsFinished++; //This would be a member variable
Log.d("mylognah", "finish" + " " + mAnimationsFinished);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
kitchenBtn.setAnimation(fallingAnimation);
kitchenBtn.setOnClickListener(this);
hotelBtn.setOnClickListener(this);
engenerBtn.setOnClickListener(this);
return v;
}
@Override
public void onClick(View v) {
}
}
答案 0 :(得分:1)
更改这些行:
if (mAnimationsFinished == 0) { //kitchenBtn animation ended
hotelBtn.setAnimation(fallingAnimation);
} else if (mAnimationsFinished == 1) { //hotelBtn animation ended
engenerBtn.setAnimation(fallingAnimation);
}
到此:
if (mAnimationsFinished == 0) { //kitchenBtn animation ended
hotelBtn.startAnimation(fallingAnimation);
} else if (mAnimationsFinished == 1) { //hotelBtn animation ended
engenerBtn.startAnimation(fallingAnimation);
}
答案 1 :(得分:1)
尝试使用startAnimation而不是setAnimation:
@Override
public void onAnimationEnd(Animation animation) {
if (mAnimationsFinished == 0) { //kitchenBtn animation ended
kitchenBtn.clearAnimation();
hotelBtn.startAnimation(fallingAnimation);
} else if (mAnimationsFinished == 1) { //hotelBtn animation ended
hotelBtn.clearAnimation();
engenerBtn.startAnimation(fallingAnimation);
}
mAnimationsFinished++; //This would be a member variable
Log.d("mylognah", "finish" + " " + mAnimationsFinished);
}
<强> setAnimation 强>
设置要为此视图播放的下一个动画。但视图动画尚未开始。
<强> startAnimation 强>
如果您想立即播放动画,请使用startAnimation。此方法允许对开始时间和失效进行细粒度控制,但您必须确保
1)动画设置了开始时间,
2)当动画应该开始时,视图将无效。