属性动画
- 实现Animation框架的的功能
- 属性动画常用属性演示
- 动画的监听事件
ImageView imageview=(ImageView)findViewById(R.id.img);
ObjectAnimator.ofFloat(imageView,"translationX",0F,200F).setDuration(200).start();
参数一为控件,参数二为移动的方向,参数移动的开始和结束
ImageView imageview=(ImageView)findViewById(R.id.img);
ObjectAnimator.ofFloat(imageView,"rotation",0F,360F).setDuration(200).start();
- 旋转360
只要属性有get和set方法就可以通过属性动画操作它
同时执行,多个动作一起执行
ImageView imageview=(ImageView)findViewById(R.id.img);
ObjectAnimator.ofFloat(imageView,"translationX",0F,200F).setDuration(200).start();//平移X
ObjectAnimator.ofFloat(imageView,"rotation",0F,360F).setDuration(200).start();//旋转
ObjectAnimator.ofFloat(imageView,"translationY",0F,200F).setDuration(200).start();//平移Y
ObjectAnimator.ofFloat(imageView,"alpha",0F,1F).setDuration(200).start();//透明度渐变
三个动画可使用类PropertyValuesHolder融合在一起,更加优化,性能提升
PropertyValuesHolder p1=PropertyValuesHolder.ofFloat("rotation",0F,360F);
PropertyValuesHolder p2=PropertyValuesHolder.ofFloat("translationX",0F,200F);
PropertyValuesHolder p3=PropertyValuesHolder.ofFloat("translationY",0F,200F);
ObjectAnimator.ofPropertyValuesHolder(imageView,p1,p2,p3).setDuration(1000).start();
也可执行AnimatorSet把所有动画添加到一起播放
AnimatorSet set=new AnimatorSet();
ObjectAnimator am1=ObjectAnimator.ofFload(imageView,"rotation",0F,360F);
ObjectAnimator am2=ObjectAnimator.ofFloat(imageView,"translationX",0F,200F);
ObjectAnimator am2=ObjectAnimator.ofFloat(imageView,"translationY",0F,200F);
set.playTogether(an1,an2,an3);
set.setDuration();
set.start();
把这句set.playTogether(an1,an2,an3);
变为
set.playSequentially(an1,an2,an3);
可实现按顺序播放动画
实现同时播放和后播放的效果:
//先执行an2和an3,后执行an1
set.play(an2).with(an3);
set.play(an1).after(an2);
动画的监听事件
ObjectAnimator an=ObjectAnimator.ofFloat(imageView,"alpha",0F,1F).setDuration(200).start();//旋转
想实现动画结束的监听只要实现
an.addListener(new Animator.AnminatorListener(){
//实现里面所有的方法
});
但是如果我们只需要实现动画里面的某个方法,可以使用监听AnimatorListenerAdapter
an.addListener(new AnimatorListenerAdapter(){
//只需要实现动画结束的方法
public void onAnimationEnd(Animator animation){
super.onAnimationEnd(animation);
//实现动画结束的逻辑
}
});
实现插值器
animation.setInterpolator(new BounceInterpolator());
BounceInterpolator只是其中一种插值器效果
ValueAnimator
使用ValueAnimator实现一个计时器的效果:
//点击事件中实现计时器
public void click(View view){
final Button but=(Button)view;
ValueAnimator anim=ValueAnimator.ofInt(0,100);
anim.setDuration(5000);
anima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener(){
public void onAnimationUpdate(ValueAnimator animation){
Integer value=(Integer)animation.getAnimatedValue();
but.settext(value+"");
}
});
anim.start();
}
总结属性-常用
- translationX\translationY
- rotation、rotationX\rotationY
- scaleX\scaleY
- X/Y
- alpha
总结--常用方法、类
- ValueAnimator
- ObjectAnimator
- AnimatorUpdateListener
- AnimatorListenerAdapter
- PropertyValuesHolder
- AnimatorSet
- TypeEvaluators
- Interpolators