学习了一下动画效果的使用,做一下笔记
ImageView imageView = findViewById(R.id.imageView); ObjectAnimator.ofFloat(imageView,"translationY",0F,200F) .setDuration(1000).start();//translationX也可以 ObjectAnimator.ofFloat(imageView,"rotation",0F,360F) .setDuration(1000).start();//旋转360度 //这里是多个动画同时实现 Y方向上的平移与自身的旋转
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0,360F); PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationX",0,200F); PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationY",0,200F); //设置三个动画 ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start(); // 先传入控件 然后是个可变长的数组
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView,"rotation",0,360F); ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView,"translationX",0,300F); ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView,"translationY",0,300F); AnimatorSet set=new AnimatorSet(); set.playTogether(animator1,animator2,animator3);//同时 set.setDuration(1000); set.start();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(imageView,"rotation",0,360F); ObjectAnimator animator2 = ObjectAnimator.ofFloat(imageView,"translationX",0,300F); ObjectAnimator animator3 = ObjectAnimator.ofFloat(imageView,"translationY",0,300F); AnimatorSet set = new AnimatorSet(); set.playSequentially(animator1,animator2,animator3);//按照顺序开始动画 set.setDuration(1000); set.start();
通过with,after,before来定义多个动画之前的先后顺序
set.play(animator2).with(animator3);
set.play(animator1).after(animator2);
ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",0F,1F);//设置透明度 animator.setDuration(1000);// 1000ms animator.addListener(new AnimatorListenerAdapter() {//设置监听器
//这里只重写了 end的监听,也可以重写 start等监听 @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation);//end时 toast Toast.makeText(MainActivity.this,"anim end",Toast.LENGTH_SHORT).show(); } }); animator.start();