中文国产日韩欧美视频,午夜精品999,色综合天天综合网国产成人网,色综合视频一区二区观看,国产高清在线精品,伊人色播,色综合久久天天综合观看

android屬性動(dòng)畫(huà)Property -電腦資料

電腦資料 時(shí)間:2019-01-01 我要投稿
【m.szmdbiao.com - 電腦資料】

    1、概述

    Android提供了幾種動(dòng)畫(huà)類型:View Animation 、Drawable Animation 、Property Animation ,

android屬性動(dòng)畫(huà)Property

。View Animation相當(dāng)簡(jiǎn)單,不過(guò)只能支持簡(jiǎn)單的縮放、平移、旋轉(zhuǎn)、透明度基本的動(dòng)畫(huà),且有一定的局限性。比如:你希望View有一個(gè)顏色的切換動(dòng)畫(huà);你希望可以使用3D旋轉(zhuǎn)動(dòng)畫(huà);你希望當(dāng)動(dòng)畫(huà)停止時(shí),View的位置就是當(dāng)前的位置;這些View Animation都無(wú)法做到。這就是Property Animation產(chǎn)生的原因,本篇博客詳細(xì)介紹Property Animation的用法。至于Drawable Animation,嗯,略~

    2、相關(guān)API

    Property Animation故名思議就是通過(guò)動(dòng)畫(huà)的方式改變對(duì)象的屬性了,我們首先需要了解幾個(gè)屬性:

    Duration動(dòng)畫(huà)的持續(xù)時(shí)間,默認(rèn)300ms。

    Time interpolation:時(shí)間差值,乍一看不知道是什么,但是我說(shuō)LinearInterpolator、AccelerateDecelerateInterpolator,大家一定知道是干嘛的了,定義動(dòng)畫(huà)的變化率。

    Repeat count and behavior:重復(fù)次數(shù)、以及重復(fù)模式;可以定義重復(fù)多少次;重復(fù)時(shí)從頭開(kāi)始,還是反向。

    Animator sets: 動(dòng)畫(huà)集合,你可以定義一組動(dòng)畫(huà),一起執(zhí)行或者順序執(zhí)行。

    Frame. refresh delay:幀刷新延遲,對(duì)于你的動(dòng)畫(huà),多久刷新一次幀;默認(rèn)為10ms,但最終依賴系統(tǒng)的當(dāng)前狀態(tài);基本不用管。

    相關(guān)的類

    ObjectAnimator 動(dòng)畫(huà)的執(zhí)行類,后面詳細(xì)介紹

    ValueAnimator 動(dòng)畫(huà)的執(zhí)行類,后面詳細(xì)介紹

    AnimatorSet 用于控制一組動(dòng)畫(huà)的執(zhí)行:線性,一起,每個(gè)動(dòng)畫(huà)的先后執(zhí)行等。

    AnimatorInflater 用戶加載屬性動(dòng)畫(huà)的xml文件

    TypeEvaluator 類型估值,主要用于設(shè)置動(dòng)畫(huà)操作屬性的值。

    TimeInterpolator 時(shí)間插值,上面已經(jīng)介紹。

    總的來(lái)說(shuō),屬性動(dòng)畫(huà)就是,動(dòng)畫(huà)的執(zhí)行類來(lái)設(shè)置動(dòng)畫(huà)操作的對(duì)象的屬性、持續(xù)時(shí)間,開(kāi)始和結(jié)束的屬性值,時(shí)間差值等,然后系統(tǒng)會(huì)根據(jù)設(shè)置的參數(shù)動(dòng)態(tài)的變化對(duì)象的屬性。

    3、ObjectAnimator實(shí)現(xiàn)動(dòng)畫(huà)

    之所以選擇ObjectAnimator為第一個(gè)是因?yàn),這個(gè)實(shí)現(xiàn)最簡(jiǎn)單一行代碼,秒秒鐘實(shí)現(xiàn)動(dòng)畫(huà),下面看個(gè)例子:

    布局文件:

    [html] view plaincopy

<code><relativelayout android:id="@+id/id_container" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"><imageview android:id="@+id/id_ball" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:onclick="rotateyAnimRun" android:scaletype="centerCrop" android:src="@drawable/mv"></imageview></relativelayout></code>

    很簡(jiǎn)單,就一張妹子圖片~

    Activity代碼:

    [java] view plaincopy

<code>package com.example.zhy_property_animation;  import android.animation.ObjectAnimator;  import android.app.Activity;  import android.os.Bundle;  import android.view.View;  public class ObjectAnimActivity extends Activity  {      @Override      protected void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          setContentView(R.layout.xml_for_anim);      }      public void rotateyAnimRun(View view)      {           ObjectAnimator//           .ofFloat(view, rotationX, 0.0F, 360.0F)//           .setDuration(500)//           .start();      }  }</code>

    效果:

   

    是不是一行代碼就能實(shí)現(xiàn)簡(jiǎn)單的動(dòng)畫(huà)~~

    對(duì)于ObjectAnimator

    1、提供了ofInt、ofFloat、ofObject,這幾個(gè)方法都是設(shè)置動(dòng)畫(huà)作用的元素、作用的屬性、動(dòng)畫(huà)開(kāi)始、結(jié)束、以及中間的任意個(gè)屬性值。

    當(dāng)對(duì)于屬性值,只設(shè)置一個(gè)的時(shí)候,會(huì)認(rèn)為當(dāng)然對(duì)象該屬性的值為開(kāi)始(getPropName反射獲。,然后設(shè)置的值為終點(diǎn)。如果設(shè)置兩個(gè),則一個(gè)為開(kāi)始、一個(gè)為結(jié)束~~~

    動(dòng)畫(huà)更新的過(guò)程中,會(huì)不斷調(diào)用setPropName更新元素的屬性,所有使用ObjectAnimator更新某個(gè)屬性,必須得有g(shù)etter(設(shè)置一個(gè)屬性值的時(shí)候)和setter方法~

    2、如果你操作對(duì)象的該屬性方法里面,比如上例的setRotationX如果內(nèi)部沒(méi)有調(diào)用view的重繪,則你需要自己按照下面方式手動(dòng)調(diào)用。

    [java] view plaincopy

<code>anim.addUpdateListener(new AnimatorUpdateListener()          {              @Override              public void onAnimationUpdate(ValueAnimator animation)              {  //              view.postInvalidate();  //              view.invalidate();              }          });</code>

    3、看了上面的例子,因?yàn)樵O(shè)置的操作的屬性只有一個(gè),那么如果我希望一個(gè)動(dòng)畫(huà)能夠讓View既可以縮小、又能夠淡出(3個(gè)屬性scaleX,scaleY,alpha),只使用ObjectAnimator咋弄?

    想法是不是很不錯(cuò),可能會(huì)說(shuō)使用AnimatorSet啊,這一看就是一堆動(dòng)畫(huà)塞一起執(zhí)行,但是我偏偏要用一個(gè)ObjectAnimator實(shí)例實(shí)現(xiàn)呢~下面看代碼:

    [java] view plaincopy

<code>public void rotateyAnimRun(final View view)  {      ObjectAnimator anim = ObjectAnimator//              .ofFloat(view, zhy, 1.0F,  0.0F)//              .setDuration(500);//      anim.start();      anim.addUpdateListener(new AnimatorUpdateListener()      {          @Override          public void onAnimationUpdate(ValueAnimator animation)          {              float cVal = (Float) animation.getAnimatedValue();              view.setAlpha(cVal);              view.setScaleX(cVal);              view.setScaleY(cVal);          }      });  }</code>

    把設(shè)置屬性的那個(gè)字符串,隨便寫一個(gè)該對(duì)象沒(méi)有的屬性,就是不管~~咱們只需要它按照時(shí)間插值和持續(xù)時(shí)間計(jì)算的那個(gè)值,我們自己手動(dòng)調(diào)用~

    效果:

   

    這個(gè)例子就是想說(shuō)明一下,有時(shí)候換個(gè)思路不要被API所約束,利用部分API提供的功能也能實(shí)現(xiàn)好玩的效果~~~

    比如:你想實(shí)現(xiàn)拋物線的效果,水平方向100px/s,垂直方向加速度200px/s*s ,咋實(shí)現(xiàn)呢~~可以自己用ObjectAnimator試試~

    4、其實(shí)還有更簡(jiǎn)單的方式,實(shí)現(xiàn)一個(gè)動(dòng)畫(huà)更改多個(gè)效果:使用propertyValuesHolder

    [java] view plaincopy

<code>public void propertyValuesHolder(View view)      {          PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat(alpha, 1f,                  0f, 1f);          PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat(scaleX, 1f,                  0, 1f);          PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat(scaleY, 1f,                  0, 1f);          ObjectAnimator.ofPropertyValuesHolder(view, pvhX, pvhY,pvhZ).setDuration(1000).start();      }</code>

    4、ValueAnimator實(shí)現(xiàn)動(dòng)畫(huà)

    和ObjectAnimator用法很類似,簡(jiǎn)單看一下用view垂直移動(dòng)的動(dòng)畫(huà)代碼:

    [java] view plaincopy

<code>public void verticalRun(View view)      {          ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight                  - mBlueBall.getHeight());          animator.setTarget(mBlueBall);          animator.setDuration(1000).start();      }</code>

    給你的感覺(jué)是不是, 啊,這和ValueAnimator有毛線區(qū)別~但是仔細(xì)看,你看會(huì)發(fā)現(xiàn),沒(méi)有設(shè)置操作的屬性~~也就是說(shuō),上述代碼是沒(méi)有任何效果的,沒(méi)有指定屬性~

    這就是和ValueAnimator的區(qū)別之處:ValueAnimator并沒(méi)有在屬性上做操作,你可能會(huì)問(wèn)這樣有啥好處?我豈不是還得手動(dòng)設(shè)置?

    好處:不需要操作的對(duì)象的屬性一定要有g(shù)etter和setter方法,你可以自己根據(jù)當(dāng)前動(dòng)畫(huà)的計(jì)算值,來(lái)操作任何屬性,記得上例的那個(gè)【我希望一個(gè)動(dòng)畫(huà)能夠讓View既可以縮小、又能夠淡出(3個(gè)屬性scaleX,scaleY,alpha)】嗎?其實(shí)就是這么個(gè)用法~

    實(shí)例:

    布局文件:

    [html] view plaincopy

<code><relativelayout android:id="@+id/id_container" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"><imageview android:id="@+id/id_ball" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/bol_blue"><li><b></button></linearlayout></imageview></relativelayout></code><b><code></code></button>

    左上角一個(gè)小球,底部?jī)蓚(gè)按鈕~我們先看一個(gè)自由落體的代碼:

    [java] view plaincopy

<code>/**      * 自由落體      * @param view      */      public void verticalRun( View view)      {          ValueAnimator animator = ValueAnimator.ofFloat(0, mScreenHeight                  - mBlueBall.getHeight());          animator.setTarget(mBlueBall);          animator.setDuration(1000).start();  //      animator.setInterpolator(value)          animator.addUpdateListener(new AnimatorUpdateListener()          {              @Override              public void onAnimationUpdate(ValueAnimator animation)              {                  mBlueBall.setTranslationY((Float) animation.getAnimatedValue());              }          });      }</code>

    與ObjectAnimator不同的就是我們自己設(shè)置元素屬性的更新~雖然多了幾行代碼,但是貌似提高靈活性~

    下面再來(lái)一個(gè)例子,如果我希望小球拋物線運(yùn)動(dòng)【實(shí)現(xiàn)拋物線的效果,水平方向100px/s,垂直方向加速度200px/s*s 】,分析一下,貌似只和時(shí)間有關(guān)系,但是根據(jù)時(shí)間的變化,橫向和縱向的移動(dòng)速率是不同的,我們?cè)撜?shí)現(xiàn)呢?此時(shí)就要重寫TypeValue的時(shí)候了,因?yàn)槲覀冊(cè)跁r(shí)間變化的同時(shí),需要返回給對(duì)象兩個(gè)值,x當(dāng)前位置,y當(dāng)前位置:

    代碼:

    [java] view plaincopy

<code>/**      * 拋物線      * @param view      */      public void paowuxian(View view)      {          ValueAnimator valueAnimator = new ValueAnimator();          valueAnimator.setDuration(3000);          valueAnimator.setObjectValues(new PointF(0, 0));          valueAnimator.setInterpolator(new LinearInterpolator());          valueAnimator.setEvaluator(new TypeEvaluator<pointf>()          {              // fraction = t / duration              @Override              public PointF evaluate(float fraction, PointF startValue,                      PointF endValue)              {                  Log.e(TAG, fraction * 3 + );                  // x方向200px/s ,則y方向0.5 * 10 * t                  PointF point = new PointF();                  point.x = 200 * fraction * 3;                  point.y = 0.5f * 200 * (fraction * 3) * (fraction * 3);                  return point;              }          });          valueAnimator.start();          valueAnimator.addUpdateListener(new AnimatorUpdateListener()          {              @Override              public void onAnimationUpdate(ValueAnimator animation)              {                  PointF point = (PointF) animation.getAnimatedValue();                  mBlueBall.setX(point.x);                  mBlueBall.setY(point.y);              }          });      }</pointf></code>

    可以看到,因?yàn)閛fInt,ofFloat等無(wú)法使用,我們自定義了一個(gè)TypeValue,每次根據(jù)當(dāng)前時(shí)間返回一個(gè)PointF對(duì)象,(PointF和Point的區(qū)別就是x,y的單位一個(gè)是float,一個(gè)是int;RectF,Rect也是)PointF中包含了x,y的當(dāng)前位置~然后我們?cè)?中獲取,動(dòng)態(tài)設(shè)置屬性:

    效果圖:

   

    有木有兩個(gè)鐵球同時(shí)落地的感覺(jué)~~對(duì),我應(yīng)該搞兩個(gè)球~~ps:物理公式要是錯(cuò)了,就當(dāng)沒(méi)看見(jiàn)哈

    自定義TypeEvaluator傳入的泛型可以根據(jù)自己的需求,自己設(shè)計(jì)個(gè)Bean,

電腦資料

android屬性動(dòng)畫(huà)Property》(http://m.szmdbiao.com)。

    好了,我們已經(jīng)分別講解了ValueAnimator和ObjectAnimator實(shí)現(xiàn)動(dòng)畫(huà);二者區(qū)別;如何利用部分API,自己更新屬性實(shí)現(xiàn)效果;自定義TypeEvaluator實(shí)現(xiàn)我們的需求;但是我們并沒(méi)有講如何設(shè)計(jì)插值,其實(shí)我覺(jué)得把,這個(gè)插值默認(rèn)的那一串實(shí)現(xiàn)類夠用了~~很少,會(huì)自己去設(shè)計(jì)個(gè)超級(jí)變態(tài)的~嗯~所以:略。

    5、監(jiān)聽(tīng)動(dòng)畫(huà)的事件

    對(duì)于動(dòng)畫(huà),一般都是一些輔助效果,比如我要?jiǎng)h除個(gè)元素,我可能希望是個(gè)淡出的效果,但是最終還是要?jiǎng)h掉,并不是你透明度沒(méi)有了,還占著位置,所以我們需要知道動(dòng)畫(huà)如何結(jié)束。

    所以我們可以添加一個(gè)動(dòng)畫(huà)的監(jiān)聽(tīng):

    [java] view plaincopy

<code>public void fadeOut(View view)      {          ObjectAnimator anim = ObjectAnimator.ofFloat(mBlueBall, alpha, 0.5f);          anim.addListener(new AnimatorListener()          {              @Override              public void onAnimationStart(Animator animation)              {                  Log.e(TAG, onAnimationStart);              }              @Override              public void onAnimationRepeat(Animator animation)              {                  // TODO Auto-generated method stub                  Log.e(TAG, onAnimationRepeat);              }              @Override              public void onAnimationEnd(Animator animation)              {                  Log.e(TAG, onAnimationEnd);                  ViewGroup parent = (ViewGroup) mBlueBall.getParent();                  if (parent != null)                      parent.removeView(mBlueBall);              }              @Override              public void onAnimationCancel(Animator animation)              {                  // TODO Auto-generated method stub                  Log.e(TAG, onAnimationCancel);              }          });          anim.start();      }</code>

    這樣就可以監(jiān)聽(tīng)動(dòng)畫(huà)的開(kāi)始、結(jié)束、被取消、重復(fù)等事件~但是有時(shí)候會(huì)覺(jué)得,我只要知道結(jié)束就行了,這么長(zhǎng)的代碼我不能接收,那你可以使用AnimatorListenerAdapter

    [java] view plaincopy

<code>anim.addListener(new AnimatorListenerAdapter()  {      @Override      public void onAnimationEnd(Animator animation)      {          Log.e(TAG, onAnimationEnd);          ViewGroup parent = (ViewGroup) mBlueBall.getParent();          if (parent != null)              parent.removeView(mBlueBall);      }  });</code>

    AnimatorListenerAdapter繼承了AnimatorListener接口,然后空實(shí)現(xiàn)了所有的方法~

    效果圖:

    animator還有cancel()和end()方法:cancel動(dòng)畫(huà)立即停止,停在當(dāng)前的位置;end動(dòng)畫(huà)直接到最終狀態(tài)。

    6、AnimatorSet的使用

    實(shí)例:

    布局文件:

    [html] view plaincopy

<code><relativelayout android:id="@+id/id_container" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"><imageview android:id="@+id/id_ball" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/bol_blue"><li><b></button></linearlayout></imageview></relativelayout></code><b><code></code></button>

    繼續(xù)玩球~

    代碼:

    [java] view plaincopy

<code>package com.example.zhy_property_animation;  import android.animation.AnimatorSet;  import android.animation.ObjectAnimator;  import android.app.Activity;  import android.os.Bundle;  import android.view.View;  import android.view.animation.LinearInterpolator;  import android.widget.ImageView;  public class AnimatorSetActivity extends Activity  {      private ImageView mBlueBall;      @Override      protected void onCreate(Bundle savedInstanceState)      {          super.onCreate(savedInstanceState);          setContentView(R.layout.anim_set);          mBlueBall = (ImageView) findViewById(R.id.id_ball);      }      public void togetherRun(View view)      {          ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, scaleX,                  1.0f, 2f);          ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, scaleY,                  1.0f, 2f);          AnimatorSet animSet = new AnimatorSet();          animSet.setDuration(2000);          animSet.setInterpolator(new LinearInterpolator());          //兩個(gè)動(dòng)畫(huà)同時(shí)執(zhí)行          animSet.playTogether(anim1, anim2);          animSet.start();      }      public void playWithAfter(View view)      {          float cx = mBlueBall.getX();          ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, scaleX,                  1.0f, 2f);          ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, scaleY,                  1.0f, 2f);          ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,                  x,  cx ,  0f);          ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,                  x, cx);          /**          * anim1,anim2,anim3同時(shí)執(zhí)行          * anim4接著執(zhí)行          */          AnimatorSet animSet = new AnimatorSet();          animSet.play(anim1).with(anim2);          animSet.play(anim2).with(anim3);          animSet.play(anim4).after(anim3);          animSet.setDuration(1000);          animSet.start();      }  }</code>

    寫了兩個(gè)效果:

    第一:使用playTogether兩個(gè)動(dòng)畫(huà)同時(shí)執(zhí)行,當(dāng)然還有playSequentially依次執(zhí)行~~

    第二:如果我們有一堆動(dòng)畫(huà),如何使用代碼控制順序,比如1,2同時(shí);3在2后面;4在1之前等~就是效果2了

    有一點(diǎn)注意:animSet.play().with();也是支持鏈?zhǔn)骄幊痰,但是不要想著狂點(diǎn),比如 animSet.play(anim1).with(anim2).before(anim3).before(anim5); 這樣是不行的,系統(tǒng)不會(huì)根據(jù)你寫的這一長(zhǎng)串來(lái)決定先后的順序,所以麻煩你按照上面例子的寫法,多寫幾行:

    效果圖:

   

最新文章