當(dāng)前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > 屬性動(dòng)畫
Android提供了幾種動(dòng)畫類型:View Animation(補(bǔ)間動(dòng)畫) 、Drawable Animation (幀動(dòng)畫)、Property Animation (屬性動(dòng)畫)。View Animation相當(dāng)簡單,不過只能支持簡單的縮放、平移、旋轉(zhuǎn)、透明度基本的動(dòng)畫,且有一定的局限性。比如:你希望View有一個(gè)顏色的切換動(dòng)畫;你 希望可以使用3D旋轉(zhuǎn)動(dòng)畫;你希望當(dāng)動(dòng)畫停止時(shí),View的位置就是當(dāng)前的位置;這些View Animation都無法做到。這就是Property Animation產(chǎn)生的 原因。
新引入的屬性動(dòng)畫機(jī)制已經(jīng)不再是針對(duì)于View來設(shè)計(jì)的了,也不限定于只能實(shí)現(xiàn)移動(dòng)、縮放、旋轉(zhuǎn)和淡入淡出這幾種動(dòng)畫操作,同時(shí)也不再只是一 種視覺上的動(dòng)畫效果了。它實(shí)際上是一種不斷地對(duì)值進(jìn)行操作的機(jī)制,并將值賦值到指定對(duì)象的指定屬性上,可以是任意對(duì)象的任意屬性。
一、相關(guān)API
ObjectAnimator 動(dòng)畫的執(zhí)行類
ValueAnimator 動(dòng)畫的執(zhí)行類
AnimatorSet 用于控制一組動(dòng)畫的執(zhí)行
setDuration() 設(shè)置動(dòng)畫時(shí)間
start() 開始動(dòng)畫
cancel() 停止動(dòng)畫在當(dāng)前位置
end() 動(dòng)畫直接到終狀態(tài)
setRepeatMode 設(shè)置動(dòng)畫重復(fù)方式
setRepeatCount設(shè)置動(dòng)畫重復(fù)次數(shù)
二、ObjectAnimator使用
ObjectAnimator是屬性動(dòng)畫框架中重要的實(shí)現(xiàn)類,創(chuàng)建一個(gè)ObjectAnimator只需要通過它的靜態(tài)方法直接返回一個(gè)ObjectAnimator對(duì)象。靜態(tài)方 法如下:
ofFloat(Object target, String propertyName, float... values);
ofInt(Object target, String propertyName, int... values);
ofObject(Object target, String propertyName, TypeEvaluator evaluator, Object... values);
這里先關(guān)注ofFloat、ofint,方法中有三個(gè)參數(shù):
Target:指定執(zhí)行動(dòng)畫的view
propertyName:指定動(dòng)畫的屬性
values:可變數(shù)組參數(shù),指定屬性對(duì)應(yīng)的屬性值
如下所示,給imageview設(shè)置漸變的動(dòng)畫。
ObjectAnimator animator = ObjectAnimator.ofFloat(imageview, "alpha", 1.0f, 0.0f);
// 設(shè)置時(shí)間
animator. setDuration(1000);
// 開始動(dòng)畫
animator.start();
下面列舉出一些可以直接使用的屬性:
translationX、translationY:這兩個(gè)屬性作為一種增量來控制著View對(duì)象從它布局容器的左上角坐標(biāo)開始的位置。
rotation、rotationX、rotationY:這三個(gè)屬性控制著View對(duì)象圍繞它的支點(diǎn)進(jìn)行2D和3D的旋轉(zhuǎn)。
scaleX和scaleY:這兩個(gè)屬性控制著View對(duì)象圍繞它的支點(diǎn)進(jìn)行2D縮放。
alpha:它表示View對(duì)象的alpha透明度。
二、ValueAnimator使用
ValueAnimator是整個(gè)屬性動(dòng)畫中核心的一個(gè)類,前面介紹的ObjectAnimator也是繼承自ValueAnimator。
ValueAnimator本身不提供任何動(dòng)畫效果,它更像一個(gè)數(shù)值發(fā)生器,用來產(chǎn)生具有一定規(guī)律的數(shù)字,從而讓調(diào)用者來控制動(dòng)畫的實(shí)現(xiàn)過程。通常情 況下,在ValueAnimator的AnimatorUpdateListener中監(jiān)聽數(shù)值的變化,從而完成動(dòng)畫的切換。
如下所示,利用ValueAnimator給imageview設(shè)置漸變的動(dòng)畫。
ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.0f);
animator.setDuration(1000);
animator.start();
// 反復(fù)循環(huán),REATART從頭開始循環(huán)
animator.setRepeatMode(ValueAnimator.REVERSE);
animator.setRepeatCount(ValueAnimator.INFINITE);// 無限循環(huán)
animator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
imageView.setAlpha((Float)animation.getAnimatedValue());
}
});
三、AnimatorSet使用
AnimatorSet這個(gè)類來幫我們實(shí)現(xiàn)組合屬性動(dòng)畫的效果。AnimatorSet這個(gè)類提供了一個(gè)play()方法,如果我們向這個(gè)方法中傳入一個(gè)Animator對(duì)象 (ObjectAnimator或者ValueAnimator)將會(huì)返回一個(gè)AnimatorSet.Builder的實(shí)例,AnimatorSet.Builder中包含了以下四個(gè)方法:
after(Animator anim) : 將現(xiàn)有動(dòng)畫插入到傳入的動(dòng)畫之后執(zhí)行。
after(long delay):將現(xiàn)有的動(dòng)畫延遲指定的毫秒后執(zhí)行。
before(Animator anim):將現(xiàn)有的動(dòng)畫插入到傳入的動(dòng)畫之前執(zhí)行。
with(Animator anim):將現(xiàn)有的動(dòng)畫和傳入的動(dòng)畫同時(shí)執(zhí)行。
// 移動(dòng)動(dòng)畫
ObjectAnimator transAnimator = ObjectAnimator.ofFloat(mTextView, "translationX", -500f, 300f);
// 旋轉(zhuǎn)動(dòng)畫
ObjectAnimator rotationAnimator = ObjectAnimator.ofFloat(mTextView, "rotation", 0f, 360f);
// 淡入淡出
ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mTextView, "alpha", 1f, 0f, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.play(rotationAnimator).with(alphaAnimator).after(transAnimator);
animatorSet.setDuration(5000);
animatorSet.start();
四、Animator監(jiān)聽器
對(duì)于動(dòng)畫,一般都是一些輔助效果,比喻說一個(gè)view動(dòng)畫結(jié)束后里一個(gè)view開始動(dòng)畫,這就需要對(duì)動(dòng)畫過程進(jìn)行監(jiān)聽。通過實(shí)現(xiàn)AnimatorListener 接口即可對(duì)動(dòng)畫的Start、End、Repeat、Cancel四個(gè)狀態(tài)監(jiān)聽。
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
Log.e("tag", "onAnimationStart");
}
@Override
public void onAnimationEnd(Animator animation) {
Log.e("tag", "onAnimationEnd");
}
@Override
public void onAnimationCancel(Animator animation) {
Log.e("tag", "onAnimationCancel");
}
@Override
public void onAnimationRepeat(Animator animation) {
Log.e("tag", "onAnimationRepeat");
}
})