日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

Android Property Animation动画

發(fā)布時(shí)間:2023/12/13 Android 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android Property Animation动画 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

3.0以前,android支持兩種動(dòng)畫(huà)模式,tween animation,frame animation,在android3.0中又引入了一個(gè)新的動(dòng)畫(huà)系統(tǒng):property animation,這三種動(dòng)畫(huà)模式在SDK中被稱(chēng)為property animation,view animation,drawable animation。 可通過(guò)NineOldAndroids項(xiàng)目在3.0之前的系統(tǒng)中使用Property Animation

1. View Animation(Tween Animation)

  View Animation(Tween Animation):補(bǔ)間動(dòng)畫(huà),給出兩個(gè)關(guān)鍵幀,通過(guò)一些算法將給定屬性值在給定的時(shí)間內(nèi)在兩個(gè)關(guān)鍵幀間漸變。

  View animation只能應(yīng)用于View對(duì)象,而且只支持一部分屬性,如支持縮放旋轉(zhuǎn)而不支持背景顏色的改變。

  而且對(duì)于View animation,它只是改變了View對(duì)象繪制的位置,而沒(méi)有改變View對(duì)象本身,比如,你有一個(gè)Button,坐標(biāo)(100,100),Width:200,Height:50,而你有一個(gè)動(dòng)畫(huà)使其變?yōu)閃idth:100,Height:100,你會(huì)發(fā)現(xiàn)動(dòng)畫(huà)過(guò)程中觸發(fā)按鈕點(diǎn)擊的區(qū)域仍是(100,100)-(300,150)。

  View Animation就是一系列View形狀的變換,如大小的縮放,透明度的改變,位置的改變,動(dòng)畫(huà)的定義既可以用代碼定義也可以用XML定義,當(dāng)然,建議用XML定義。

  可以給一個(gè)View同時(shí)設(shè)置多個(gè)動(dòng)畫(huà),比如從透明至不透明的淡入效果,與從小到大的放大效果,這些動(dòng)畫(huà)可以同時(shí)進(jìn)行,也可以在一個(gè)完成之后開(kāi)始另一個(gè)。

  用XML定義的動(dòng)畫(huà)放在/res/anim/文件夾內(nèi),XML文件的根元素可以為<alpha>,<scale>,<translate>,<rotate>,interpolator元素或<set>(表示以上幾個(gè)動(dòng)畫(huà)的集合,set可以嵌套)。默認(rèn)情況下,所有動(dòng)畫(huà)是同時(shí)進(jìn)行的,可以通過(guò)startOffset屬性設(shè)置各個(gè)動(dòng)畫(huà)的開(kāi)始偏移(開(kāi)始時(shí)間)來(lái)達(dá)到動(dòng)畫(huà)順序播放的效果。

  可以通過(guò)設(shè)置interpolator屬性改變動(dòng)畫(huà)漸變的方式,如AccelerateInterpolator,開(kāi)始時(shí)慢,然后逐漸加快。默認(rèn)為AccelerateDecelerateInterpolator。

  定義好動(dòng)畫(huà)的XML文件后,可以通過(guò)類(lèi)似下面的代碼對(duì)指定View應(yīng)用動(dòng)畫(huà)。

ImageView spaceshipImage = (ImageView)findViewById(R.id.spaceshipImage);
Animation hyperspaceJumpAnimation=AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
spaceshipImage.startAnimation(hyperspaceJumpAnimation);

2. Drawable Animation(Frame Animation)

  Drawable Animation(Frame Animation):幀動(dòng)畫(huà),就像GIF圖片,通過(guò)一系列Drawable依次顯示來(lái)模擬動(dòng)畫(huà)的效果。在XML中的定義方式如下:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="true"><item android:drawable="@drawable/rocket_thrust1" android:duration="200" /><item android:drawable="@drawable/rocket_thrust2" android:duration="200" /><item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>

  必須以<animation-list>為根元素,以<item>表示要輪換顯示的圖片,duration屬性表示各項(xiàng)顯示的時(shí)間。XML文件要放在/res/drawable/目錄下。示例:

protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setBackgroundResource(R.drawable.drawable_anim);
anim = (AnimationDrawable) imageView.getBackground();
}

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
anim.stop();
anim.start();
return true;
}
return super.onTouchEvent(event);
}

  我在實(shí)驗(yàn)中遇到兩點(diǎn)問(wèn)題:

  • 要在代碼中調(diào)用Imageview的setBackgroundResource方法,如果直接在XML布局文件中設(shè)置其src屬性當(dāng)觸發(fā)動(dòng)畫(huà)時(shí)會(huì)FC。
  • 在動(dòng)畫(huà)start()之前要先stop(),不然在第一次動(dòng)畫(huà)之后會(huì)停在最后一幀,這樣動(dòng)畫(huà)就只會(huì)觸發(fā)一次。
  • 最后一點(diǎn)是SDK中提到的,不要在onCreate中調(diào)用start,因?yàn)锳nimationDrawable還沒(méi)有完全跟Window相關(guān)聯(lián),如果想要界面顯示時(shí)就開(kāi)始動(dòng)畫(huà)的話,可以在onWindowFoucsChanged()中調(diào)用start()。
  • 3. Property Animation

      屬性動(dòng)畫(huà),這個(gè)是在Android 3.0中才引進(jìn)的,以前學(xué)WPF時(shí)里面的動(dòng)畫(huà)機(jī)制好像就是這個(gè),它更改的是對(duì)象的實(shí)際屬性,在View Animation(Tween Animation)中,其改變的是View的繪制效果,真正的View的屬性保持不變,比如無(wú)論你在對(duì)話中如何縮放Button的大小,Button的有效點(diǎn)擊區(qū)域還是沒(méi)有應(yīng)用動(dòng)畫(huà)時(shí)的區(qū)域,其位置與大小都不變。而在Property Animation中,改變的是對(duì)象的實(shí)際屬性,如Button的縮放,Button的位置與大小屬性值都改變了。而且Property Animation不止可以應(yīng)用于View,還可以應(yīng)用于任何對(duì)象。Property Animation只是表示一個(gè)值在一段時(shí)間內(nèi)的改變,當(dāng)值改變時(shí)要做什么事情完全是你自己決定的。

    在Property Animation中,可以對(duì)動(dòng)畫(huà)應(yīng)用以下屬性:

    • Duration:動(dòng)畫(huà)的持續(xù)時(shí)間
    • TimeInterpolation:屬性值的計(jì)算方式,如先快后慢
    • TypeEvaluator:根據(jù)屬性的開(kāi)始、結(jié)束值與TimeInterpolation計(jì)算出的因子計(jì)算出當(dāng)前時(shí)間的屬性值
    • Repeat Count and behavoir:重復(fù)次數(shù)與方式,如播放3次、5次、無(wú)限循環(huán),可以此動(dòng)畫(huà)一直重復(fù),或播放完時(shí)再反向播放
    • Animation sets:動(dòng)畫(huà)集合,即可以同時(shí)對(duì)一個(gè)對(duì)象應(yīng)用幾個(gè)動(dòng)畫(huà),這些動(dòng)畫(huà)可以同時(shí)播放也可以對(duì)不同動(dòng)畫(huà)設(shè)置不同開(kāi)始偏移
    • Frame refreash delay:多少時(shí)間刷新一次,即每隔多少時(shí)間計(jì)算一次屬性值,默認(rèn)為10ms,最終刷新時(shí)間還受系統(tǒng)進(jìn)程調(diào)度與硬件的影響

    3.1 Property Animation的工作方式

      對(duì)于下圖的動(dòng)畫(huà),這個(gè)對(duì)象的X坐標(biāo)在40ms內(nèi)從0移動(dòng)到40 pixel.按默認(rèn)的10ms刷新一次,這個(gè)對(duì)象會(huì)移動(dòng)4次,每次移動(dòng)40/4=10pixel。

      也可以改變屬性值的改變方法,即設(shè)置不同的interpolation,在下圖中運(yùn)動(dòng)速度先逐漸增大再逐漸減小

      下圖顯示了與上述動(dòng)畫(huà)相關(guān)的關(guān)鍵對(duì)象

    ValueAnimator ?表示一個(gè)動(dòng)畫(huà),包含動(dòng)畫(huà)的開(kāi)始值,結(jié)束值,持續(xù)時(shí)間等屬性。

    ValueAnimator封裝了一個(gè)TimeInterpolator,TimeInterpolator定義了屬性值在開(kāi)始值與結(jié)束值之間的插值方法。

    ValueAnimator還封裝了一個(gè)TypeAnimator,根據(jù)開(kāi)始、結(jié)束值與TimeIniterpolator計(jì)算得到的值計(jì)算出屬性值。

    ValueAnimator根據(jù)動(dòng)畫(huà)已進(jìn)行的時(shí)間跟動(dòng)畫(huà)總時(shí)間(duration)的比計(jì)算出一個(gè)時(shí)間因子(0~1),然后根據(jù)TimeInterpolator計(jì)算出另一個(gè)因子,最后TypeAnimator通過(guò)這個(gè)因子計(jì)算出屬性值,如上例中10ms時(shí):

    首先計(jì)算出時(shí)間因子,即經(jīng)過(guò)的時(shí)間百分比:t=10ms/40ms=0.25

    經(jīng)插值計(jì)算(inteplator)后的插值因子:大約為0.15,上述例子中用了AccelerateDecelerateInterpolator,計(jì)算公式為(input即為時(shí)間因子):

    (Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;

    最后根據(jù)TypeEvaluator計(jì)算出在10ms時(shí)的屬性值:0.15*(40-0)=6pixel。上例中TypeEvaluator為FloatEvaluator,計(jì)算方法為 :

    public Float evaluate(float fraction, Number startValue, Number endValue) {float startFloat = startValue.floatValue();return startFloat + fraction * (endValue.floatValue() - startFloat); }

    參數(shù)分別為上一步的插值因子,開(kāi)始值與結(jié)束值。

    3.2 ValueAnimator

      ValueAnimator包含Property Animation動(dòng)畫(huà)的所有核心功能,如動(dòng)畫(huà)時(shí)間,開(kāi)始、結(jié)束屬性值,相應(yīng)時(shí)間屬性值計(jì)算方法等。應(yīng)用Property Animation有兩個(gè)步聚:

  • 計(jì)算屬性值
  • 根據(jù)屬性值執(zhí)行相應(yīng)的動(dòng)作,如改變對(duì)象的某一屬性。
  •   ValuAnimiator只完成了第一步工作,如果要完成第二步,需要實(shí)現(xiàn)ValueAnimator.onUpdateListener接口,這個(gè)接口只有一個(gè)函數(shù)onAnimationUpdate(),在這個(gè)函數(shù)中會(huì)傳入ValueAnimator對(duì)象做為參數(shù),通過(guò)這個(gè)ValueAnimator對(duì)象的getAnimatedValue()函數(shù)可以得到當(dāng)前的屬性值如:

    ValueAnimator animation = ValueAnimator.ofFloat(0f, 1f);
    animation.setDuration(1000);
    animation.addUpdateListener(new AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
    Log.i("update", ((Float) animation.getAnimatedValue()).toString());
    }
    });
    animation.setInterpolator(new CycleInterpolator(3));
    animation.start();

    此示例中只是向Logcat輸出了一些信息,可以改為想做的工作。

    Animator.AnimatorListener

    onAnimationStart()onAnimationEnd()onAnimationRepeat()//當(dāng)動(dòng)畫(huà)被取消時(shí)調(diào)用,同時(shí)會(huì)調(diào)用onAnimationEnd(). onAnimationCancel()

    ValueAnimator.AnimatorUpdateListener

    onAnimationUpdate()  //通過(guò)監(jiān)聽(tīng)這個(gè)事件在屬性的值更新時(shí)執(zhí)行相應(yīng)的操作,對(duì)于ValueAnimator一般要監(jiān)聽(tīng)此事件執(zhí)行相應(yīng)的動(dòng)作,不然Animation沒(méi)意義,在ObjectAnimator(繼承自ValueAnimator)中會(huì)自動(dòng)更新屬性,如無(wú)必要不必監(jiān)聽(tīng)。在函數(shù)中會(huì)傳遞一個(gè)ValueAnimator參數(shù),通過(guò)此參數(shù)的getAnimatedValue()取得當(dāng)前動(dòng)畫(huà)屬性值。

      可以繼承AnimatorListenerAdapter而不是實(shí)現(xiàn)AnimatorListener接口來(lái)簡(jiǎn)化操作,這個(gè)類(lèi)對(duì)AnimatorListener中的函數(shù)都定義了一個(gè)空函數(shù)體,這樣我們就只用定義想監(jiān)聽(tīng)的事件而不用實(shí)現(xiàn)每個(gè)函數(shù)卻只定義一空函數(shù)體。

    ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f); oa.setDuration(3000); oa.addListener(new AnimatorListenerAdapter(){public void on AnimationEnd(Animator animation){Log.i("Animation","end");} }); oa.start();

    3.3 ObjectAnimator

      繼承自ValueAnimator,要指定一個(gè)對(duì)象及該對(duì)象的一個(gè)屬性,當(dāng)屬性值計(jì)算完成時(shí)自動(dòng)設(shè)置為該對(duì)象的相應(yīng)屬性,即完成了Property Animation的全部?jī)刹讲僮鳌?shí)際應(yīng)用中一般都會(huì)用ObjectAnimator來(lái)改變某一對(duì)象的某一屬性,但用ObjectAnimator有一定的限制,要想使用ObjectAnimator,應(yīng)該滿足以下條件:

    • 對(duì)象應(yīng)該有一個(gè)setter函數(shù):set<PropertyName>(駝峰命名法)
    • 如上面的例子中,像ofFloat之類(lèi)的工場(chǎng)方法,第一個(gè)參數(shù)為對(duì)象名,第二個(gè)為屬性名,后面的參數(shù)為可變參數(shù),如果values…參數(shù)只設(shè)置了一個(gè)值的話,那么會(huì)假定為目的值,屬性值的變化范圍為當(dāng)前值到目的值,為了獲得當(dāng)前值,該對(duì)象要有相應(yīng)屬性的getter方法:get<PropertyName>
    • 如果有g(shù)etter方法,其應(yīng)返回值類(lèi)型應(yīng)與相應(yīng)的setter方法的參數(shù)類(lèi)型一致。

      如果上述條件不滿足,則不能用ObjectAnimator,應(yīng)用ValueAnimator代替。

    tv=(TextView)findViewById(R.id.textview1); btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {ObjectAnimator oa=ObjectAnimator.ofFloat(tv, "alpha", 0f, 1f);oa.setDuration(3000);oa.start();} });

      把一個(gè)TextView的透明度在3秒內(nèi)從0變至1。

      根據(jù)應(yīng)用動(dòng)畫(huà)的對(duì)象或?qū)傩缘牟煌?#xff0c;可能需要在onAnimationUpdate函數(shù)中調(diào)用invalidate()函數(shù)刷新視圖。

    3.4 通過(guò)AnimationSet應(yīng)用多個(gè)動(dòng)畫(huà)

      AnimationSet提供了一個(gè)把多個(gè)動(dòng)畫(huà)組合成一個(gè)組合的機(jī)制,并可設(shè)置組中動(dòng)畫(huà)的時(shí)序關(guān)系,如同時(shí)播放,順序播放等。

      以下例子同時(shí)應(yīng)用5個(gè)動(dòng)畫(huà):

  • 播放anim1;
  • 同時(shí)播放anim2,anim3,anim4;
  • 播放anim5。
  • AnimatorSet bouncer = new AnimatorSet(); bouncer.play(anim1).before(anim2); bouncer.play(anim2).with(anim3); bouncer.play(anim2).with(anim4) bouncer.play(anim5).after(amin2); animatorSet.start();

    3.5 TypeEvalutors

      根據(jù)屬性的開(kāi)始、結(jié)束值與TimeInterpolation計(jì)算出的因子計(jì)算出當(dāng)前時(shí)間的屬性值,android提供了以下幾個(gè)evalutor:

    • IntEvaluator:屬性的值類(lèi)型為int;
    • FloatEvaluator:屬性的值類(lèi)型為float;
    • ArgbEvaluator:屬性的值類(lèi)型為十六進(jìn)制顏色值;
    • TypeEvaluator:一個(gè)接口,可以通過(guò)實(shí)現(xiàn)該接口自定義Evaluator。

      自定義TypeEvalutor很簡(jiǎn)單,只需要實(shí)現(xiàn)一個(gè)方法,如FloatEvalutor的定義:

    public class FloatEvaluator implements TypeEvaluator {public Object evaluate(float fraction, Object startValue, Object endValue) {float startFloat = ((Number) startValue).floatValue();return startFloat + fraction * (((Number) endValue).floatValue() - startFloat);} }

      根據(jù)動(dòng)畫(huà)執(zhí)行的時(shí)間跟應(yīng)用的Interplator,會(huì)計(jì)算出一個(gè)0~1之間的因子,即evalute函數(shù)中的fraction參數(shù),通過(guò)上述FloatEvaluator應(yīng)該很好看出其意思。

    3.6 TimeInterplator

      Time interplator定義了屬性值變化的方式,如線性均勻改變,開(kāi)始慢然后逐漸快等。在Property Animation中是TimeInterplator,在View Animation中是Interplator,這兩個(gè)是一樣的,在3.0之前只有Interplator,3.0之后實(shí)現(xiàn)代碼轉(zhuǎn)移至了TimeInterplator。Interplator繼承自TimeInterplator,內(nèi)部沒(méi)有任何其他代碼。

    • AccelerateInterpolator      ? ? 加速,開(kāi)始時(shí)慢中間加速
    • DecelerateInterpolator       ? 減速,開(kāi)始時(shí)快然后減速
    • AccelerateDecelerateInterolator  ? 先加速后減速,開(kāi)始結(jié)束時(shí)慢,中間加速
    • AnticipateInterpolator       ?反向 ,先向相反方向改變一段再加速播放
    • AnticipateOvershootInterpolator  ? 反向加回彈,先向相反方向改變,再加速播放,會(huì)超出目的值然后緩慢移動(dòng)至目的值
    • BounceInterpolator        ?跳躍,快到目的值時(shí)值會(huì)跳躍,如目的值100,后面的值可能依次為85,77,70,80,90,100
    • CycleIinterpolator         循環(huán),動(dòng)畫(huà)循環(huán)一定次數(shù),值的改變?yōu)橐徽液瘮?shù):Math.sin(2 * mCycles * Math.PI * input)
    • LinearInterpolator         線性,線性均勻改變
    • OvershottInterpolator       ?回彈,最后超出目的值然后緩慢改變到目的值
    • TimeInterpolator         ? 一個(gè)接口,允許你自定義interpolator,以上幾個(gè)都是實(shí)現(xiàn)了這個(gè)接口

    3.7 當(dāng)Layout改變時(shí)應(yīng)用動(dòng)畫(huà)

      ViewGroup中的子元素可以通過(guò)setVisibility使其Visible、Invisible或Gone,當(dāng)有子元素可見(jiàn)性改變時(shí)(VISIBLE、GONE),可以向其應(yīng)用動(dòng)畫(huà),通過(guò)LayoutTransition類(lèi)應(yīng)用此類(lèi)動(dòng)畫(huà):

    transition.setAnimator(LayoutTransition.DISAPPEARING, customDisappearingAnim);

      通過(guò)setAnimator應(yīng)用動(dòng)畫(huà),第一個(gè)參數(shù)表示應(yīng)用的情境,可以以下4種類(lèi)型:

    • APPEARING        當(dāng)一個(gè)元素在其父元素中變?yōu)閂isible時(shí)對(duì)這個(gè)元素應(yīng)用動(dòng)畫(huà)
    • CHANGE_APPEARING    當(dāng)一個(gè)元素在其父元素中變?yōu)閂isible時(shí),因系統(tǒng)要重新布局有一些元素需要移動(dòng),對(duì)這些要移動(dòng)的元素應(yīng)用動(dòng)畫(huà)
    • DISAPPEARING       當(dāng)一個(gè)元素在其父元素中變?yōu)镚ONE時(shí)對(duì)其應(yīng)用動(dòng)畫(huà)
    • CHANGE_DISAPPEARING  ?當(dāng)一個(gè)元素在其父元素中變?yōu)镚ONE時(shí),因系統(tǒng)要重新布局有一些元素需要移動(dòng),這些要移動(dòng)的元素應(yīng)用動(dòng)畫(huà).

      第二個(gè)參數(shù)為一Animator。

    mTransitioner.setStagger(LayoutTransition.CHANGE_APPEARING, 30);

      此函數(shù)設(shè)置動(dòng)畫(huà)延遲時(shí)間,參數(shù)分別為類(lèi)型與時(shí)間。

    3.8 Keyframes

      keyFrame是一個(gè) 時(shí)間/值 對(duì),通過(guò)它可以定義一個(gè)在特定時(shí)間的特定狀態(tài),即關(guān)鍵幀,而且在兩個(gè)keyFrame之間可以定義不同的Interpolator,就好像多個(gè)動(dòng)畫(huà)的拼接,第一個(gè)動(dòng)畫(huà)的結(jié)束點(diǎn)是第二個(gè)動(dòng)畫(huà)的開(kāi)始點(diǎn)。KeyFrame是抽象類(lèi),要通過(guò)ofInt(),ofFloat(),ofObject()獲得適當(dāng)?shù)腒eyFrame,然后通過(guò)PropertyValuesHolder.ofKeyframe獲得PropertyValuesHolder對(duì)象,如以下例子:

    Keyframe kf0 = Keyframe.ofInt(0, 400); Keyframe kf1 = Keyframe.ofInt(0.25f, 200); Keyframe kf2 = Keyframe.ofInt(0.5f, 400); Keyframe kf4 = Keyframe.ofInt(0.75f, 100); Keyframe kf3 = Keyframe.ofInt(1f, 500); PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("width", kf0, kf1, kf2, kf4, kf3); ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(btn2, pvhRotation); rotationAnim.setDuration(2000);

      上述代碼的意思為:設(shè)置btn對(duì)象的width屬性值使其:

    • 開(kāi)始時(shí) Width=400
    • 動(dòng)畫(huà)開(kāi)始1/4時(shí) Width=200
    • 動(dòng)畫(huà)開(kāi)始1/2時(shí) Width=400
    • 動(dòng)畫(huà)開(kāi)始3/4時(shí) Width=100
    • 動(dòng)畫(huà)結(jié)束時(shí) Width=500
    第一個(gè)參數(shù)為時(shí)間百分比,第二個(gè)參數(shù)是在第一個(gè)參數(shù)的時(shí)間時(shí)的屬性值。 定義了一些Keyframe后,通過(guò)PropertyValuesHolder類(lèi)的方法ofKeyframe一個(gè)PropertyValuesHolder對(duì)象,然后通過(guò)ObjectAnimator.ofPropertyValuesHolder獲得一個(gè)Animator對(duì)象。 用下面的代碼可以實(shí)現(xiàn)同樣的效果(上述代碼時(shí)間值是線性,變化均勻): ObjectAnimator oa=ObjectAnimator.ofInt(btn2, "width", 400,200,400,100,500);
    oa.setDuration(2000);
    oa.start();

    3.9 Animating Views

      在View Animation中,對(duì)View應(yīng)用Animation并沒(méi)有改變View的屬性,動(dòng)畫(huà)的實(shí)現(xiàn)是通過(guò)其Parent View實(shí)現(xiàn)的,在View被drawn時(shí)Parents View改變它的繪制參數(shù),draw后再改變參數(shù)invalidate,這樣雖然View的大小或旋轉(zhuǎn)角度等改變了,但View的實(shí)際屬性沒(méi)變,所以有效區(qū)域還是應(yīng)用動(dòng)畫(huà)之前的區(qū)域,比如你把一按鈕放大兩倍,但還是放大這前的區(qū)域可以觸發(fā)點(diǎn)擊事件。為了改變這一點(diǎn),在Android 3.0中給View增加了一些參數(shù)并對(duì)這些參數(shù)增加了相應(yīng)的getter/setter函數(shù)(ObjectAnimator要用這些函數(shù)改變這些屬性):

    • translationX,translationY: View相對(duì)于原始位置的偏移量
    • rotation,rotationX,rotationY: 旋轉(zhuǎn),rotation用于2D旋轉(zhuǎn)角度,3D中用到后兩個(gè)
    • scaleX,scaleY: 縮放比
    • x,y: View的最終坐標(biāo),是View的left,top位置加上translationX,translationY
    • alpha: 透明度
    跟位置有關(guān)的參數(shù)有3個(gè),以X坐標(biāo)為例,可以通過(guò)getLeft(),getX(),getTranslateX()獲得,若有一Button btn2,布局時(shí)其坐標(biāo)為(40,0): //應(yīng)用動(dòng)畫(huà)之前
    btn2.getLeft(); //40
    btn2.getX(); //40
    btn2.getTranslationX(); //0
    //應(yīng)用translationX動(dòng)畫(huà)
    ObjectAnimator oa=ObjectAnimator.ofFloat(btn2,"translationX", 200);
    oa.setDuration(2000);
    oa.start();
    /*應(yīng)用translationX動(dòng)畫(huà)后
    btn2.getLeft(); //40
    btn2.getX(); //240
    btn2.getTranslationX(); //200
    */
    //應(yīng)用X動(dòng)畫(huà),假設(shè)沒(méi)有應(yīng)用之前的translationX動(dòng)畫(huà)
    ObjectAnimator oa=ObjectAnimator.ofFloat(btn2, "x", 200);
    oa.setDuration(2000);
    oa.start();
    /*應(yīng)用X動(dòng)畫(huà)后
    btn2.getLeft(); //40
    btn2.getX(); //200
    btn2.getTranslationX(); //160
    */ 無(wú)論怎樣應(yīng)用動(dòng)畫(huà),原來(lái)的布局時(shí)的位置通過(guò)getLeft()獲得,保持不變; X是View最終的位置; translationX為最終位置與布局時(shí)初始位置這差。 所以若就用translationX即為在原來(lái)基礎(chǔ)上移動(dòng)多少,X為最終多少 getX()的值為getLeft()與getTranslationX()的和 對(duì)于X動(dòng)畫(huà),源代碼是這樣的: case X:
    info.mTranslationX = value - mView.mLeft;
    break;

      Property Animation也可以在XML中定義

    • <set> - AnimatorSet
    • <animator> -?ValueAnimator
    • <objectAnimator> -?ObjectAnimator
    XML文件應(yīng)放大/res/animator/中,通過(guò)以下方式應(yīng)用動(dòng)畫(huà): AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext, R.anim.property_animator);
    set.setTarget(myObject);
    set.start();

    3.10 ViewPropertyAnimator

      如果需要對(duì)一個(gè)View的多個(gè)屬性進(jìn)行動(dòng)畫(huà)可以用ViewPropertyAnimator類(lèi),該類(lèi)對(duì)多屬性動(dòng)畫(huà)進(jìn)行了優(yōu)化,會(huì)合并一些invalidate()來(lái)減少刷新視圖,該類(lèi)在3.1中引入。

      以下兩段代碼實(shí)現(xiàn)同樣的效果: 

    PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", 50f);
    PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 100f);
    ObjectAnimator.ofPropertyValuesHolder(myView, pvhX, pvyY).start(); myView.animate().x(50f).y(100f);

    ?

    作者:AngelDevil
    出處:www.cnblogs.com/angeldevil

    ?

    轉(zhuǎn)載于:https://www.cnblogs.com/tiantianbyconan/p/3737942.html

    總結(jié)

    以上是生活随笔為你收集整理的Android Property Animation动画的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。