安卓動畫目前共分為三種動畫逐幀動畫、補間動畫和屬性動畫。
一、逐幀動畫(frame-by-frame animation)
逐幀動畫就是將一個完整的動畫拆分成一張張單獨的圖片,然后再將它們連貫起來進行播放,類似于動畫片的工作原理。
- 素材圖片放到drawable文件夾下,mipmap下無法引用
在drawable文件夾下新建wifi-z的xml文件用來存放圖片
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
//oneshot表示是否循環播放 true只播放一次,false循環播放,圖片順序不能變否則動畫不連貫
<item
android:drawable="@drawable/icon_5"
android:duration="300" />
<item
android:drawable="@drawable/icon_4"
android:duration="300" />
<item
android:drawable="@drawable/icon_3"
android:duration="300" />
<item
android:drawable="@drawable/icon_2"
android:duration="300" />
<item
android:drawable="@drawable/icon_1"
android:duration="300" />
<item
android:drawable="@drawable/icon_0"
android:duration="300" />
</animation-list>-
Activity中引用動畫
// 存放動畫圖片的imageview
private ImageView mImg;
// 開啟動畫的按鈕
private Button mBtn;
// 實現逐幀動畫的類
private AnimationDrawable animationDrawable;mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { mImg.setImageResource(R.drawable.wifi_d); animationDrawable = (AnimationDrawable) mImg.getDrawable(); animationDrawable.start();//停止調用stop方法 } });
二、補間動畫(tweened animation)
補間動畫則是可以對View進行一系列的動畫操作,包括淡入淡出、縮放、平移、旋轉四種。
<b>
補間動畫只是表面上的View移動,View實際還在原來位置,而屬性動畫則是View也跟著移動</b>
-
淡入淡出
AlphaAnimation:透明度(alpha)漸變效果,對應<alpha/>標簽。
外部xml方法
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromAlpha="1.0"
android:fillAfter="false"http://動畫結束時是否保持在該位置
android:interpolator="@android:anim/accelerate_decelerate_interpolator"http://插值器
android:toAlpha="0.1" />Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha); mImg.startAnimation(animation);
java方式
Animation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);
alphaAnimation.setDuration(2000);
alphaAnimation.setFillAfter(false);
mImg.startAnimation(alphaAnimation); 縮放
ScaleAnimation:縮放漸變,可以指定縮放的參考點,對應<scale/>標簽。
xml方式
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXScale="0.2"
android:fromYScale="0.2"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%" //動畫起始位置,相對于屏幕的百分比,兩個都為50%表示動畫從自身中間開始
android:toXScale="1.5"
android:toYScale="1.5" />
java方式
Animation scaleAnimation = new ScaleAnimation(0.0f, 1.5f, 0.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(2000);//設置動畫持續時間為500毫
scaleAnimation.setInterpolator(this, android.R.anim.accelerate_interpolator);//設置動畫插入器
mImg.startAnimation(scaleAnimation);平移
TranslateAnimation:位移漸變,需要指定移動點的開始和結束坐標,對應<translate/>標簽。
xml方式
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toXDelta="320"
android:toYDelta="0" />
java方式
Animation translateAnimation = new TranslateAnimation(0, 320, 0, 0);
translateAnimation.setDuration(2000);
translateAnimation.setInterpolator(this, android.R.accelerate_decelerate_interpolator);//設置動畫插入器
mImg.startAnimation(translateAnimation);旋轉
RotateAnimation:旋轉漸變,可以指定旋轉的參考點,對應<rotate/>標簽。
xml方式
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:repeatCount="-1"http://-1代表無限循環 正數代表循環幾次
android:repeatMode="reverse"http://動畫重復的模式,reverse為反向,當第偶次執行時,動畫方向會相反。restart為重新執行,方向不變
android:toDegrees="360" />
java方式
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(1000);
rotateAnimation.setInterpolator(this, android.R.anim.accelerate_decelerate_interpolator);//設置動畫插入器
mImg.startAnimation(rotateAnimation);組合
AnimationSet:組合漸變,支持組合多種漸變效果,對應<set/>標簽。
java方式
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(scaleAnimation);
mImg.startAnimation(animationSet);動畫監聽器
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//動畫開始時調用 }
@Override
public void onAnimationEnd(Animation animation) {
//動畫結束時調用 }
@Override
public void onAnimationRepeat(Animation animation) {
//動畫重復時調用 } });插值器
AccelerateInterpolator 加速,開始時慢中間加速
DecelerateInterpolator 減速,開始時快然后減速
AccelerateDecelerateInterolator 先加速后減速,開始結束時慢,中間加速
AnticipateInterpolator 反向,先向相反方向改變一段再加速播放
AnticipateOvershootInterpolator 反向加超越,先向相反方向改變,再加速播放,會超出目的值然后緩慢移動至目的值
BounceInterpolator 跳躍,快到目的值時值會跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
CycleIinterpolator 循環,動畫循環一定次數,值的改變為一正弦函數:Math.sin(2* mCycles* Math.PI* input)
LinearInterpolator 線性,線性均勻改變
OvershootInterpolator超越,最后超出目的值然后緩慢改變到目的值
三、屬性動畫(property animation)
功能很強大,可以替代逐幀動畫與補間動畫。
-
ValueAnimator
屬性動畫的運行機制是通過不斷地對值進行操作來實現的,而初始值和結束值之間的動畫過渡就是由ValueAnimator這個類來負責計算的。它的內部使用一種時間循環的機制來計算值與值之間的動畫過渡,我們只需要將初始值和結束值提供給ValueAnimator,并且告訴它動畫所需運行的時長,那么ValueAnimator就會自動幫我們完成從初始值平滑地過渡到結束值這樣的效果。//從0-1-5-1 float 類型 整型可以ofInt ValueAnimator animator = ValueAnimator.ofFloat(0f, 1.0f, 5.0f, 1.0f); animator.setDuration(300); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator valueAnimator) { float value = (float) valueAnimator.getAnimatedValue(); Log.d(TAG, "onAnimationUpdate: " + value); } }); animator.start();
ObjectAnimator
可以直接對任意對象的任意屬性進行動畫操作的,比如說View的alpha屬性。
// 旋轉:rotation 漸變:alpha 平移:x軸tarnslationX(平移時需要先獲取當前位置,getTranslationX)
//縮放 scaleX/scaleY
ObjectAnimator animator = ObjectAnimator.ofFloat(mProperty, "alpha", 1.f, 0f);
animator.setDuration(1000);
animator.start();AnimatorSet(組合動畫)
after(Animator anim) 將現有動畫插入到傳入的動畫之后執行
after(long delay) 將現有動畫延遲指定毫秒后執行
before(Animator anim) 將現有動畫插入到傳入的動畫之前執行
with(Animator anim) 將現有動畫和傳入的動畫同時執行
ObjectAnimator translate = ObjectAnimator.ofFloat(mProperty, "translationX", -500f, 0f);
ObjectAnimator rotation = ObjectAnimator.ofFloat(mProperty, "rotation", 0f, 360f);
ObjectAnimator scale = ObjectAnimator.ofFloat(mProperty, "scaleX", 1.0f, 2.0f, 1.0f);
AnimatorSet set = new AnimatorSet();
set.play(rotation).with(scale).after(translate);
set.setDuration(5000);
set.start();Animator監聽器
-
監聽所有方法
translate.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {} @Override public void onAnimationEnd(Animator animator) { } @Override public void onAnimationCancel(Animator animator) { } @Override public void onAnimationRepeat(Animator animator) { } });
監聽單個方法
rotation.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});