標籤:android style blog class code java
Animation
在android 程式當中很多時候要用到動畫效果,而動畫效果主要是Animation來實現的,API給出的解釋:
其中包含4種動畫效果
AlphaAnimation 漸層透明度
RotateAnimation 畫面旋轉
ScaleAnimation 漸層尺寸縮放
TranslateAnimation 位置移動
但如果你想把這些動畫效果聯合起來就需要用到一個類AnimationSet 動畫集。
下面就對這幾個類進行一個簡單的解釋:
AlphaAnimation 的例子:
1 Animation alpha = new AlphaAnimation(0.1f, 1.0f);2 alpha.setDuration(2000);3 image.startAnimation(alpha);
透明度從0.1f變化至1.0f, 變化所需要的事件為2s;
RotateAnimation 的例子:
1 Animation rotate = new RotateAnimation(0f, 360f);2 rotate.setDuration(2000);3 image.startAnimation(rotate);
從0度旋轉至360度,期間是2S
ScaleAnimation 的例子:
Animation scale = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f); scale.setDuration(2000); image.startAnimation(scale);
X軸上的長度從0.1變化至1.0f(view原本的寬度)
Y軸上的長度從0.1變化至1.0f(view原本的長度)
TranslateAnimation 的例子:
1 Animation translate = new TranslateAnimation(2 Animation.RELATIVE_TO_SELF, 1.0f,3 Animation.RELATIVE_TO_SELF, 0.0f,4 Animation.RELATIVE_TO_SELF, 1.0f,5 Animation.RELATIVE_TO_SELF, 0.0f);6 translate.setDuration(2000);7 image.startAnimation(translate);
AnimationSet的例子:
Animation rotate1 = new RotateAnimation(0f, 360f); Animation translate1 = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f); AnimationSet set = new AnimationSet(true); set.addAnimation(rotate1); set.addAnimation(translate1); set.setDuration(2000); image.startAnimation(set);
除了再代碼中添加動畫外,還可以直接在xml檔案中定義好動畫,在直接調用就OK了,例如:
1 <?xml version="1.0" encoding="utf-8"?> 2 <set xmlns:android="http://schemas.android.com/apk/res/android"> 3 <alpha 4 android:fromAlpha="0.1" 5 android:toAlpha="1.0" 6 android:duration="2000" 7 /> 8 </set>
程式碼效果:
每個按鈕對應不同的動畫
代碼下載:下載