即之前了解了屬性動(dòng)畫(huà) Property Animator 的 Evaluator 添吗、Interpolator 和 xml 定義 之后,今天就完成最后一部分的總結(jié):布局動(dòng)畫(huà)瞬哼。不過(guò)婚肆,先補(bǔ)充一個(gè)知識(shí)點(diǎn):PropertyValuesHolder。
PropertyValuesHolder
我們可以理解為 多屬性值控制器 坐慰, 它可以幫助我們跟簡(jiǎn)單方便的實(shí)現(xiàn)多個(gè)同步動(dòng)畫(huà)的效果较性。
// 用 PropertyValuesHolder 實(shí)現(xiàn)多個(gè)同步動(dòng)畫(huà)
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 0, 600, 0);
ObjectAnimator.ofPropertyValuesHolder(animView, pvhX, pvhY).setDuration(1000).start();
如上就是實(shí)現(xiàn) 同步 漸變 和 Y軸縮放 的動(dòng)畫(huà)效果用僧。可以與其他實(shí)現(xiàn)方式比較下赞咙,的確是更加方便责循。
布局動(dòng)畫(huà) (Layout Aniamtion)
以下參靠Android 屬性動(dòng)畫(huà)(Property Animation) 完全解析 (下),的確這種方式講解最簡(jiǎn)單攀操。主要使用LayoutTransition為布局的容器設(shè)置動(dòng)畫(huà)院仿,當(dāng)容器中的視圖層次發(fā)生變化時(shí)存在過(guò)渡的動(dòng)畫(huà)效果。
過(guò)渡動(dòng)畫(huà)有四種類型:
- LayoutTransition.APPEARING 當(dāng)一個(gè)View A 在ViewGroup中出現(xiàn)時(shí)速和,對(duì)View A設(shè)置的動(dòng)畫(huà)
- LayoutTransition.DISAPPEARING 當(dāng)一個(gè)View A 在ViewGroup中消失時(shí)歹垫,對(duì)View A設(shè)置的動(dòng)畫(huà)
- LayoutTransition.CHANGE_APPEARING 當(dāng)一個(gè)View A 在ViewGroup中出現(xiàn)時(shí),對(duì)被 View A 影響了位置的 ** View ** 設(shè)置的動(dòng)畫(huà)
- LayoutTransition.CHANGE_DISAPPEARING 當(dāng)一個(gè)View A 在ViewGroup中消失時(shí)颠放,對(duì)被 View A 影響了位置的 ** View ** 設(shè)置的動(dòng)畫(huà)
·
主要?jiǎng)赢?huà)設(shè)置代碼:
LayoutTransition transition = new LayoutTransition();
/** @param transitionType One of {@link #CHANGE_APPEARING},
* {@link #CHANGE_DISAPPEARING},
* {@link #CHANGING},
* {@link #APPEARING},
* or {@link #DISAPPEARING},
* which determines the* animation whose animator is being set.
* @param animator The animation being assigned.
* A value of <code>null</code> means that no animation
* will be run for the specified transitionType.
* */
transition.setAnimator(LayoutTransition.CHANGE_APPEARING, transition.getAnimator(LayoutTransition.CHANGE_APPEARING));
ViewGroup.setLayoutTransition(transition );
如上大家可以看到 LayoutTransition.setAnimator(int transitionType, Animator animator); 第一個(gè)參數(shù)就是 設(shè)置布局動(dòng)畫(huà)類型(四種)排惨,第二個(gè)參數(shù)就是動(dòng)畫(huà)對(duì)象,我們可以任意設(shè)置碰凶。
當(dāng)然暮芭,LayoutTransition 可以設(shè)置多個(gè) setAnimator。
實(shí)現(xiàn)demo 代碼
布局文件:
<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);
}
}
大家可以運(yùn)行欲低,看下效果谴麦。
總結(jié)
進(jìn)過(guò) 三篇文章的學(xué)習(xí)總結(jié),我們對(duì) 屬性動(dòng)畫(huà) (Property Animator)有了全面的了解∩焱罚現(xiàn)在匾效,我們就可以去實(shí)戰(zhàn)中使用他們啦。至于直播打賞動(dòng)畫(huà)恤磷,