Android Animation(四)AnimationListener

利用AnimationListener在一个动画完成之后,继续执行下一个动画


1.activity_main.xml




    


    

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);

            }
        });

    }

}


你可能感兴趣的:(android开发,android,animation)