android的動畫提供了旋轉、移動、伸展和淡出等等效果
1、Alpha——淡入淡出
2、Scale——縮放
3、Rotate——旋轉
4、Translate——移動
用java代碼來實現動畫效果如下:
//1表示不透明,0表示透明(淡入淡出)
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//前四個參數表示由什麼樣縮放到什麼樣,後四個參數指明縮放後的位置(縮放)
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1,
0.5f, Animation.RELATIVE_TO_SELF, 0.1f,
Animation.RELATIVE_TO_SELF, 0.1f);
//前兩個參數是由多少度轉到多少度,後四個參數是轉的圓心,RELATIVE_TO_SELF是以自己爲參照物,RELATIVE_TO_PARENT是以父節點爲參照物來定義的,但是都是由自己的(0,0)開始算的(旋轉)
RotateAnimation mRotateUpAnim = new RotateAnimation(0.0f,
-180.0f, Animation.RELATIVE_TO_SELF, 0.2f,
Animation.RELATIVE_TO_SELF, 0.2f);
//前四個參數時表示x軸由哪移動到哪,後四個參數表示y軸有哪移動到哪(移動)
TranslateAnimation translateAnimation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF,
0.5f, Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 1.0f);
//可以將幾個動畫用animationset合併在一起
AnimationSet animationSet = new AnimationSet(true);
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
//設置動畫執行的時間(單位:毫秒)
alphaAnimation.setDuration(1000);
//將AlphaAnimation對象添加到AnimationSet當中
animationSet.addAnimation(alphaAnimation);
ScaleAnimation scaleAnimation = new ScaleAnimation(1, 0.5f, 1, 0.5f, Animation.RELATIVE_TO_SELF, 0.1f, Animation.RELATIVE_TO_SELF, 0.1f);
scaleAnimation.setDuration(1000);
//如果值爲true,控件則保持動畫結束的狀態
scaleAnimation.setFillAfter(true);
animationSet.addAnimation(scaleAnimation );
//使用ImageView的startAnimation方法開始執行動畫
imageView.startAnimation(animationSet);
利用幀動畫
Drawable序列,這些Drawable可以按照指定的時間間歇一個一個顯示
在res/drawable-ldpi目錄下創建anim_nv.xml文件
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/nv1" android:duration="500" />
<item android:drawable="@drawable/nv2" android:duration="500" />
<item android:drawable="@drawable/nv3" android:duration="500" />
<item android:drawable="@drawable/nv4" android:duration="500" />
</animation-list>
如要要把這個動畫設置爲一個圖片的背景:
用java代碼指定該圖片的背景爲該動畫
imageView.setBackgroundResource(R.drawable.anim_nv);
AnimationDrawable animationDrawable = (AnimationDrawable)imageView.getBackground();
animationDrawable.start();
利用xml配置動畫
1、在res文件夾下面建立一個名爲anim的文件夾(控制動畫的文件夾)
2、創建xml文件,首先加入set標籤
3、在該標籤中加入rotate、alpha、scale、translate
4、在代碼中使用AnimationUtil當中裝載xml文件,並生成Animation對象
android:pivotX="50" //使用絕對位置定位
android:pivotX="50%" //使用相對於控件本身定位
android:pivotX="50%p" //相對於控件的父控件定位
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:shareInterpolator="true">
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.0"
android:startOffset="500"
android:duration="2000" />
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="2000" />
</set>
//使用AnimationUtils裝載動畫設置文件
Animation animation=AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);