欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android中編寫(xiě)屬性動(dòng)畫(huà)PropertyAnimation的進(jìn)階實(shí)例

 更新時(shí)間:2016年04月19日 15:01:54   作者:鴻洋_  
這篇文章主要介紹了Android中編寫(xiě)屬性動(dòng)畫(huà)PropertyAnimation的進(jìn)階實(shí)例,包括一些縮放和淡入淡出效果的設(shè)計(jì),強(qiáng)大且不算復(fù)雜,需要的朋友可以參考下

0、基礎(chǔ)回顧
PropertyAnimation,屬性動(dòng)畫(huà),顧名思義就是利用對(duì)象的屬性變化形成動(dòng)畫(huà)的效果。屬性動(dòng)畫(huà)的類可以用Animator這個(gè)抽象類來(lái)表示,通常使用它的子類:AnimatorSet和ValueAnimator,同時(shí)ValueAnimator有兩個(gè)子類分別是ObjectAniamtor和TimeAnimator。
定義屬性動(dòng)畫(huà)的XML資源的時(shí)候通常可以是如下三個(gè)元素之一作為根元素:
<set>元素:該資源元素代表的是AniamtorSet類,這個(gè)類可以包含<set>,<objectAniamtor>,<animator>三個(gè)子元素。
<objectAnimator>元素:用于定義objectAniamtor類。
<animator>元素:用于定義ValueAnimator類。
比如說(shuō)這里一個(gè)資源文件的定義如下:

<set android:ordering="[together|sequentially]">
 
  <objectAnimator
 
      android:propertyName="string"
      android:duration="int"
      android:valueFrom="float|int|color"
      android:valueTo="float|int|color"
      android:startOffset="int"
      android:repeatCount="int"
      android:interpolator=""
      android:repeatMode="[reapeat|reverse]"
      android:valueType="[intType|floatType]"/>
  
  <animator 
      
      android:duration="int"
      android:valueFrom="float|int|color"
      android:valueTo="float|int|color"
      android:startOffset="int"
      android:repeatCount="int"
      android:interpolator=""
      android:repeatMode="[reapeat|reverse]"
      android:valueType="[intType|floatType]"/>
 
  <set>
    ....
  </set>
 
</set>

屬性文件通常保存在animator文件夾下面。

1、如何使用xml文件來(lái)創(chuàng)建屬性動(dòng)畫(huà)
大家肯定都清楚,View Animator 、Drawable Animator都可以在anim文件夾下創(chuàng)建動(dòng)畫(huà),然后在程序中使用,甚至在Theme中設(shè)置為屬性值。當(dāng)然了,屬性動(dòng)畫(huà)其實(shí)也可以在文件中聲明:
首先在res下建立animator文件夾,然后建立res/animator/scalex.xml

<?xml version="1.0" encoding="utf-8"?> 
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" 
  android:duration="1000" 
  android:propertyName="scaleX" 
  android:valueFrom="1.0" 
  android:valueTo="2.0" 
  android:valueType="floatType" > 
</objectAnimator> 

代碼:

public void scaleX(View view) 
  { 
    // 加載動(dòng)畫(huà) 
    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scalex); 
    anim.setTarget(mMv); 
    anim.start(); 
  } 

使用AnimatorInflater加載動(dòng)畫(huà)的資源文件,然后設(shè)置目標(biāo),就ok~~是不是很簡(jiǎn)單,這只是單純橫向的放大一倍~
如果我希望縱向與橫向同時(shí)縮放呢?則可以怎么定義屬性文件:

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
  android:ordering="together" > 
 
  <objectAnimator 
    android:duration="1000" 
    android:propertyName="scaleX" 
    android:valueFrom="1" 
    android:valueTo="0.5" > 
  </objectAnimator> 
  <objectAnimator 
    android:duration="1000" 
    android:propertyName="scaleY" 
    android:valueFrom="1" 
    android:valueTo="0.5" > 
  </objectAnimator> 
 
</set> 

使用set標(biāo)簽,有一個(gè)orderring屬性設(shè)置為together,【還有另一個(gè)值:sequentially(表示一個(gè)接一個(gè)執(zhí)行)】。
上篇博客中忽略了一個(gè)效果,就是縮放、反轉(zhuǎn)等都有中心點(diǎn)或者軸,默認(rèn)中心縮放,和中間對(duì)稱線為反轉(zhuǎn)線,所以我決定這個(gè)橫向,縱向縮小以左上角為中心點(diǎn):
代碼:

// 加載動(dòng)畫(huà) 
    Animator anim = AnimatorInflater.loadAnimator(this, R.animator.scale); 
    mMv.setPivotX(0); 
    mMv.setPivotY(0); 
    //顯示的調(diào)用invalidate 
    mMv.invalidate(); 
    anim.setTarget(mMv); 
    anim.start(); 

很簡(jiǎn)單,直接給View設(shè)置pivotX和pivotY,然后調(diào)用一下invalidate,就ok了。
下面看效果圖:

2016419145721361.gif (733×398)

好了,通過(guò)寫(xiě)xml聲明動(dòng)畫(huà),使用set嵌套set,結(jié)合orderring屬性,也基本可以實(shí)現(xiàn)任何動(dòng)畫(huà)~~上面也演示了pivot的設(shè)置。

2、布局動(dòng)畫(huà)(Layout Animations)
主要使用LayoutTransition為布局的容器設(shè)置動(dòng)畫(huà),當(dāng)容器中的視圖層次發(fā)生變化時(shí)存在過(guò)渡的動(dòng)畫(huà)效果。
基本代碼為:

LayoutTransition transition = new LayoutTransition(); 
  transition.setAnimator(LayoutTransition.CHANGE_APPEARING, 
      transition.getAnimator(LayoutTransition.CHANGE_APPEARING)); 
  transition.setAnimator(LayoutTransition.APPEARING, 
      null); 
  transition.setAnimator(LayoutTransition.DISAPPEARING, 
      null); 
  transition.setAnimator(LayoutTransition.CHANGE_DISAPPEARING, 
      null); 
  mGridLayout.setLayoutTransition(transition); 

過(guò)渡的類型一共有四種:
(1)LayoutTransition.APPEARING 當(dāng)一個(gè)View在ViewGroup中出現(xiàn)時(shí),對(duì)此View設(shè)置的動(dòng)畫(huà)
(2)LayoutTransition.CHANGE_APPEARING 當(dāng)一個(gè)View在ViewGroup中出現(xiàn)時(shí),對(duì)此View對(duì)其他View位置造成影響,對(duì)其他View設(shè)置的動(dòng)畫(huà)
(3)LayoutTransition.DISAPPEARING  當(dāng)一個(gè)View在ViewGroup中消失時(shí),對(duì)此View設(shè)置的動(dòng)畫(huà)
(4)LayoutTransition.CHANGE_DISAPPEARING 當(dāng)一個(gè)View在ViewGroup中消失時(shí),對(duì)此View對(duì)其他View位置造成影響,對(duì)其他View設(shè)置的動(dòng)畫(huà)
(5)LayoutTransition.CHANGE 不是由于View出現(xiàn)或消失造成對(duì)其他View位置造成影響,然后對(duì)其他View設(shè)置的動(dòng)畫(huà)。
注意動(dòng)畫(huà)到底設(shè)置在誰(shuí)身上,此View還是其他View。
好了下面看一個(gè)綜合的例子:
布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  xmlns:tools="http://schemas.android.com/tools" 
  android:id="@+id/id_container" 
  android:layout_width="match_parent" 
  android:layout_height="match_parent" 
  android:orientation="vertical" > 
 
  <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:onClick="addBtn" 
    android:text="addBtns" /> 
 
  <CheckBox 
    android:id="@+id/id_appear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="true" 
    android:text="APPEARING" /> 
 
  <CheckBox 
    android:id="@+id/id_change_appear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="true" 
    android:text="CHANGE_APPEARING" /> 
 
  <CheckBox 
    android:id="@+id/id_disappear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="true" 
    android:text="DISAPPEARING" /> 
 
  <CheckBox 
     android:id="@+id/id_change_disappear" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:checked="true" 
    android:text="CHANGE_DISAPPEARING " /> 
 
</LinearLayout> 

代碼:

package com.example.zhy_property_animation; 
 
import android.animation.LayoutTransition; 
import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.CompoundButton; 
import android.widget.CompoundButton.OnCheckedChangeListener; 
import android.widget.GridLayout; 
 
public class LayoutAnimaActivity extends Activity implements 
    OnCheckedChangeListener 
{ 
  private ViewGroup viewGroup; 
  private GridLayout mGridLayout; 
  private int mVal; 
  private LayoutTransition mTransition; 
 
  private CheckBox mAppear, mChangeAppear, mDisAppear, mChangeDisAppear; 
 
  @Override 
  public void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_animator); 
    viewGroup = (ViewGroup) findViewById(R.id.id_container); 
 
    mAppear = (CheckBox) findViewById(R.id.id_appear); 
    mChangeAppear = (CheckBox) findViewById(R.id.id_change_appear); 
    mDisAppear = (CheckBox) findViewById(R.id.id_disappear); 
    mChangeDisAppear = (CheckBox) findViewById(R.id.id_change_disappear); 
 
    mAppear.setOnCheckedChangeListener(this); 
    mChangeAppear.setOnCheckedChangeListener(this); 
    mDisAppear.setOnCheckedChangeListener(this); 
    mChangeDisAppear.setOnCheckedChangeListener(this); 
 
    // 創(chuàng)建一個(gè)GridLayout 
    mGridLayout = new GridLayout(this); 
    // 設(shè)置每列5個(gè)按鈕 
    mGridLayout.setColumnCount(5); 
    // 添加到布局中 
    viewGroup.addView(mGridLayout); 
    //默認(rèn)動(dòng)畫(huà)全部開(kāi)啟 
    mTransition = new LayoutTransition(); 
    mGridLayout.setLayoutTransition(mTransition); 
 
  } 
 
  /** 
   * 添加按鈕 
   * 
   * @param view 
   */ 
  public void addBtn(View view) 
  { 
    final Button button = new Button(this); 
    button.setText((++mVal) + ""); 
    mGridLayout.addView(button, Math.min(1, mGridLayout.getChildCount())); 
    button.setOnClickListener(new OnClickListener() 
    { 
 
      @Override 
      public void onClick(View v) 
      { 
        mGridLayout.removeView(button); 
      } 
    }); 
  } 
 
  @Override 
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
  { 
    mTransition = new LayoutTransition(); 
    mTransition.setAnimator( 
        LayoutTransition.APPEARING, 
        (mAppear.isChecked() ? mTransition 
            .getAnimator(LayoutTransition.APPEARING) : null)); 
    mTransition 
        .setAnimator( 
            LayoutTransition.CHANGE_APPEARING, 
            (mChangeAppear.isChecked() ? mTransition 
                .getAnimator(LayoutTransition.CHANGE_APPEARING) 
                : null)); 
    mTransition.setAnimator( 
        LayoutTransition.DISAPPEARING, 
        (mDisAppear.isChecked() ? mTransition 
            .getAnimator(LayoutTransition.DISAPPEARING) : null)); 
    mTransition.setAnimator( 
        LayoutTransition.CHANGE_DISAPPEARING, 
        (mChangeDisAppear.isChecked() ? mTransition 
            .getAnimator(LayoutTransition.CHANGE_DISAPPEARING) 
            : null)); 
    mGridLayout.setLayoutTransition(mTransition); 
  } 
} 

效果圖:

2016419145837034.gif (422×670)

動(dòng)畫(huà)有點(diǎn)長(zhǎng),耐心點(diǎn)看,一定要注意,是對(duì)當(dāng)前View還是其他Views設(shè)置的動(dòng)畫(huà)。
當(dāng)然了動(dòng)畫(huà)支持自定義,還支持設(shè)置時(shí)間,比如我們修改下,添加的動(dòng)畫(huà)為:

mTransition.setAnimator(LayoutTransition.APPEARING, (mAppear 
        .isChecked() ? ObjectAnimator.ofFloat(this, "scaleX", 0, 1) 
        : null)); 

則效果為:

2016419145900874.gif (406×346)

原本的淡入,變成了寬度從中間放大的效果~~是不是還不錯(cuò)~~

3、View的anim方法
在SDK11的時(shí)候,給View添加了animate方法,更加方便的實(shí)現(xiàn)動(dòng)畫(huà)效果。
布局文件:

<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"  
  > 
 
  <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="viewAnim" 
      android:text="View Anim" /> 
 
    <Button 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:onClick="propertyValuesHolder" 
      android:text="PropertyValuesHolder " /> 
     
 
  </LinearLayout> 
 
</RelativeLayout> 

代碼:

package com.example.zhy_property_animation; 
 
import android.animation.ObjectAnimator; 
import android.animation.PropertyValuesHolder; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.View; 
import android.widget.ImageView; 
 
public class ViewAnimateActivity extends Activity 
{ 
  protected static final String TAG = "ViewAnimateActivity"; 
 
  private ImageView mBlueBall; 
  private float mScreenHeight; 
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) 
  { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.view_animator); 
 
    DisplayMetrics outMetrics = new DisplayMetrics(); 
    getWindowManager().getDefaultDisplay().getMetrics(outMetrics); 
    mScreenHeight = outMetrics.heightPixels; 
    mBlueBall = (ImageView) findViewById(R.id.id_ball); 
 
  } 
 
  public void viewAnim(View view) 
  { 
    // need API12 
    mBlueBall.animate()// 
        .alpha(0)// 
        .y(mScreenHeight / 2).setDuration(1000) 
        // need API 12 
        .withStartAction(new Runnable() 
        { 
          @Override 
          public void run() 
          { 
            Log.e(TAG, "START"); 
          } 
          // need API 16 
        }).withEndAction(new Runnable() 
        { 
 
          @Override 
          public void run() 
          { 
            Log.e(TAG, "END"); 
            runOnUiThread(new Runnable() 
            { 
              @Override 
              public void run() 
              { 
                mBlueBall.setY(0); 
                mBlueBall.setAlpha(1.0f); 
              } 
            }); 
          } 
        }).start(); 
  }  
}         

                                                                                                       

簡(jiǎn)單的使用mBlueBall.animate().alpha(0).y(mScreenHeight / 2).setDuration(1000).start()就能實(shí)現(xiàn)動(dòng)畫(huà)~~不過(guò)需要SDK11,此后在SDK12,SDK16又分別添加了withStartAction和withEndAction用于在動(dòng)畫(huà)前,和動(dòng)畫(huà)后執(zhí)行一些操作。當(dāng)然也可以.setListener(listener)等操作。
使用ObjectAnimator實(shí)現(xiàn)上面的變化,我們可以使用:PropertyValueHolder

PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 
      0f, 1f); 
  PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 0, 
      mScreenHeight / 2, 0); 
  ObjectAnimator.ofPropertyValuesHolder(mBlueBall, pvhX, pvhY).setDuration(1000).start(); 

效果與上面一樣。
運(yùn)行結(jié)果:

2016419150021670.gif (793×406)

相關(guān)文章

  • Android中處理apple-touch-icon詳解

    Android中處理apple-touch-icon詳解

    這篇文章主要介紹了Android中處理apple-touch-icon詳解,在Android如果想把網(wǎng)頁(yè)放到桌面上,也需要使用Touch Icon圖標(biāo)才可實(shí)現(xiàn),本文即講解Android中處理Touch Icon的一些知識(shí),需要的朋友可以參考下
    2015-01-01
  • Android圖片緩存之Lru算法(二)

    Android圖片緩存之Lru算法(二)

    LRU緩存簡(jiǎn)單的說(shuō)就是緩存一定量的數(shù)據(jù),當(dāng)超過(guò)設(shè)定的閾值時(shí)就把一些過(guò)期的數(shù)據(jù)刪除掉,這篇文章主要介紹了Android圖片緩存Lru算法,感興趣的小伙伴們可以參考一下
    2016-08-08
  • Android listview定位到上次顯示的位置的實(shí)現(xiàn)方法

    Android listview定位到上次顯示的位置的實(shí)現(xiàn)方法

    這篇文章主要介紹了Android listview定位到上次顯示的位置的實(shí)現(xiàn)方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-08-08
  • Android 通過(guò)Intent使用Bundle傳遞對(duì)象詳細(xì)介紹

    Android 通過(guò)Intent使用Bundle傳遞對(duì)象詳細(xì)介紹

    這篇文章主要介紹了Android 通過(guò)Intent使用Bundle傳遞對(duì)象詳細(xì)介紹的相關(guān)資料,并附實(shí)例代碼講解,具有一定的參考價(jià)值,需要的朋友可以參考下
    2016-11-11
  • Android封裝MVP實(shí)現(xiàn)登錄注冊(cè)功能

    Android封裝MVP實(shí)現(xiàn)登錄注冊(cè)功能

    這篇文章主要為大家詳細(xì)介紹了Android封裝MVP實(shí)現(xiàn)登錄注冊(cè)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • 詳解Android中的SharedPreferences

    詳解Android中的SharedPreferences

    這篇文章主要介紹了Android中的SharedPreferences,包括 SharedPreferences的特點(diǎn)和使用方法,需要的朋友可以參考下
    2017-05-05
  • Android Studio使用教程(四):Gradle基礎(chǔ)

    Android Studio使用教程(四):Gradle基礎(chǔ)

    這篇文章主要介紹了Android Studio使用教程(四):Gradle基礎(chǔ),本文講解了什么是Gradle、安裝Gradle、Gradle 基本概念等內(nèi)容,需要的朋友可以參考下
    2015-05-05
  • myeclipse android環(huán)境搭建圖文教程

    myeclipse android環(huán)境搭建圖文教程

    在從事android應(yīng)用開(kāi)發(fā)過(guò)程中,搭配環(huán)境是所有開(kāi)始的第一步,本文將介紹myeclipse android環(huán)境搭建圖文教程,希望可以幫助大家完成android開(kāi)發(fā)之旅
    2012-11-11
  • Android編程實(shí)現(xiàn)抽屜效果的方法詳解

    Android編程實(shí)現(xiàn)抽屜效果的方法詳解

    這篇文章主要介紹了Android編程實(shí)現(xiàn)抽屜效果的方法,結(jié)合具體實(shí)例形式分析了Android實(shí)現(xiàn)抽屜效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-05-05
  • android app判斷是否有系統(tǒng)簽名步驟詳解

    android app判斷是否有系統(tǒng)簽名步驟詳解

    這篇文章主要為大家介紹了android app判斷是否有系統(tǒng)簽名步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11

最新評(píng)論