天天看點

Android UI開發第十二篇——動畫效果Animation(一)

Android架構本身就使用了大量的動畫效果,比如Activity切換的動畫效果,Dialog彈出和關閉時的漸變動畫效果以及Toast顯示資訊時的淡入淡出效果等等。Android系統架構為我們提供了一些動畫類及其工具類,是以在Andorid應用中使用動畫效果非常簡單。Android中可以在xml中定義Animation,也可以在java code中定義。

Android中動畫的實作分兩種方式,一種方式是補間動畫 Tween Animation,就是說你定義一個開始和結束,中間的部分由android自身實作。另一種叫逐幀動畫 Frame Animation,就是說一幀一幀的連起來播放就變成了動畫。

一、Tween Animation

xml中實作:

alpha

漸變透明度動畫效果

scale

漸變尺寸伸縮動畫效果

translate

畫面轉換位置移動動畫效果

rotate

畫面轉移旋轉動畫效果

JavaCode中 

AlphaAnimation

ScaleAnimation

TranslateAnimation

RotateAnimation

使用XML檔案定義Tween Animation時XML檔案的根節點可以是<alpha>、<scale> <translate>、<rotate>或者是把它們都放入<set>節點中。如下:

<?xml version="1.0" encoding="utf-8"?>

< set xmlns:android="http://schemas.android.com/apk/res/android">

<alpha/>

<scale/>

<translate/>

<rotate/>

< /set>

Java Code實作如下:

AlphaAnimation:

AnimationSet animationSet = new AnimationSet(true);//建立一個AnimationSet對象

AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//建立一個AlphaAnimation對象

alphaAnimation.setDuration(1000);//設定動畫執行的時間(機關:毫秒)

animationSet.addAnimation(alphaAnimation);//将AlphaAnimation對象添加到AnimationSet當中

view.startAnimation(animationSet);//使用view的startAnimation方法開始執行動畫

RotateAnimation :

AnimationSet animationSet = new AnimationSet(true);

/**

* 前兩個參數定義旋轉的起始和結束的度數,後兩個參數定義圓心的位置

*/

RotateAnimation rotateAnimation = new RotateAnimation(0, 360,

Animation.RELATIVE_TO_PARENT, 1f,

Animation.RELATIVE_TO_PARENT, 0f);

rotateAnimation.setDuration(5000);

animationSet.addAnimation(rotateAnimation);

imageView.startAnimation(animationSet);

TranslateAnimation:

* x和y軸的起始和結束位置

TranslateAnimation translateAnimation = new TranslateAnimation

(

Animation.RELATIVE_TO_SELF, 0f,

Animation.RELATIVE_TO_SELF,0.5f,

Animation.RELATIVE_TO_SELF, 1.0f

);

translateAnimation.setDuration(1000);

animationSet.addAnimation(translateAnimation);

view.startAnimation(animationSet);

ScaleAnimation:

* 圍繞一個點伸縮

ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.1f, 1,

0.1f, Animation.RELATIVE_TO_SELF, 0.5f,

Animation.RELATIVE_TO_SELF, 0.5f);

animationSet.addAnimation(scaleAnimation);

animationSet.setStartOffset(1000);

animationSet.setFillAfter(true);

animationSet.setFillBefore(false);

animationSet.setDuration(2000);

代碼下載下傳位址:

<a href="http://www.devdiv.com/forum.php?mod=viewthread&amp;tid=88504&amp;pid=546599&amp;page=1&amp;extra=#pid546599" target="_blank">http://www.devdiv.com/forum.php?mod=viewthread&amp;tid=88504&amp;pid=546599&amp;page=1&amp;extra=#pid546599</a>

     本文轉自xyz_lmn51CTO部落格,原文連結:http://blog.51cto.com/xyzlmn/817314,如需轉載請自行聯系原作者

繼續閱讀