我想要一个可以无限上下移动图像的动画。但是,当我运行代码时,动画仅发生一次。这是动画 xml:
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="3"
android:repeatMode="restart">
<!-- move down -->
<translate
android:fromYDelta="0%"
android:toYDelta="50%"
android:duration="1500"
android:fillAfter="true"/>
<!-- move up -->
<translate
android:fromYDelta="0%"
android:startOffset="1500"
android:toYDelta="-50%"
android:duration="1500"
android:fillAfter="true"/>
<!-- continue to move up -->
<translate
android:fromYDelta="0%"
android:startOffset="3000"
android:toYDelta="-20%"
android:duration="600"
android:fillAfter="true"/>
<!-- down to original pos -->
<translate
android:fromYDelta="0%"
android:startOffset="3600"
android:toYDelta="20%"
android:duration="600"
android:fillAfter="true"/>
</set>
这是我的java代码:
public class MainActivity extends AppCompatActivity {
Animation move1, move2, move3, move4, move5;
Button login;
ImageView bar1, bar2, bar3, bar4, bar5;
int delay1 = 500;
int delay2 = 1560;
int delay3 = 2300;
int delay4 = 3065;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
login = findViewById(R.id.btLogin);
bar1 = findViewById(R.id.ivBar1);
bar2 = findViewById(R.id.ivBar2);
bar3 = findViewById(R.id.ivBar3);
bar4 = findViewById(R.id.ivBar4);
bar5 = findViewById(R.id.ivBar5);
move1= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
move2= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
move3= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
move4= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
move5= AnimationUtils.loadAnimation(getApplicationContext(),R.anim.move);
bar1.startAnimation(move1);
Handler handler = new Handler();
handler.postDelayed(() -> bar2.startAnimation(move2), delay1);
handler.postDelayed(() -> bar3.startAnimation(move3), delay2);
handler.postDelayed(() -> bar4.startAnimation(move4), delay3);
handler.postDelayed(() -> bar5.startAnimation(move5), delay4);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
}
}
为什么它不无限重复?和xml中的fill-after属性有关系吗?非常感谢您的帮助,这是一个班级项目。
从动画 XML 中删除
android:fillAfter="true"
,并在父 android:repeatCount="infinite"
标记中设置 <set>
,而不是 repeatCount="3"
。 fillAfter
使每个动画保持其最终状态,防止正确循环。
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:repeatCount="infinite"
android:repeatMode="restart">
<!-- move down -->
<translate
android:fromYDelta="0%"
android:toYDelta="50%"
android:duration="1500"/>
<!-- move up -->
<translate
android:fromYDelta="50%"
android:startOffset="1500"
android:toYDelta="-50%"
android:duration="1500"/>
<!-- continue to move up -->
<translate
android:fromYDelta="-50%"
android:startOffset="3000"
android:toYDelta="-20%"
android:duration="600"/>
<!-- down to original pos -->
<translate
android:fromYDelta="-20%"
android:startOffset="3600"
android:toYDelta="0%"
android:duration="600"/>
</set>
这就是让它无限循环所需的全部。