1. 程式人生 > >Android動畫之屬性動畫(四)

Android動畫之屬性動畫(四)

一 前言

      在《》、《Android屬性動畫之ObjectAnimator和AnimatorSet》兩篇文章中學習了ValueAnimator、ObjectAnimator、AnimatorSet等類的使用,而且知道了屬性動畫通過改變一個物件的屬性值來來實現動畫效果,屬性動畫包含了以下幾個特性:    (1)持續時間(Duration) :主要用來定義動畫的持續時間,預設值為300ms,一般可以通過setDuration的函式進行設定
  (2)時間插值器(Time interpolation): 指定時間變化的百分比,就是當前流逝時間除以指定的持續時間,這個可以自定義,繼承Interpolator,重寫getInterpolation方法,系統給定的插值器有線性插值器、加法插值器、減法插值器、峰值插值器(先進行加法達到一定高度再減)。

  (3)重複次數和行為(Repeat count and behavior) :指定動畫的執行次數和動畫的重複模式,可以通過setRepeatCount函式設定動畫重複的次數,通過setRepeatMode函式設定動畫重複的模式。
  (4)動畫集(Animator sets) :可以把多個動畫放到一個集合中,可以使它們同時執行或者依次執行,或者指定它們直接的順序和延遲,相關的函式有play、with、before、after。

  (5)Frame refresh delay(幀重新整理延遲) :可以指定如何去重新整理動畫的幀,預設是每10ms重新整理一次,這個重新整理也取決於系統的繁忙程度。

二 屬性動畫相關的類

Animator

      這個可以說是屬性動畫的鼻祖,除了ViewPropertyAnimator之外,其餘都是由Animator派生出來的,例如上面的ValueAnimator、ObjectAnimator、AnimatorSet最終都是繼承的Animator。

(1)它幾個重要的成員變數

ArrayList<AnimatorListener> mListeners = null;
ArrayList<AnimatorPauseListener> mPauseListeners = null;

說明,mListeners為AnimatorListener型別,

AnimatorListener是一個回撥介面其原始碼如下:

public static interface AnimatorListener {   
         //動畫開始回撥  
        void onAnimationStart(Animator animation); 
        //動畫結束時回撥 
        void onAnimationEnd(Animator animation);
        //動畫取消時回撥  
        void onAnimationCancel(Animator animation);
        //動畫重複時回撥  
        void onAnimationRepeat(Animator animation);
}

mListeners用來儲存屬性動畫所用的AnimatorListener例項。

mPauseListenersAnimatorPauseListener型別,AnimatorPauseListener是一個回撥介面其原始碼如下:
public static interface AnimatorPauseListener {
        //動畫暫停時回撥
        void onAnimationPause(Animator animation);
         //動畫重啟時回撥
        void onAnimationResume(Animator animation);
}

(2)常見方法

public void start()
動畫啟動
public void cancel()
動畫取消
public void end()
動畫結束
public void pause()
動畫暫停
public void resume()
動畫重啟
public void addListener(AnimatorListener listener)
新增AnimatorListener監聽
public void removeListener(AnimatorListener listener)
移除AnimatorListener監聽
public void setupStartValues()
設定屬性起始值
public void setupEndValues()
設定屬性結束值
public void setTarget(@Nullable Object target)
設定動畫的作用物件

(3)抽象方法

public abstract boolean isRunning()
返回動畫執行的狀態
public abstract void setInterpolator(TimeInterpolator value)
設定時間插值器
public abstract long getDuration()
獲取動畫時間間隔
public abstract Animator setDuration(long duration)
設定動畫時間間隔
public abstract long getStartDelay()
獲取動畫啟動的延遲時間
public abstract void setStartDelay(long startDelay)
設定動畫啟動的延遲時間
public void addPauseListener(AnimatorPauseListener listener)
新增AnimatorPauseListener監聽
public void removePauseListener(AnimatorPauseListener listener)
移除AnimatorPauseListener監聽

AnimatorSet

     AnimatorSet直接繼承Animator,它用來實現屬性的組合動畫,它可以組合多個ValueAnimator或者ObjectAnimator,它是以Builder模式進行動畫新增的,首先呼叫play函式新增一個主動畫,返回Builder,再者可以通過with()、after()、before()函式新增其它的動畫。更多參見《Android屬性動畫之ObjectAnimator和AnimatorSet》。

ValueAnimator

     ValueAnimator繼承於AnimatorSet,它監聽屬性動畫的整個過程,它本身沒有動畫的效果,需要自己新增AnimatorUpdateListener監聽來實現動畫的效果,AnimatorUpdateListener原始碼如下:

public static interface AnimatorUpdateListener {
        void onAnimationUpdate(ValueAnimator animation);
}

說明,AnimatorUpdateListener非常簡單隻有一個方法onAnimationUpdate需要實現,在這個方法中可以實現自己想要的動畫,更多參見

ObjectAnimator

       ObjectAnimator繼承於ValueAnimator,它作用一個物件,需要指定該物件具體的屬性,本身實現動畫效果,不必自己去實現,只需指出具體的屬性就可以了,更多參見《Android屬性動畫之ObjectAnimator和AnimatorSet》。

ViewPropertyAnimator

ViewPropertyAnimator用於實現View動畫,需要注意的是它的獲取物件例項的方法是在View類中,如下:

View#animate:
public ViewPropertyAnimator animate() {
        if (mAnimator == null) {
            mAnimator = new ViewPropertyAnimator(this);
        }
        return mAnimator;
}

使用示例:

ViewPropertyAnimator viewPropertyAnimator = img.animate();
viewPropertyAnimator.translationX(200).scaleX(2).setDuration(2000).start();

三 使用XML

      在屬性動畫中使用XML檔案,就是自定義一些XML檔案,在這些XML檔案中使用objectAnimator標籤來實現動畫效果,接下來直接上示例來看看屬性動畫的XML檔案是如何使用的,示例說明:使用AdapterViewFlipper控制元件來實現,當點選next按鈕時,圖片從左進右出,點選previous按鈕時,圖片右進左出:

1 建立XML檔案

首先在資原始檔res下建立目錄animator,注意這裡建立的不是animation動畫使用的anim目錄,否則的話很容易找不到objectAnimator標籤或其它錯誤,圖片從左進右出和圖片右進左出,這裡需要四個XML檔案,它們依次是:

anim_left_enter.xml:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:propertyName="x"
    android:valueType="floatType"
    android:valueFrom="-1500"
    android:valueTo="0"
    android:duration="600">
</objectAnimator>
anim_left_exit.xml:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <objectAnimator
        android:propertyName="x"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="1500"
        android:duration="600"/>
</objectAnimator>
anim_right_enter.xml:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:propertyName="x"
    android:valueType="floatType"
    android:valueFrom="1500"
    android:valueTo="0"
    android:duration="600">
</objectAnimator>
anim_right_exit.xml:
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
     android:interpolator="@android:anim/accelerate_decelerate_interpolator">
    <objectAnimator
        android:propertyName="x"
        android:valueType="floatType"
        android:valueFrom="0"
        android:valueTo="-1500"
        android:duration="600"/>
</objectAnimator>

      說明,上面出現了兩種情況,一直接使用objectAnimator標籤,二在使用objectAnimator標籤(父標籤)後,又巢狀一個objectAnimator標籤(子標籤),在這裡沒有特殊的含義,僅僅想證明兩個情況都可以使用,還有的資料說父標籤使用set、子標籤使用objectAnimator標籤,這情況,我嘗試過,執行時會出現異常,不知道別人是怎麼成功,(我使用的虛擬機器是Android 5.0系統)。

      還有一點,感覺需要注意下,如果你先前使用父標籤是set,那麼即使換過來(父標籤換為objectAnimator),儲存遠行,可能還會提示遠行異常資訊如android.animation.AnimatorSet cannot be cast to android.animation.ObjectAnimator等,那麼就請clear、rebuild下你的專案吧,再次執行,看看有沒有異常,還是會有的話,那就儲存內容重啟AS吧(我也很絕望!我就是被這麼坑過來的,以此作此總結,以防更多的小夥伴被坑),好閒話少說繼續看。

2 佈置介面

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:gravity="center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--屬性  android:loopViews="true" 設定動畫可迴圈播放-->
    <AdapterViewFlipper
        android:id="@+id/av_flipper"
        android:layout_width="match_parent"
        android:loopViews="true"
        android:layout_weight="1"
        android:layout_height="wrap_content">
    </AdapterViewFlipper>
    <LinearLayout
        android:layout_width="match_parent"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:orientation="horizontal">
        <Button
            android:text="下一個"
            android:onClick="next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="上一個"
            android:onClick="previous"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <Button
            android:text="自動播放"
            android:onClick="auto"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>

說明,這裡很簡單,一個輪番顯示圖片的AdapterViewFlipper控制元件,即三個按鈕,分別是顯示下一張、上一張圖片,及其自動輪番播放按鈕。

3 自定義介面卡

public class FliperAdapter extends BaseAdapter {

    private int[] imgIds;
    private Context context;

    public FliperAdapter(Context context, int[] imgIds) {
        this.imgIds = imgIds;
        this.context  = context;
    }

    @Override
    public int getCount() {
        return imgIds == null ? 0 : imgIds.length;
    }

    @Override
    public Object getItem(int position) {
        return imgIds[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolde holde = null;
        if (convertView == null){
            convertView = LayoutInflater.from(context).inflate(R.layout.item_fliper,parent,false);
            holde = new ViewHolde();
            holde.imageView = (ImageView) convertView.findViewById(R.id.item_img);
            convertView.setTag(holde);
        }else {
            holde = (ViewHolde) convertView.getTag();
        }
        holde.imageView.setImageResource(imgIds[position]);
        return convertView;
    }


    static class ViewHolde{
        public ImageView imageView;
    }
}

4 Activity中使用XML動畫

public class AnimatorXMLTestActivityextends Activity  {private AdapterViewFlipper avFlipper;//圖片資源publicint[] imgIds ={R.mipmap.pic_1,R.mipmap.pic_2,R.mipmap.pic_3,R.mipmap.pic_4,R.mipmap.pic_5};@OverrideprotectedvoidonCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fliper);
        FliperAdapter adapter  =new FliperAdapter(this,imgIds);
        avFlipper =(AdapterViewFlipper) findViewById(R.id.av_flipper);//設定動畫自動播放的時間間隔
        avFlipper.setFlipInterval(1000);//設定顯示第一個View時是否使用動畫
        avFlipper.setAnimateFirstView(true);//設定介面卡
        avFlipper.setAdapter(adapter);//        ObjectAnimator  objectAnimator = ObjectAnimator.ofFloat(avFlipper,"x",0f,1f);//        ObjectAnimator  objectAnimator2 = ObjectAnimator.ofFloat(avFlipper,"x",1f,0f);}/**     * 下一個     * @param view     */public
            
           

相關推薦

Android動畫屬性動畫

一 前言       在《》、《Android屬性動畫之ObjectAnimator和AnimatorSet》兩篇文章中學習了ValueAnimator、ObjectAnimator、Animator

Android動畫屬性動畫

前言        前面我們已經完整的講述了屬性動畫的實現,我們已經學會了怎麼實現動畫,如果沒有屬性我們也學會了怎麼新增屬性,還學習了用ValueAnimator來實現動畫。 Evaluator        這裡我們來學習剩下的屬性,首先我們來看看E

Unity3DMecanim動畫系統學習筆記:Animation State

大致 面板 輸入 jpg any 動畫播放 速度 nsf 顯示 動畫的設置 我們先看看Animation Clip的一些設置: Loop time:動畫是否循環播放。 下面出現了3個大致一樣的選項: Root Transform Rotation:表示為播放動畫

Android架構】基於MVP模式的Retrofit2+RXjava封裝常見問題

###先回顧下之前的 【Android架構】基於MVP模式的Retrofit2+RXjava封裝(一) 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之檔案下載(二) 【Android架構】基於MVP模式的Retrofit2+RXjava封裝之檔案上傳(三)

Android動畫屬性動畫基礎用法

在上一篇Android動畫基礎之補間動畫與逐幀動畫 ,我們複習了Android的基礎動畫Tweened Animation、Frame Animation,同時指出他們的缺陷,改變是View的顯示效果而不是View本身的屬性,還有我們不多不少會看多人家的APP有一些很酷炫的動畫效果,但是

Android 動畫屬性動畫

import android.animation.ValueAnimator; import android.animation.ValueAnimator.AnimatorUpdateListener; import android.app.Activity;

Android 動畫屬性動畫- Interpolator(內插器)

Android動畫系列: 介紹 對於Interpolator(內插器),寫過動畫的都不會陌生,其本身看做是一個時間計算器,用於定義動畫的執行的速率。 public class ValueAnimator extends Animator impleme

AndroidFresco框架--ImagePipeline的呼叫和使用

之前大致把ImagePipeline的配置和底層實現都講了一下,這一篇來重點講一下我們在傳送圖片請求的時候是怎麼把請求傳給ImagePipeline的,以及我們如何自己直接對ImagePipeline例項進行請求,記憶體管理等操作。SimpleDraweeView中Image

爬蟲庫BeautifulSoup學習

所有 字符串 判斷 href gin int 過濾器 amp link 探索文檔樹: find_all(name,attrs,recursive,text,**kwargs) 方法搜索當前tag的所有tag子節點,並判斷是否符合過濾器的條件 1、name參數,可

轉載:monkeyrunnereclipse中運行monkeyrunner腳本環境搭建

導包 rep 是把 body tle cnblogs 9.png 解決方法 align 轉載自:lynnLi 的monkeyrunner之eclipse中運行monkeyrunner腳本之環境搭建(四) monkeyrunner腳本使用Python語法編寫,但它實際上是通

MVC實戰排球計分—— View設計與實現

service family 角色 元素 需要 rom 之前 con xsl (view)視圖 視圖是用戶看到並與之交互的界面。對老式的Web應用程序來說,視圖就是由HTML元素組成的界面,在新式的Web應用程序中,HTML依舊在視圖中扮演著重要的角色,但一些新的技術已層出

MVC系列博客排球計分視圖的實現

ont shtml dev 相關 control 沒有 mage evel 技術分享 Views 文件夾 Views 文件夾存儲的是與應用程序顯示(用戶界面)相關的文件(HTML 文件)。根據所采用的語言內容,這些文件可能擴展名可能是 html、asp、aspx、cshtm

android深入設計模式托付模式

-h listen back != new 聚合 string static data- (一)托付模式簡單介紹 托付模式是主要的設計模式之中的一個。托付。即是讓還有一個對象幫你做事情。 更多的模式,如狀態模式、策略模式、訪問者模式本質上是在更特殊的場合採用了托

Appium python自動化測試系列認識Appium

ndt require 差異 make python自動化 復雜 appium launched interface ?4.1界面認識 在之前安裝appium的時候說過我們有兩種方法安裝,也就有兩種結果,一種是有界面的,一種是沒有界面的,首先我們先講一下有界面的,以及界面有

Java基礎加強並發synchronized關鍵字

inter span static www name play demo try 繼續 並發系列參考文章http://www.cnblogs.com/skywang12345/p/3323085.html#3907193 synchronized原理 在java中,每一

構建NetCore應用框架實戰篇:BitAdminCore框架1.0登錄功能細化及技術選型

1.0 dmi 也會 繼承 blank bit 技術選型 cor 我會 本篇承接上篇內容,如果你不小心點擊進來,建議從第一篇開始完整閱讀,文章內容繼承性連貫性。 構建NetCore應用框架之實戰篇系列 一、BitAdminCore框架1.0版本 1、1.0版本是指

處理器緩存

nag AD 51cto flag 就是 讀取 RoCE Oz TE 今天我們來看看緩存。在我們平時的生活中,知道處理器和內存是不同的。它們所使用的半導體器件工藝不同,工藝的差異導致了處理器與內存的速度差異。處理器的器件比內存是要好很多的,因此它的容量就勢必

springcloud入門斷路器Hystrix

-c 例子 控制臺 介紹 tro you work 不同的 實現 什麽是斷路器 斷路器模式源於Martin Fowler的Circuit Breaker一文。“斷路器”本身是一種開關裝置,用於在電路上保護線路過載,當線路中有電器發生短路時,“斷路器”能夠及時的切斷故障電路,

從零開始學 Web Vue.jsVue的Ajax請求和跨域

在線安裝 配置 name php文件 splay .json alert 參考 1.0 大家好,這裏是「 從零開始學 Web 系列教程 」,並在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公眾號:Web前端

linux 核心模組程式設計模組引數

通過巨集module_param指定模組引數,模組引數用於在載入模組時傳遞給模組。 module_param(name, type, perm) name是模組引數的名字 type是這個引數的型別,常見值:bool、int、charp(字串型) perm是模組