- Tween animation補(bǔ)間動(dòng)畫(huà),主要是通過(guò)對(duì)控件實(shí)現(xiàn)透明度(alpha)、尺寸(scale)、位置(translate)、旋轉(zhuǎn)rotate)進(jìn)行改變,通過(guò)集合(set)的方式,實(shí)現(xiàn)連續(xù)的動(dòng)畫(huà)效果
- Frame animation
Tween動(dòng)畫(huà)
1、直接子類(lèi)
- AlphaAnimation;控制一個(gè)對(duì)象的透明度播放的動(dòng)畫(huà)
- RotateAnimation;控制一個(gè)對(duì)象旋轉(zhuǎn)的動(dòng)畫(huà)
- ScaleAnimation;控制一個(gè)對(duì)象尺寸的動(dòng)畫(huà)
- TranslateAnimation;控制一個(gè)對(duì)象位置的動(dòng)畫(huà)
2、監(jiān)聽(tīng)器
animation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
3、屬性及其對(duì)應(yīng)方法(所有子類(lèi)都擁有)
android:detachWallpaper: 是否在壁紙上運(yùn)行
android:duration: 動(dòng)畫(huà)時(shí)長(zhǎng),單位毫秒。
android:fillAfter: 設(shè)置為true,控件動(dòng)畫(huà)結(jié)束時(shí)將保持動(dòng)畫(huà)最后一幀(xml文件中,需要設(shè)置在set便簽才生效)。
android:fillBefore: 設(shè)置為true,控件動(dòng)畫(huà)結(jié)束時(shí)將保持動(dòng)畫(huà)開(kāi)始第一幀(感覺(jué)很坑爹,設(shè)置true和false還有刪除這個(gè)屬性,效果都一樣)。
android:fillEnabled: 效果和fillBefore一樣(同樣坑爹,經(jīng)測(cè)試這個(gè)屬性可有可無(wú),求打臉。
android:interpolator: 插值器。設(shè)置動(dòng)畫(huà)速率的變化(譬如加速、減速、勻速等),后面詳說(shuō)。
android:repeatCount: 動(dòng)畫(huà)重復(fù)次數(shù)
ndroid:repeatMode: 重復(fù)模式,有reverse(倒序)和restart(重復(fù))兩種,必須配合repeatCount一起使用
android:startOffset: 延遲一定毫秒之后才開(kāi)始動(dòng)畫(huà)
-
android:zAdjustment: 表示被設(shè)置動(dòng)畫(huà)的內(nèi)容在動(dòng)畫(huà)運(yùn)行時(shí)在Z軸上的位置,有以下三個(gè)值
- normal 默認(rèn)值,保持內(nèi)容在Z軸上的位置不變
- top 保持在Z周最上層
- bottom 保持在Z軸最下層
4、Interpolator
主要實(shí)現(xiàn)動(dòng)畫(huà)的速率變化,interpolator作為一個(gè)接口,然后抽象類(lèi)BaseInterpolator實(shí)現(xiàn)Interpolator接口,在BaseInterpolator的子類(lèi)就是android一系列Android提供的插值器。
用法:
- 在XML的標(biāo)簽下設(shè)置:android:interpolator=”@android:anim/accelerate_decelerate_interpolator”
- 在JAVA代碼中使用:animation.setInterpolator(new AccelerateDecelerateInterpolator());
以下提供android 所有的插值器
- AccelerateDecelerateInterpolator:開(kāi)始和結(jié)束速度慢,中間部分加速
- AccelerateInterpolator:開(kāi)始緩慢,然后加速
- AnticipateInterpolator:開(kāi)始后退,然后前進(jìn)
- AnticipateOvershootInterpolator:開(kāi)始后退,然后前進(jìn),直到超出目標(biāo)值,再后退至目標(biāo)值
- BounceInterpolator:在結(jié)束時(shí)彈跳
- CycleInterpolator:在指定的周期內(nèi)重復(fù)動(dòng)畫(huà),速度變化遵循正弦規(guī)律
- DecelerateInterpolator:開(kāi)始加速,結(jié)束緩慢
- LinearInterpolator:勻速
- OvershootInterpolator:前進(jìn),直到超出目標(biāo)值,再后退至目標(biāo)值
- PathInterpolator: 根據(jù)路勁變化改變速率
5、AnimationSet
在res/anim/.xml文件下實(shí)現(xiàn)多種動(dòng)畫(huà)(透明度、尺寸、位置、旋轉(zhuǎn))
6、相關(guān)代碼實(shí)現(xiàn)
-
java代碼實(shí)現(xiàn)動(dòng)畫(huà)
private void alpha() { AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f); alphaAnimation.setDuration(2000); alphaAnimation.setFillAfter(true); imv.setAnimation(alphaAnimation); //imv.startAnimation(alphaAnimation); } private void rote() { RotateAnimation rotateAnimation = new RotateAnimation(0, 720f); rotateAnimation.setDuration(2000); rotateAnimation.setFillAfter(true); imv2.setAnimation(rotateAnimation); } private void scale() { ScaleAnimation scaleAnimation = new ScaleAnimation(0f, 2f, 0f, 3f); scaleAnimation.setDuration(3000); scaleAnimation.setFillAfter(true); imv.setAnimation(scaleAnimation); } private void translate() { TranslateAnimation animation = new TranslateAnimation(0f, 200f, 0f, 300f); animation.setDuration(3000); animation.setFillAfter(true); imv.setAnimation(animation); }
-
xml實(shí)現(xiàn)動(dòng)畫(huà)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:shareInterpolator="false"> <scale android:duration="1500" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="0.5" /> <set android:duration="2000" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:startOffset="1500"> <scale android:duration="1500" android:fromXScale="1.5" android:fromYScale="0.6" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:pivotX="50%" android:pivotY="50%" android:toXScale="0.0" android:toYScale="0.0" /> <rotate android:fromDegrees="0.0" android:pivotX="50%" android:pivotY="50%" android:toDegrees="-45" /> </set> </set>
java中引用res文件
private void animationSet(){
Animation animation = AnimationUtils.loadAnimation(getActivity(), R.anim.animation_set);
imv.startAnimation(animation);
}