學習android中,看了這個博主的文章挺好。爲了更好的學習作爲記錄。
引用地址:http://blog.csdn.net/xyz_lmn/article/details/6906783
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文件
<?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:
[java]
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 :
[java]
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:
[java]
AnimationSet animationSet = new AnimationSet(true);
/**
* x和y軸的起始和結束位置
*/
TranslateAnimation translateAnimation = new TranslateAnimation
(
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF,0.5f,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 1.0f
);
translateAnimation.setDuration(1000);
animationSet.addAnimation(translateAnimation);
view.startAnimation(animationSet);
ScaleAnimation:
[java]
AnimationSet animationSet = new AnimationSet(true);
/**
* 圍繞一個點伸縮
*/
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);
view.startAnimation(animationSet);