前言
補(bǔ)間動(dòng)畫也是由我們指定動(dòng)畫開始、動(dòng)畫結(jié)束2個(gè)關(guān)鍵點(diǎn),中間部分的動(dòng)畫由系統(tǒng)完成。Android補(bǔ)間動(dòng)畫不改變本身執(zhí)行動(dòng)畫view的屬性,我們看到只是效果。
屏幕坐標(biāo)軸

image
具體動(dòng)畫實(shí)現(xiàn)
透明度、旋轉(zhuǎn)、位移、縮放
main/res/anim
1. 透明度
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:interpolator="@android:anim/linear_interpolator"
android:fromAlpha="1"
android:toAlpha="0"/>
AlphaAnimation a1 = (AlphaAnimation) AnimationUtils.loadAnimation(TweenActivity.this, R.anim.a_ainm);
tweenImg. startAnimation(a1);
2. 旋轉(zhuǎn)
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fillAfter="true"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180"/>
RotateAnimation r1 = (RotateAnimation) AnimationUtils.loadAnimation(TweenActivity.this, R.anim.r_ainm);
tweenImg.startAnimation(r1);
3. 位移
<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXDelta="0"
android:fromYDelta="0"
android:interpolator="@android:anim/linear_interpolator"
android:toXDelta="100%"
android:toYDelta="100%" />
TranslateAnimation t1 = (TranslateAnimation) AnimationUtils.loadAnimation(TweenActivity.this, R.anim.t_anim);
tweenImg.startAnimation(t1);
4. 縮放
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="2"
android:toYScale="1"/>
ScaleAnimation s1 = (ScaleAnimation) AnimationUtils.loadAnimation(TweenActivity.this, R.anim.s_ainm);
tweenImg.startAnimation(s1);
組合動(dòng)畫(AnimationSet)
AnimationSet是一個(gè)動(dòng)畫的集合
/*
* 創(chuàng)建一個(gè)AnimationSet,它能夠同時(shí)執(zhí)行多個(gè)動(dòng)畫效果
* 構(gòu)造方法的入?yún)⑷绻恰皌rue”,則代表使用默認(rèn)的interpolator,如果是“false”則代表使用自定義interpolator
*/
AnimationSet animationSet = new AnimationSet(true);
/*
* 設(shè)置動(dòng)畫的持續(xù)時(shí)間
*/
translateAnimation.setDuration(3000);
/*
* 將四種動(dòng)畫效果放入同一個(gè)AnimationSet中
*/
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(scaleAnimation);
animationSet.addAnimation(translateAnimation);
/*
* 同時(shí)執(zhí)行多個(gè)動(dòng)畫效果
*/
view.startAnimation(animationSet);
代碼中編寫補(bǔ)間動(dòng)畫
TranslateAnimation t2 = new TranslateAnimation(0, tweenImg.getWidth(), 0, tweenImg.getHeight());
t2.setDuration(1000);
t2.setFillAfter(false);
t2.setInterpolator(new LinearInterpolator());
t2.setRepeatCount(1);//動(dòng)畫重復(fù)次數(shù)-->執(zhí)行2次-->
動(dòng)畫監(jiān)聽(AnimationListener)
t2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//動(dòng)畫開始
}
@Override
public void onAnimationEnd(Animation animation) {
//動(dòng)畫結(jié)束
}
@Override
public void onAnimationRepeat(Animation animation) {
//動(dòng)畫重復(fù)
}
});
差值器(Interpolator)
什么是Interpolator?
通俗易懂的說(shuō),Interpolator負(fù)責(zé)控制動(dòng)畫變化的速率,使得基本的動(dòng)畫效果能夠以勻速、加速、減速、拋物線速率等各種速率變化。
| java類 | xml資源id | 說(shuō)明 |
|---|---|---|
| AccelerateDecelerateInterpolator | @android:anim/accelerate_decelerate_interpolator | 其變化開始和結(jié)束速率較慢,中間加速 |
| AccelerateInterpolator | @android:anim/accelerate_interpolator | 其變化開始速率較慢,后面加速 |
| DecelerateInterpolator | @android:anim/decelerate_interpolator | 其變化開始速率較快,后面減速 |
| LinearInterpolator | @android:anim/linear_interpolator | 其變化速率恒定 |
| AnticipateInterpolator | @android:anim/anticipate_interpolator | 其變化開始向后甩,然后向前 |
| AnticipateOvershootInterpolator | @android:anim/anticipate_overshoot_interpolator | 其變化開始向后甩,然后向前甩,過沖到目標(biāo)值,最后又回到了終值 |
| OvershootInterpolator | @android:anim/overshoot_interpolator | 其變化開始向前甩,過沖到目標(biāo)值,最后又回到了終值 |
| BounceInterpolator | @android:anim/bounce_interpolator | 其變化在結(jié)束時(shí)反彈 |
| CycleInterpolator | @android:anim/cycle_interpolator | 循環(huán)播放,其速率為正弦曲線 |
| TimeInterpolator | 一個(gè)接口,可以自定義插值器 |