Android中的動畫主要分爲三類
1.Drawable Animation
2.View Animation
3.Property Animation
這裏介紹其中的兩類,Drawable Animation(逐幀動畫)和View Animation
Drawable Animation 逐幀播放每一張圖片,就好像動畫播放一樣。
View Animation 包括 平移動畫 translate 、縮放動畫 scale 、旋轉動畫 rotate 、漸變動畫 alpha
Drawable Animation (逐幀動畫)
我們使用 animation-list 來定義動畫xml文件,在工程res的anim文件夾或者drawable文件夾下建立我們的動畫文件
anim_list.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<!-- onshot 爲true表示只播放一次,false表示循環播放 -->
<item
android:drawable="@drawable/one"
android:duration="500"/>
<item
android:drawable="@drawable/two"
android:duration="500"/>
<item
android:drawable="@drawable/three"
android:duration="500"/>
<item
android:drawable="@drawable/four"
android:duration="500"/>
<item
android:drawable="@drawable/five"
android:duration="500"/>
<item
android:drawable="@drawable/six"
android:duration="500"/>
</animation-list>
使用AnimationDrawable 啓動動畫,代碼如下,調用stop可以停止動畫
private AnimationDrawable walkDrawable;
image.setImageResource(R.drawable.anim_list);
walkDrawable = (AnimationDrawable)image.getDrawable();
walkDrawable.start();
View Animation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="1000"
android:fromAlpha="0.1"
android:toAlpha="1.0" >
</alpha>
</set>
縮放動畫<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="2000"
android:fillAfter="false"
android:fromXScale="0.0"
android:fromYScale="0.0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
</set>
平移動畫<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:duration="1000"
android:fromXDelta="10"
android:fromYDelta="10"
android:toXDelta="100"
android:toYDelta="100" />
</set>
旋轉動畫<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<rotate
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="+360" />
</set>
加載xml動畫的方式爲Animation loadAnimation;
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.xxx);
view.startAnimation(loadAnimation);
縮放效果旋轉效果
loadAnimation = AnimationUtils.loadAnimation(this, R.anim.translate);
image.startAnimation(loadAnimation);
final Animation loadAnimation2 = AnimationUtils.loadAnimation(this,
R.anim.rotate);
//給動畫1設置監聽
loadAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {
}
@Override
public void onAnimationRepeat(Animation arg0) {
}
@Override
public void onAnimationEnd(Animation arg0) {
//動畫1播放完畢播放動畫2
image.startAnimation(loadAnimation2);
}
});
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<alpha
android:duration="3000"
android:fromAlpha="0.2"
android:toAlpha="1.0" />
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:startOffset="3000"
android:toAlpha="0.2" />
</set>
調用loadAnimation = AnimationUtils.loadAnimation(this,R.anim.continue_anim);
image.startAnimation(loadAnimation);
閃爍效果,有透明變爲不透明,設置倒序重複模式
//閃爍效果
AlphaAnimation alphaAnimation = new AlphaAnimation(0.1f, 1.0f);
alphaAnimation.setDuration(100);
alphaAnimation.setRepeatCount(10);
//倒序重複REVERSE 正序重複RESTART
alphaAnimation.setRepeatMode(Animation.REVERSE);
image.startAnimation(alphaAnimation);
TranslateAnimation translate = new TranslateAnimation(-50, 50,
0, 0);
translate.setDuration(1000);
translate.setRepeatCount(Animation.INFINITE);
translate.setRepeatMode(Animation.REVERSE);
image.startAnimation(translate);
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator" >
<scale
android:duration="1000"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.0"
android:toYScale="1.0" />
<alpha
android:duration="1000"
android:fromAlpha="0"
android:toAlpha="1.0" />
</set>
zoom_out.xml<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/decelerate_interpolator"
android:zAdjustment="top" >
<scale
android:duration="@android:integer/config_mediumAnimTime"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%p"
android:pivotY="50%p"
android:toXScale="0.1"
android:toYScale="0.1" />
<alpha
android:duration="@android:integer/config_mediumAnimTime"
android:fromAlpha="1.0"
android:toAlpha="0" />
</set>
設置界面跳轉動畫overridePendingTransition(R.anim.zoom_in,R.anim.zoom_out);
ListView佈局動畫,多個item同時執行一種效果。如果我們需要一個界面中的多個控件按照相同的動畫方式但是每個控件完成該動畫的時刻不同的話,就可採用LayoutAnimationController
LayoutAnimationController lac=new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.zoom_in));
//順序播放
lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
listView.setLayoutAnimation(lac);
listView.startLayoutAnimation();