我有三个具有相同翻译动画的视图。使用0-2中的随机数,三个视图中的一个是动画的。我无法重复动画和每个动画的延迟(应该是大约2000毫秒)。
Animation move = AnimationUtils.loadAnimation(this, R.anim.move);
View view1 = (View) findViewById(R.id.view1);
View view2 = (View) findViewById(R.id.view2);
View view3 = (View) findViewById(R.id.view3);
Random color_box_fall_random = new Random();
int random_int = (color_box_fall_random.nextInt(2));
for (int i = 0; i < 10; i++){
if (random_int == 0){
view1.startAnimation(move);
}
else if (random_int == 1){
view2.startAnimation(move);
}
else{
view3.startAnimation(move);
}
}
move.xml
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator">
<translate
android:startOffset="2000"
android:fromYDelta="-200"
android:toYDelta="50%p"
android:duration="2000" />
</set>
答案 0 :(得分:0)
您需要使用AnimationListener
在结束后开始下一个动画。您可以发布延迟2秒的可运行来延迟。
Animation move;
View view1;
View view2;
View view3;
Random color_box_fall_random;
int i;
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
move = AnimationUtils.loadAnimation(this, R.anim.move);
view1 = findViewById(R.id.view1);
view2 = findViewById(R.id.view2);
view3 = findViewById(R.id.view3);
color_box_fall_random = new Random();
handler = new Handler();
move.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
i++;
if(i < 10) {
handler.postDelayed(new Runnable() {
@Override
public void run() {
switch(color_box_fall_random.nextInt(2)){
case 0:
view1.startAnimation(move);
break;
case 1:
view2.startAnimation(move);
break;
case 2:
view3.startAnimation(move);
break;
}
}
}, 2000);
}
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
switch(color_box_fall_random.nextInt(2)){
case 0:
view1.startAnimation(move);
break;
case 1:
view2.startAnimation(move);
break;
case 2:
view3.startAnimation(move);
break;
}
}