不能做按钮的动画序列
Can not do the animation sequence of buttons
我正在尝试创建一个动画按钮。我有 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>
在我的 class 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;
}
您必须在 Animation
上设置一个 AnimationListener
并跟踪它完成的次数并开始适当的下一个 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
。
使用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);
假设您有一个要动态添加这些按钮的 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() 中的可运行实例;
我正在尝试创建一个动画按钮。我有 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>
在我的 class 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;
}
您必须在 Animation
上设置一个 AnimationListener
并跟踪它完成的次数并开始适当的下一个 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
。
使用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);
假设您有一个要动态添加这些按钮的 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() 中的可运行实例;