利用AnimationListener在一个动画完成之后,继续执行下一个动画
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="sc.animationforth.MainActivity">
<ImageView
android:id="@+id/image_view"
android:src="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="启动动画java"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
2.MainActivity.java
public class MainActivity extends AppCompatActivity{
private ImageView imageView;
private Button btnStart;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.image_view);
btnStart = (Button) findViewById(R.id.btn_start);
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AnimationSet animationSet = new AnimationSet(true);
ScaleAnimation scaleAnimation = new ScaleAnimation(0,0.1f,0,0.1f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
final RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
rotateAnimation.setDuration(5000);
scaleAnimation.setDuration(5000);
//设置一个动画监听器
scaleAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
//动画开始的时候执行的方法
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this,"动画开始了",Toast.LENGTH_SHORT).show();
}
@Override
//动画结束的时候执行的方法
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this,"动画结束了",Toast.LENGTH_SHORT).show();
imageView.startAnimation(rotateAnimation);
}
@Override
//动画重复的时候执行的方法
public void onAnimationRepeat(Animation animation) {
}
});
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
imageView.startAnimation(animationSet);
}
});
}
}