說起動畫,其實一點也不陌生,在使用一款app的時候為了優化用戶體驗,多多少少的,都會加入動畫。
安卓中的動畫,分為兩大類:補間動畫和屬性動畫。本篇博文會詳細介紹總結這兩大動畫,希望本篇博文對你的學習和生活有所幫助。
**補間動畫**
補間動畫分為四類:平移動畫,旋轉動畫,縮放動畫和漸變動畫。這幾類動畫用法都差不多,只是對象參數不同這里我統一展示出來。以下是效果圖:
實現代碼很簡單:
btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //平移動畫 TranslateAnimation translate=new TranslateAnimation(Animation.RELATIVE_TO_SELF,300, Animation.RELATIVE_TO_SELF,300); translate.setDuration(3000); translate.setFillAfter(true); img.startAnimation(translate); } }); btn2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //旋轉動畫 RotateAnimation rotate=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotate.setFillAfter(true); rotate.setDuration(3000); img.startAnimation(rotate); } }); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //縮放動畫 ScaleAnimation scale=new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); scale.setDuration(3000); img.startAnimation(scale); } }); btn4.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //漸變動畫 AlphaAnimation alpha =new AlphaAnimation(0,1); alpha.setDuration(3000); img.startAnimation(alpha); } });
總結以下:
AlphaAnimation:漸變動畫,0.0f完全透明,1.0f完全顯示。
RotateAnimation:旋轉動畫。起始角度,結束角度,相對值,橫坐標的比例,相對值,縱坐標的比例。(結束角度為正就為順時針,負數為逆時針)
ScaleAnimation:比例動畫,x的位置比例起始,x的位置比例結束,y的位置比例起始,y的位置比例結束,x的錨點,x的參數,y的錨點,y的參數

AnimationSet animationSet=new AnimationSet(false); TranslateAnimation translate=new TranslateAnimation(Animation.RELATIVE_TO_SELF,300, Animation.RELATIVE_TO_SELF,300); translate.setDuration(3000); translate.setFillAfter(true); animationSet.addAnimation(translate); RotateAnimation rotate=new RotateAnimation(0,360, Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); rotate.setFillAfter(true); rotate.setDuration(3000); animationSet.addAnimation(rotate); ScaleAnimation scale=new ScaleAnimation(0,2,0,2,Animation.RELATIVE_TO_SELF,0.5f, Animation.RELATIVE_TO_SELF,0.5f); scale.setDuration(3000); animationSet.addAnimation(scale); AlphaAnimation alpha =new AlphaAnimation(0,1); alpha.setDuration(3000); animationSet.addAnimation(alpha); img.startAnimation(animationSet);
AnimationSet通過addAnimation()方法,可以將動畫結合起來,實現更多的動畫效果。
**屬性動畫**
早起的補間動畫是沒有改變view的參數的,view在刷新的時候會變回原來的樣子。所以,在安卓3.0之后,谷歌推出了屬性動畫。
而屬性動畫的實質就是通過例如:translationX,translationY,scaleX,scaleY,rotationX,rotationY等等,這些新增的屬性,記錄View的值
從而使View的動畫可以得到保存,刷新后不會消失。
下面是一個簡單例子,通過屬性動畫改變x上的縮放值:
接下來是實現代碼:
btn6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(img,"scaleX",0.5f); objectAnimator.setDuration(3000); objectAnimator.setStartDelay(1000); objectAnimator.start(); } });
如果這樣實現,其實是有很多弊端的。因為,屬性動畫只有在3.0的版本之后能用,版本過低,是沒法使用的。並且,實現一個屬性動畫,參數也過於繁瑣。所以,這里推薦
使用開源框架:nineoldandroids。
如果你使用的是Android studio 在gradle中加入:dependencies {
compile 'com.nineoldandroids:library:2.4.0'
}
使用這個開源框架,不但能在低版本中使用屬性動畫,而且,實現更為簡單更加方便。
btn6.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //NIneOldAndroid中的一個類實現 ViewPropertyAnimator .animate(img)
.scaleX(0.5f) .setInterpolator(new OvershootInterpolator())//超過一點回來 .setDuration(3000) .start(); } });
一個簡單的例子:
怎么樣,是不是更加好看了?
代碼也是非常簡單:
ViewPropertyAnimator .animate(img) .translationY(600) .setInterpolator(new BounceInterpolator())//彈性掉落 .setDuration(1500) .start();
還可以這樣:
代碼:
ViewPropertyAnimator .animate(img) .translationX(40) .setInterpolator(new CycleInterpolator(6))//左右來回抖動 .setDuration(1500) .start();
好了,關於動畫就介紹到這里,如果懂得了基本方法,如何酷炫的動畫都可以通過自己組合起來,如果本篇博文有什么寫的不對的地方,歡迎留言,互相探討,最后,希望
你通過這篇博文對你的學生和生活有所幫助。