日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Hongyang 生命不息,奋斗不止,万事起于忽微,量变引起质变 目录视图 摘要视图 订阅 Android 属性动画(Property Animation) 完全解析 (上)

發(fā)布時間:2024/1/18 48 豆豆

轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/lmj623565791/article/details/38067475

1、概述

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

2、相關(guān)API

Property Animation故名思議就是通過動畫的方式改變對象的屬性了,我們首先需要了解幾個屬性:

Duration動畫的持續(xù)時間,默認(rèn)300ms。

Time interpolation:時間差值,乍一看不知道是什么,但是我說LinearInterpolator、AccelerateDecelerateInterpolator,大家一定知道是干嘛的了,定義動畫的變化率。

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

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

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

相關(guān)的類

ObjectAnimator ?動畫的執(zhí)行類,后面詳細(xì)介紹

ValueAnimator 動畫的執(zhí)行類,后面詳細(xì)介紹?

AnimatorSet 用于控制一組動畫的執(zhí)行:線性,一起,每個動畫的先后執(zhí)行等。

AnimatorInflater 用戶加載屬性動畫的xml文件

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

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

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

3、ObjectAnimator實現(xiàn)動畫

之所以選擇ObjectAnimator為第一個~~是因為,這個實現(xiàn)最簡單~~一行代碼,秒秒鐘實現(xiàn)動畫,下面看個例子:
布局文件:

[html]? view plain copy
  • <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代碼:

    [java]? view plain copy
  • 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();??
  • ????}??
  • ??
  • }??

  • 效果:

    是不是一行代碼就能實現(xiàn)簡單的動畫~~

    對于ObjectAnimator

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

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

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

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

    [java]? view plain copy
  • anim.addUpdateListener(new?AnimatorUpdateListener()??
  • ????????{??
  • ????????????@Override??
  • ????????????public?void?onAnimationUpdate(ValueAnimator?animation)??
  • ????????????{??
  • //??????????????view.postInvalidate();??
  • //??????????????view.invalidate();??
  • ????????????}??
  • ????????});??
  • 3、看了上面的例子,因為設(shè)置的操作的屬性只有一個,那么如果我希望一個動畫能夠讓View既可以縮小、又能夠淡出(3個屬性scaleX,scaleY,alpha),只使用ObjectAnimator咋弄?

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

    [java]? view plain copy
  • 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è)置屬性的那個字符串,隨便寫一個該對象沒有的屬性,就是不管~~咱們只需要它按照時間插值和持續(xù)時間計算的那個值,我們自己手動調(diào)用~

    效果:

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

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

    4、其實還有更簡單的方式,實現(xiàn)一個動畫更改多個效果:使用propertyValuesHolder

    [java]? view plain copy
  • 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實現(xiàn)動畫

    和ObjectAnimator用法很類似,簡單看一下用view垂直移動的動畫代碼:

    [java]? view plain copy
  • public?void?verticalRun(View?view)??
  • ????{??
  • ????????ValueAnimator?animator?=?ValueAnimator.ofFloat(0,?mScreenHeight??
  • ????????????????-?mBlueBall.getHeight());??
  • ????????animator.setTarget(mBlueBall);??
  • ????????animator.setDuration(1000).start();??
  • ????}??

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

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

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

    實例:

    布局文件:

    [html]? view plain copy
  • <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>??
  • 左上角一個小球,底部兩個按鈕~我們先看一個自由落體的代碼:

    [java]? view plain copy
  • /**?
  • ?????*?自由落體?
  • ?????*?@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è)置元素屬性的更新~雖然多了幾行代碼,但是貌似提高靈活性~

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

    代碼:

    [java]? view plain copy
  • /**?
  • ?????*?拋物線?
  • ?????*?@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);??
  • ??
  • ????????????}??
  • ????????});??
  • ????}??
  • 可以看到,因為ofInt,ofFloat等無法使用,我們自定義了一個TypeValue,每次根據(jù)當(dāng)前時間返回一個PointF對象,(PointF和Point的區(qū)別就是x,y的單位一個是float,一個是int;RectF,Rect也是)PointF中包含了x,y的當(dāng)前位置~然后我們在監(jiān)聽器中獲取,動態(tài)設(shè)置屬性:

    效果圖:


    有木有兩個鐵球同時落地的感覺~~對,我應(yīng)該搞兩個球~~ps:物理公式要是錯了,就當(dāng)沒看見哈

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

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

    5、監(jiān)聽動畫的事件

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

    所以我們可以添加一個動畫的監(jiān)聽:

    [java]? view plain copy
  • 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)聽動畫的開始、結(jié)束、被取消、重復(fù)等事件~但是有時候會覺得,我只要知道結(jié)束就行了,這么長的代碼我不能接收,那你可以使用AnimatorListenerAdapter

    [java]? view plain copy
  • 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接口,然后空實現(xiàn)了所有的方法~

    效果圖:


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

    6、AnimatorSet的使用

    實例:

    布局文件:

    [html]? view plain copy
  • <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="簡單的多動畫Together"?/>??
  • ??
  • ????????<Button??
  • ????????????android:layout_width="wrap_content"??
  • ????????????android:layout_height="wrap_content"??
  • ????????????android:onClick="playWithAfter"??
  • ????????????android:text="多動畫按次序執(zhí)行"?/>??
  • ??????????
  • ??
  • ????</LinearLayout>??
  • ??
  • </RelativeLayout>??

  • 繼續(xù)玩球~

    代碼:

    [java]? view plain copy
  • 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());??
  • ????????//兩個動畫同時執(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); ?
  • 總結(jié)

    以上是生活随笔為你收集整理的Hongyang 生命不息,奋斗不止,万事起于忽微,量变引起质变 目录视图 摘要视图 订阅 Android 属性动画(Property Animation) 完全解析 (上)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

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