1 ViewPropertyAnimator 概述
屬性動(dòng)畫(huà)對(duì)比原來(lái)的視圖動(dòng)畫(huà)有很多的優(yōu)點(diǎn)妄讯,屬性動(dòng)畫(huà)可以對(duì)所有的對(duì)象做動(dòng)畫(huà)操作顽悼,但Android開(kāi)發(fā)中需要做動(dòng)畫(huà)最多的還是View灭必,如果只是對(duì)一個(gè)view做動(dòng)畫(huà)膨报,很少的幾個(gè)屬性還行要拂,如果是對(duì)同一個(gè)view的十幾個(gè)屬性同時(shí)做動(dòng)畫(huà),相信屬性動(dòng)畫(huà)的寫(xiě)法是比較繁瑣的塔沃。ViewPropertyAnimator從名字就可以看出是專(zhuān)用于View的屬性動(dòng)畫(huà),在API12被提供曙痘。ViewPropertyAnimator專(zhuān)用于操作View動(dòng)畫(huà)芳悲,語(yǔ)法更加簡(jiǎn)潔,使用更加方便边坤。
developer:https://developer.android.google.cn/reference/android/view/ViewPropertyAnimator
如何獲取ViewPropertyAnimator 對(duì)象:
ViewPropertyAnimator 沒(méi)有構(gòu)造函數(shù)名扛,通過(guò)View.animate()方法可以方便的獲取ViewPropertyAnimator 對(duì)象,此時(shí)獲取的動(dòng)畫(huà)對(duì)象就專(zhuān)用于操作當(dāng)前view茧痒。
public ViewPropertyAnimator animate() {
if (mAnimator == null) {
mAnimator = new ViewPropertyAnimator(this);
}
return mAnimator;
}
ViewPropertyAnimator原理簡(jiǎn)析
內(nèi)部利用ValueAnimator實(shí)現(xiàn)動(dòng)畫(huà)肮韧。
用ViewPropertyAnimator 還是ObjectAnimator(ValueAnimator)
如果ViewPropertyAnimator 可以實(shí)現(xiàn)的功能盡量用ViewPropertyAnimator ,因?yàn)楹?jiǎn)單快捷。
2 ViewPropertyAnimator基本用法
基本函數(shù)
setDuration(); //設(shè)置動(dòng)畫(huà)時(shí)長(zhǎng)
setInterpolator(); //設(shè)置插值器
setStartDelay(); //設(shè)置延遲開(kāi)始時(shí)間
start(); //立刻開(kāi)始動(dòng)畫(huà)
cancel(); //取消動(dòng)畫(huà)
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate();
viewPropertyAnimator.setDuration(2000);
viewPropertyAnimator.alpha(0);
沒(méi)有調(diào)用開(kāi)始弄企,如果強(qiáng)制調(diào)用開(kāi)始呢超燃?
沒(méi)有設(shè)置start開(kāi)始動(dòng)畫(huà),動(dòng)畫(huà)依然執(zhí)行了拘领,說(shuō)明設(shè)置完所有動(dòng)畫(huà)之后意乓,動(dòng)畫(huà)會(huì)自動(dòng)開(kāi)始執(zhí)行,也可以手動(dòng)調(diào)用start函數(shù)強(qiáng)制動(dòng)畫(huà)立刻執(zhí)行约素,后面再添加動(dòng)畫(huà)也會(huì)執(zhí)行届良,只不過(guò)會(huì)稍晚點(diǎn)。
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate();
viewPropertyAnimator.setDuration(2000);
viewPropertyAnimator.translationY(300);
viewPropertyAnimator.start();
viewPropertyAnimator.translationX(300);
明顯看到圖片先往下走了一段圣猎,然后向左的動(dòng)畫(huà)才開(kāi)始執(zhí)行士葫。
鏈?zhǔn)讲僮?/strong>
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(2000).translationY(300).scaleX(0.5f);
3 ViewPropertyAnimator 支持的View動(dòng)畫(huà)
ViewPropertyAnimator支持的動(dòng)畫(huà):
alpha(float value) 設(shè)置View的透明度,value最終值
alphaBy(float value) 設(shè)置View的透明度送悔,value是在view當(dāng)前值的基礎(chǔ)上的偏移量慢显,rotation(float value):旋轉(zhuǎn)View,正值順時(shí)針欠啤,負(fù)值逆時(shí)針荚藻,value最終值
rotationBy(float value):旋轉(zhuǎn),在當(dāng)前值得基礎(chǔ)上偏移量
rotationX(float value):繞x軸旋轉(zhuǎn)
rotationXBy(float value):當(dāng)View旋轉(zhuǎn)的基礎(chǔ)上以value為偏移量繞X軸旋轉(zhuǎn)
rotationY(float value):繞Y軸旋轉(zhuǎn)
rotationYBy(float value):在當(dāng)前旋轉(zhuǎn)的基礎(chǔ)上繞Y軸旋轉(zhuǎn)
scaleX(float value):縮放view的X軸方向上的大小
scaleXBy(float value):當(dāng)前View縮放的基礎(chǔ)上跪妥,在X軸方向上對(duì)view進(jìn)行縮放
scaleY(float value):縮放view的Y軸方向上的大小
scaleYBy(float value):當(dāng)前View縮放的基礎(chǔ)上鞋喇,對(duì)view的Y軸方向進(jìn)行縮放
translationX(float value):沿X軸方向平移,value大于0眉撵,X軸正方向
translationXBy(float value):帶有偏移量的平移
translationY(float value):沿Y軸方向平移侦香,value大于0,沿Y軸正方向平移
translationYBy(float value) :在當(dāng)前值的基礎(chǔ)上纽疟,在Y軸方向上平移
x(float value):在當(dāng)前值的基礎(chǔ)上罐韩,修改view 的X坐標(biāo)
xBy(float value):在當(dāng)前值的基礎(chǔ)上,修改view 的X坐標(biāo)
y(float value):在當(dāng)前值的基礎(chǔ)上污朽,修改View的Y的坐標(biāo)
yBy(float value):在當(dāng)前值的基礎(chǔ)上散吵,修改View的Y的坐標(biāo)
z(float value):在當(dāng)前值的基礎(chǔ)上,修改View的Z的坐標(biāo)
zBy(float value):在當(dāng)前值的基礎(chǔ)上蟆肆,修改View的Z的坐標(biāo)
以上函數(shù)的執(zhí)行矾睦,會(huì)導(dǎo)致相同動(dòng)畫(huà)的cancle,以上函數(shù)都是除了坐標(biāo)相關(guān)都是以view左上角為坐標(biāo)原點(diǎn)炎功。
方法都比較簡(jiǎn)單枚冗,下面距幾個(gè)列子:
首先利用translationX,translationXBy來(lái)區(qū)分By的意義:
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(3000).translationY(300);
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(3000).translationYBy(300);
translationYBy 可以多次移動(dòng)View蛇损,translationY多次執(zhí)行沒(méi)有效果赁温。
x坛怪,y修改在屏幕上的坐標(biāo)
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(3000).x(100).y(100);
rotation ,rotationX股囊,rotationY
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(2000).rotation(270);
4 ViewPropertyAnimator 添加監(jiān)聽(tīng)
監(jiān)聽(tīng)函數(shù):
withEndAction:指定一個(gè)操作在下一個(gè)動(dòng)畫(huà)結(jié)束的時(shí)候發(fā)生袜匿。
withStartAction:指定一個(gè)操作在下一個(gè)動(dòng)畫(huà)開(kāi)始的時(shí)候發(fā)生。
setUpdateListener:添加動(dòng)畫(huà)屬性變化監(jiān)聽(tīng)
setListener:添加動(dòng)畫(huà)狀態(tài)監(jiān)聽(tīng)
據(jù)觀察withEndAction相當(dāng)于動(dòng)畫(huà)開(kāi)始稚疹,withStartAction相當(dāng)于動(dòng)畫(huà)結(jié)束居灯。
ViewPropertyAnimator viewPropertyAnimator = gongxiang.animate().setDuration(3000).x(700).y(700).rotation(270).alpha(0.5f).withEndAction(new Runnable() {
@Override
public void run() {
System.out.println("=========withEndAction=======");
}
}).withStartAction(new Runnable() {
@Override
public void run() {
System.out.println("=========withStartAction=======");
}
}).setUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
);
}
}).setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationCancel(Animator animation) {
super.onAnimationCancel(animation);
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
System.out.println("=========onAnimationEnd=======");
}
@Override
public void onAnimationRepeat(Animator animation) {
super.onAnimationRepeat(animation);
}
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
System.out.println("=========onAnimationStart=======");
}
@Override
public void onAnimationPause(Animator animation) {
super.onAnimationPause(animation);
}
@Override
public void onAnimationResume(Animator animation) {
super.onAnimationResume(animation);
}
});
可以看到可以添加setUpdateListener監(jiān)聽(tīng),但無(wú)法通過(guò)這個(gè)監(jiān)聽(tīng)函數(shù)監(jiān)聽(tīng)到當(dāng)前動(dòng)畫(huà)的信息内狗,所以這個(gè)監(jiān)聽(tīng)函數(shù)可能用處不大穆壕。
Animation動(dòng)畫(huà)概述和執(zhí)行原理
Android動(dòng)畫(huà)之補(bǔ)間動(dòng)畫(huà)TweenAnimation
Android動(dòng)畫(huà)之逐幀動(dòng)畫(huà)FrameAnimation
Android動(dòng)畫(huà)之插值器簡(jiǎn)介和系統(tǒng)默認(rèn)插值器
Android動(dòng)畫(huà)之插值器Interpolator自定義
Android動(dòng)畫(huà)之視圖動(dòng)畫(huà)的缺點(diǎn)和屬性動(dòng)畫(huà)的引入
Android動(dòng)畫(huà)之ValueAnimator用法和自定義估值器
Android動(dòng)畫(huà)之ObjectAnimator實(shí)現(xiàn)補(bǔ)間動(dòng)畫(huà)和ObjectAnimator自定義屬性
Android動(dòng)畫(huà)之ObjectAnimator中ofXX函數(shù)全解析-自定義Property,TypeConverter其屏,TypeEvaluator
Android動(dòng)畫(huà)之AnimatorSet聯(lián)合動(dòng)畫(huà)用法
Android動(dòng)畫(huà)之LayoutTransition布局動(dòng)畫(huà)
Android動(dòng)畫(huà)之共享元素動(dòng)畫(huà)
Android動(dòng)畫(huà)之ViewPropertyAnimator(專(zhuān)用于view的屬性動(dòng)畫(huà))
Android動(dòng)畫(huà)之Activity切換動(dòng)畫(huà)overridePendingTransition實(shí)現(xiàn)和Theme Xml方式實(shí)現(xiàn)
Android動(dòng)畫(huà)之ActivityOptionsCompat概述
Android動(dòng)畫(huà)之場(chǎng)景變換Transition動(dòng)畫(huà)的使用
Android動(dòng)畫(huà)之Transition和TransitionManager使用
Android動(dòng)畫(huà)之圓形揭露動(dòng)畫(huà)Circular Reveal
Android 動(dòng)畫(huà)之 LayoutAnimation 動(dòng)畫(huà)
Android動(dòng)畫(huà)之視圖動(dòng)畫(huà)的缺點(diǎn)和屬性動(dòng)畫(huà)的引入