屬性動畫
- 實現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