亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android中Property Animation屬性動(dòng)畫編寫的實(shí)例教程

 更新時(shí)間:2016年04月19日 14:47:56   作者:鴻洋_  
這篇文章主要介紹了Android中Property Animation屬性動(dòng)畫編寫的實(shí)例教程,Property Animation對于動(dòng)畫幀的操控十分強(qiáng)大,需要的朋友可以參考下

1、概述
Android提供了幾種動(dòng)畫類型:View Animation 、Drawable Animation 、Property Animation 。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)生的原因,本篇?jiǎng)t來詳細(xì)介紹Property Animation的用法。
property 動(dòng)畫系統(tǒng)是相當(dāng)健壯的框架,它幾乎可以動(dòng)畫顯示任何對象。 你可以定義一個(gè)動(dòng)畫來定時(shí)改變?nèi)魏螌ο蟮膶傩灾?,不論該對象是否在屏幕上顯示。 property 動(dòng)畫將以一定的時(shí)間間隔修改屬性值(對象中的字段值)。 要實(shí)現(xiàn)動(dòng)畫顯示,你須指定對象的相應(yīng)屬性(比如對象的屏幕位置),以及動(dòng)畫時(shí)長、動(dòng)畫時(shí)間間隔。
property 動(dòng)畫系統(tǒng)能讓你設(shè)定以下動(dòng)畫要素:
(1)持續(xù)時(shí)間:指定動(dòng)畫的持續(xù)顯示時(shí)間。默認(rèn)的時(shí)長是300毫秒。
(2)圖像插值方式:指定屬性值的變化方式,表示為關(guān)于動(dòng)畫已顯示時(shí)間的函數(shù)。
(3)重復(fù)次數(shù)和方式:指定動(dòng)畫是否循環(huán)播放,以及重復(fù)的次數(shù)。還可以指定動(dòng)畫是否反向播放。可以設(shè)為先正向播放再反向回放,如此往復(fù)直至達(dá)到設(shè)定的重復(fù)次數(shù)。
(4)動(dòng)畫集合:你可以把動(dòng)畫分為多個(gè)邏輯組,以便實(shí)現(xiàn)同時(shí)播放、順序播放或間隔一段時(shí)間再播放。
(5)幀刷新間隔:指定動(dòng)畫幀的刷新頻率。默認(rèn)是每 10 ms刷新一次,但應(yīng)用程序?qū)嶋H可執(zhí)行的刷新頻率取決于系統(tǒng)整體的繁忙程度,以及系統(tǒng)對定時(shí)器的支持程度。

2、相關(guān)API
Property Animation故名思議就是通過動(dòng)畫的方式改變對象的屬性了,我們首先需要了解幾個(gè)屬性:
(1)Duration動(dòng)畫的持續(xù)時(shí)間,默認(rèn)300ms。
(2)Time interpolation:時(shí)間差值,乍一看不知道是什么,但是我說LinearInterpolator、AccelerateDecelerateInterpolator,大家一定知道是干嘛的了,定義動(dòng)畫的變化率。
(3)Repeat count and behavior:重復(fù)次數(shù)、以及重復(fù)模式;可以定義重復(fù)多少次;重復(fù)時(shí)從頭開始,還是反向。
(4)Animator sets: 動(dòng)畫集合,你可以定義一組動(dòng)畫,一起執(zhí)行或者順序執(zhí)行。
(5)Frame refresh delay:幀刷新延遲,對于你的動(dòng)畫,多久刷新一次幀;默認(rèn)為10ms,但最終依賴系統(tǒng)的當(dāng)前狀態(tài);基本不用管。
相關(guān)的類:
(1)ObjectAnimator  動(dòng)畫的執(zhí)行類,后面詳細(xì)介紹
(2)ValueAnimator 動(dòng)畫的執(zhí)行類,后面詳細(xì)介紹
(3)AnimatorSet 用于控制一組動(dòng)畫的執(zhí)行:線性,一起,每個(gè)動(dòng)畫的先后執(zhí)行等。
(4)AnimatorInflater 用戶加載屬性動(dòng)畫的xml文件
(5)TypeEvaluator  類型估值,主要用于設(shè)置動(dòng)畫操作屬性的值。
(6)TimeInterpolator 時(shí)間插值,上面已經(jīng)介紹。
總的來說,屬性動(dòng)畫就是,動(dòng)畫的執(zhí)行類來設(shè)置動(dòng)畫操作的對象的屬性、持續(xù)時(shí)間,開始和結(jié)束的屬性值,時(shí)間差值等,然后系統(tǒng)會(huì)根據(jù)設(shè)置的參數(shù)動(dòng)態(tài)的變化對象的屬性。

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:id="@+id/id_container" > 
 
  <ImageView 
    android:id="@+id/id_ball" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:src="@drawable/mv"  
    android:scaleType="centerCrop" 
    android:onClick="rotateyAnimRun" 
    /> 
 
</RelativeLayout> 

很簡單,就一張妹子圖片~
Activity代碼:

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(); 
  } 
 
} 

效果:

2016419144329934.gif (414×564)

是不是一行代碼就能實(shí)現(xiàn)簡單的動(dòng)畫~~
對于ObjectAnimator:
(1)提供了ofInt、ofFloat、ofObject,這幾個(gè)方法都是設(shè)置動(dòng)畫作用的元素、作用的屬性、動(dòng)畫開始、結(jié)束、以及中間的任意個(gè)屬性值。
當(dāng)對于屬性值,只設(shè)置一個(gè)的時(shí)候,會(huì)認(rèn)為當(dāng)然對象該屬性的值為開始(getPropName反射獲取),然后設(shè)置的值為終點(diǎn)。如果設(shè)置兩個(gè),則一個(gè)為開始、一個(gè)為結(jié)束~~~ 
動(dòng)畫更新的過程中,會(huì)不斷調(diào)用setPropName更新元素的屬性,所有使用ObjectAnimator更新某個(gè)屬性,必須得有g(shù)etter(設(shè)置一個(gè)屬性值的時(shí)候)和setter方法~
(2)如果你操作對象的該屬性方法里面,比如上例的setRotationX如果內(nèi)部沒有調(diào)用view的重繪,則你需要自己按照下面方式手動(dòng)調(diào)用。

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

(3)看了上面的例子,因?yàn)樵O(shè)置的操作的屬性只有一個(gè),那么如果我希望一個(gè)動(dòng)畫能夠讓View既可以縮小、又能夠淡出(3個(gè)屬性scaleX,scaleY,alpha),只使用ObjectAnimator咋弄?
想法是不是很不錯(cuò),可能會(huì)說使用AnimatorSet啊,這一看就是一堆動(dòng)畫塞一起執(zhí)行,但是我偏偏要用一個(gè)ObjectAnimator實(shí)例實(shí)現(xiàn)呢~下面看代碼:

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); 
    } 
  }); 
} 

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

這個(gè)例子就是想說明一下,有時(shí)候換個(gè)思路不要被API所約束,利用部分API提供的功能也能實(shí)現(xiàn)好玩的效果~~~
比如:你想實(shí)現(xiàn)拋物線的效果,水平方向100px/s,垂直方向加速度200px/s*s ,咋實(shí)現(xiàn)呢~~可以自己用ObjectAnimator試試~
(4)其實(shí)還有更簡單的方式,實(shí)現(xiàn)一個(gè)動(dòng)畫更改多個(gè)效果:使用propertyValuesHolder

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(); 
  } 


 4、ValueAnimator實(shí)現(xiàn)動(dòng)畫
和ObjectAnimator用法很類似,簡單看一下用view垂直移動(dòng)的動(dòng)畫代碼:

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

給你的感覺是不是,坑爹啊,這和ValueAnimator有毛線區(qū)別~但是仔細(xì)看,你看會(huì)發(fā)現(xiàn),沒有設(shè)置操作的屬性~~也就是說,上述代碼是沒有任何效果的,沒有指定屬性~
這就是和ValueAnimator的區(qū)別之處:ValueAnimator并沒有在屬性上做操作,你可能會(huì)問這樣有啥好處?我豈不是還得手動(dòng)設(shè)置?
好處:不需要操作的對象的屬性一定要有g(shù)etter和setter方法,你可以自己根據(jù)當(dāng)前動(dòng)畫的計(jì)算值,來操作任何屬性,記得上例的那個(gè)【我希望一個(gè)動(dòng)畫能夠讓View既可以縮小、又能夠淡出(3個(gè)屬性scaleX,scaleY,alpha)】嗎?其實(shí)就是這么個(gè)用法~
實(shí)例:
布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"  
  android:id="@+id/id_container" 
  
  > 
 
  <ImageView 
    android:id="@+id/id_ball" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/bol_blue" /> 
 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" > 
 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="verticalRun" 
      android:text="垂直" /> 
 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="paowuxian" 
      android:text="拋物線" /> 
 
  </LinearLayout> 
 
</RelativeLayout> 

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

/** 
   * 自由落體 
   * @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()); 
      } 
    }); 
  } 

與ObjectAnimator不同的就是我們自己設(shè)置元素屬性的更新~雖然多了幾行代碼,但是貌似提高靈活性~
下面再來一個(gè)例子,如果我希望小球拋物線運(yùn)動(dòng)【實(shí)現(xiàn)拋物線的效果,水平方向100px/s,垂直方向加速度200px/s*s 】,分析一下,貌似只和時(shí)間有關(guān)系,但是根據(jù)時(shí)間的變化,橫向和縱向的移動(dòng)速率是不同的,我們該咋實(shí)現(xiàn)呢?此時(shí)就要重寫TypeValue的時(shí)候了,因?yàn)槲覀冊跁r(shí)間變化的同時(shí),需要返回給對象兩個(gè)值,x當(dāng)前位置,y當(dāng)前位置:
代碼:

/** 
   * 拋物線 
   * @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); 
 
      } 
    }); 
  } 

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

2016419144549172.gif (756×434)

有木有兩個(gè)鐵球同時(shí)落地的感覺~~對,我應(yīng)該搞兩個(gè)球~~ps:物理公式要是錯(cuò)了,就當(dāng)沒看見哈
自定義TypeEvaluator傳入的泛型可以根據(jù)自己的需求,自己設(shè)計(jì)個(gè)Bean。
好了,我們已經(jīng)分別講解了ValueAnimator和ObjectAnimator實(shí)現(xiàn)動(dòng)畫;二者區(qū)別;如何利用部分API,自己更新屬性實(shí)現(xiàn)效果;自定義TypeEvaluator實(shí)現(xiàn)我們的需求;但是我們并沒有講如何設(shè)計(jì)插值,其實(shí)我覺得把,這個(gè)插值默認(rèn)的那一串實(shí)現(xiàn)類夠用了~~很少,會(huì)自己去設(shè)計(jì)個(gè)超級變態(tài)的~嗯~所以:略。
5、監(jiān)聽動(dòng)畫的事件
對于動(dòng)畫,一般都是一些輔助效果,比如我要?jiǎng)h除個(gè)元素,我可能希望是個(gè)淡出的效果,但是最終還是要?jiǎng)h掉,并不是你透明度沒有了,還占著位置,所以我們需要知道動(dòng)畫如何結(jié)束。
所以我們可以添加一個(gè)動(dòng)畫的監(jiān)聽:

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(); 
  } 

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

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); 
  } 
}); 

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

animator還有cancel()和end()方法:cancel動(dòng)畫立即停止,停在當(dāng)前的位置;end動(dòng)畫直接到最終狀態(tài)。
6、AnimatorSet的使用
實(shí)例:
布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent"  
  android:id="@+id/id_container" 
   
  > 
 
  <ImageView 
    android:id="@+id/id_ball" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerInParent="true" 
    android:src="@drawable/bol_blue" /> 
 
  <LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:orientation="horizontal" > 
 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="togetherRun" 
      android:text="簡單的多動(dòng)畫Together" /> 
 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="playWithAfter" 
      android:text="多動(dòng)畫按次序執(zhí)行" /> 
     
 
  </LinearLayout> 
 
</RelativeLayout> 

繼續(xù)玩球~
代碼:

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)畫同時(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(); 
  } 
} 

這里寫了兩個(gè)效果:
第一:使用playTogether兩個(gè)動(dòng)畫同時(shí)執(zhí)行,當(dāng)然還有playSequentially依次執(zhí)行~~
第二:如果我們有一堆動(dòng)畫,如何使用代碼控制順序,比如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ù)你寫的這一長串來決定先后的順序,所以麻煩你按照上面例子的寫法,多寫幾行:
效果圖:

相關(guān)文章

最新評論