无法做按钮的动画序列

时间:2015-01-12 07:14:12

标签: android android-animation

我正在尝试创建动画按钮。我有3个按钮。我根据需要创建了一个动画。并将其应用于按钮。但它同时在所有按钮上执行。我想让她顺序跟随她。首先是第一个按钮,然后是第二个按钮,然后是第三个按钮。 我的动画:

<?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>

在我的班上onCreateView:

Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.falling);
        Button kitchenBtn = (Button) v.findViewById(R.id.buttonKitchen);
        Button hotelBtn = (Button) v.findViewById(R.id.buttonHotel);
        Button engenerBtn = (Button) v.findViewById(R.id.buttonEngener);
        kitchenBtn.setAnimation(animation);
        hotelBtn.setAnimation(animation);
        engenerBtn.setAnimation(animation);

按钮从左边缘开始。我想从第一个左边开始,然后是她身后的第二个和第三个。现在他们同时做了。

完整代码:

public class PurchaseMenu extends Fragment implements View.OnClickListener {
    private int mAnimationsFinished = 0;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.purchase_menu, null);
        final Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.falling);
        final Button kitchenBtn = (Button) v.findViewById(R.id.buttonKitchen);
        final Button hotelBtn = (Button) v.findViewById(R.id.buttonHotel);
        final Button engenerBtn = (Button) v.findViewById(R.id.buttonEngener);
        animation.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(animation);
                } else if (mAnimationsFinished == 1) { //hotelBtn animation ended
                    engenerBtn.setAnimation(animation);
                }
                mAnimationsFinished++; //This would be a member variable
                Log.d("mylognah","finish"+" "+mAnimationsFinished);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
        kitchenBtn.setAnimation(animation);
        kitchenBtn.setOnClickListener(this);
        hotelBtn.setOnClickListener(this);
        engenerBtn.setOnClickListener(this);
        return v;
    }

3 个答案:

答案 0 :(得分:2)

您必须在AnimationListener上设置Animation并跟踪完成的次数,并启动相应的下一个Animation,如下所示:

final Animation fallingAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.falling);
    final Button kitchenBtn = (Button) v.findViewById(R.id.buttonKitchen);
    final Button hotelBtn = (Button) v.findViewById(R.id.buttonHotel);
    final Button engenerBtn = (Button) v.findViewById(R.id.buttonEngener);
    fallingAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            if (mAnimationsFinished == 0) { //Kitchen animation ended
                kitchenBtn.setAnimation(null); //Set the animation on the other button to null
                engenerBtn.setAnimation(null); //Otherwise they animate too
                hotelBtn.setVisibility(View.VISIBLE); //Make the view visible
                hotelBtn.setAnimation(fallingAnimation);
                fallingAnimation.start(); //Restart animation. DO NOT FORGET THIS ONE
            } else if (mAnimationsFinished == 2) { //Hotel animation ended. I couldn't figure out why, but this won't work when you try == 1.
                kitchenBtn.setAnimation(null); //Set the animation on the other button to null
                hotelBtn.setAnimation(null); //Otherwise they animate too
                engenerBtn.setVisibility(View.VISIBLE); //Make the view visible
                engenerBtn.setAnimation(fallingAnimation);
                fallingAnimation.start(); //Restart animation. DO NOT FORGET THIS ONE
            }
            mAnimationsFinished++;
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    kitchenBtn.setAnimation(fallingAnimation);
    hotelBtn.setVisibility(View.INVISIBLE); //Make view invisible. Otherwise they're visible before animated
    engenerBtn.setVisibility(View.INVISIBLE); //Make view invisible. Otherwise they're visible before animated

这样,kitchedBtn将首先设置动画,当Animation结束时,onAnimationEnd方法将被调用,从下一个Animation开始。

答案 1 :(得分:0)

使用startOffSet可以避免此问题。像这样:

Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.falling);
        Button kitchenBtn = (Button) v.findViewById(R.id.buttonKitchen);
        Button hotelBtn = (Button) v.findViewById(R.id.buttonHotel);
        Button engenerBtn = (Button) v.findViewById(R.id.buttonEngener);
        kitchenBtn.setAnimation(animation);
        animation.setStartOffset(animation.getDuration());
        hotelBtn.setAnimation(animation);
        animation.setStartOffset(animation.getDuration()*2);
        engenerBtn.setAnimation(animation);

答案 2 :(得分:0)

假设您有一个要动态添加这些按钮的LinearLayout(或任何视图组)。你可以这样做:

for (int i = 0; i < buttons.size(); i++) {
     Button button = new Button(context); 
     new Handler().postDelayed(new Runnable() {
         @Override
         public void run() {
            linearLayout.addView(button);
            animateButton(button);
         }
     }, BUTTON_DELAY_DURATION * i);
 }

并且不要忘记调用handler.removeCallBacks()来删除onPause()中的runnable实例;