一配椭、動(dòng)畫效果
(1)AlphaAnimation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:toAlpha="0.2" />
</set>
- duration:設(shè)置當(dāng)前動(dòng)畫時(shí)長(zhǎng)
- fromAlpha:設(shè)置透明度的初始值
- toAlpha :設(shè)置透明度的結(jié)束值
(2)RotateAnimation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:duration="3000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
</set>
- fromDegrees:旋轉(zhuǎn)初始的角度
- toDegrees:旋轉(zhuǎn)結(jié)束的角度
- pivotX: 旋轉(zhuǎn)中心點(diǎn)x坐標(biāo)
- pivotY: 旋轉(zhuǎn)中心點(diǎn)y坐標(biāo)
(3)ScaleAnimation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale
android:duration="3000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:toXScale="0.2"
android:toYScale="0.2"
android:pivotX="50%"
android:pivotY="50%" />
</set>
- fromXScale:水平方向縮放比例的初始值
- fromYScale:豎直方向縮放比例的初始值
- toXScale:水平方向縮放比例的結(jié)束值
- toYScale:豎直方向縮放比例的結(jié)束值
- pivotX:縮放中心點(diǎn)的x坐標(biāo)
- pivotY:縮放中心點(diǎn)的y坐標(biāo)
(4)TranslateAnimation
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="3000"
android:fromXDelta="20%"
android:fromYDelta="20%"
android:toXDelta="100%"
android:toYDelta="100%" />
</set>
- fromXDelta:移動(dòng)起始點(diǎn)的x坐標(biāo)
- fromYDelta:移動(dòng)起始點(diǎn)的y坐標(biāo)
- toXDelta:移動(dòng)結(jié)束點(diǎn)的x坐標(biāo)
- toYDelta:移動(dòng)結(jié)束點(diǎn)的y坐標(biāo)
(5)組合動(dòng)畫
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="4000"
android:fillAfter="true"
android:fillBefore="true"
android:startOffset="1000">
<alpha
android:duration="3000"
android:fromAlpha="1.0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="0.2" />
<rotate
android:duration="3000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toDegrees="360" />
<scale
android:duration="3000"
android:fromXScale="1.0"
android:fromYScale="1.0"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toXScale="0.2"
android:toYScale="0.2" />
<translate
android:duration="3000"
android:fromXDelta="20%"
android:fromYDelta="20%"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toXDelta="100%"
android:toYDelta="100%" />
</set>
- fillAfter:視圖是否會(huì)停留在動(dòng)畫結(jié)束的狀態(tài)拴事,默認(rèn)為false
- fillBefore:視圖是否會(huì)停留在動(dòng)畫開始的狀態(tài)况凉,默認(rèn)為true
- startOffset:動(dòng)畫延遲開始時(shí)間
二、動(dòng)畫實(shí)現(xiàn)
(1)通過xml文件實(shí)現(xiàn)動(dòng)畫
- 在res/anim文件下創(chuàng)建xml文件
- 在代碼中通過引用xml文件實(shí)現(xiàn)動(dòng)畫效果
Animation loadAnimation = AnimationUtils.loadAnimation(this, R.anim.animation_translate);
loadAnimation.setFillAfter(true);
View.startAnimation(loadAnimation);
- setDuration:動(dòng)畫的運(yùn)行時(shí)間
- setFillAfter:動(dòng)畫結(jié)束時(shí)是否保持動(dòng)畫最后的狀態(tài)
- setFillBefore:動(dòng)畫結(jié)束時(shí)是否保持動(dòng)畫最后的狀態(tài)
- setInterpolator:設(shè)定插值器
- setRepeatCount:重復(fù)次數(shù)
- setRepeatMode:重復(fù)類型有兩個(gè)值胧奔,reverse表示倒序回放或衡,restart表示從頭播放
- setStartOffset:調(diào)用start函數(shù)之后等待開始運(yùn)行的時(shí)間
(2)通過代碼實(shí)現(xiàn)動(dòng)畫
AnimationSet setAnimation = new AnimationSet(true);
// 設(shè)置組合動(dòng)畫的重復(fù)類型
setAnimation.setRepeatMode(Animation.RESTART);
//透明度
Animation alpha = new AlphaAnimation(1,0);
alpha.setDuration(3000);
//旋轉(zhuǎn)
Animation rotate = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
rotate.setDuration(1000);
rotate.setRepeatMode(Animation.RESTART);
rotate.setRepeatCount(Animation.INFINITE);
//縮放
Animation scale = new ScaleAnimation(1, 0.5f, 1, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
scale.setDuration(1000);
//平移動(dòng)畫
Animation translate = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT, -0.5f,
TranslateAnimation.RELATIVE_TO_PARENT, 0.5f,
TranslateAnimation.RELATIVE_TO_SELF, 0,
TranslateAnimation.RELATIVE_TO_SELF, 0);
translate.setDuration(10000);
//設(shè)置到組合動(dòng)畫
setAnimation.addAnimation(alpha);
setAnimation.addAnimation(rotate);
setAnimation.addAnimation(translate);
setAnimation.addAnimation(scale1);
//添加到imageView中
imageView.startAnimation(setAnimation);
- 補(bǔ)間動(dòng)畫只能夠作用在視圖View上芙沥,無法對(duì)非View的對(duì)象進(jìn)行動(dòng)畫操作。
- 補(bǔ)間動(dòng)畫只是改變了View的視覺效果京办,而不會(huì)真正去改變View的屬性掀序。
- 補(bǔ)間動(dòng)畫只能實(shí)現(xiàn)平移、旋轉(zhuǎn)惭婿、縮放 & 透明度這些簡(jiǎn)單的動(dòng)畫需求不恭。