即之前了解了属性动画 Property Animator 的 Evaluator 、Interpolator 和 xml 定义 之后,今天就完成最后一部分的总结:布局动画。不过,先补充一个知识点:PropertyValuesHolder。
PropertyValuesHolder
我们可以理解为 多属性值控制器 , 它可以帮助我们跟简单方便的实现多个同步动画的效果。
// 用 PropertyValuesHolder 实现多个同步动画
PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f, 1f);
PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", 0, 600, 0);
ObjectAnimator.ofPropertyValuesHolder(animView, pvhX, pvhY).setDuration(1000).start();
如上就是实现 同步 渐变 和 Y轴缩放 的动画效果。可以与其他实现方式比较下,的确是更加方便。
布局动画 (Layout Aniamtion)
以下参靠Android 属性动画(Property Animation) 完全解析 (下),的确这种方式讲解最简单。主要使用LayoutTransition为布局的容器设置动画,当容器中的视图层次发生变化时存在过渡的动画效果。
过渡动画有四种类型:
- LayoutTransition.APPEARING 当一个View A 在ViewGroup中出现时,对View A设置的动画
- LayoutTransition.DISAPPEARING 当一个View A 在ViewGroup中消失时,对View A设置的动画
- LayoutTransition.CHANGE_APPEARING 当一个View A 在ViewGroup中出现时,对被 View A 影响了位置的 ** View ** 设置的动画
- LayoutTransition.CHANGE_DISAPPEARING 当一个View A 在ViewGroup中消失时,对被 View A 影响了位置的 ** View ** 设置的动画
·
主要动画设置代码:
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); 第一个参数就是 设置布局动画类型(四种),第二个参数就是动画对象,我们可以任意设置。
当然,LayoutTransition 可以设置多个 setAnimator。
实现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);
// 创建一个GridLayout
mGridLayout = new GridLayout(this);
// 设置每列5个按钮
mGridLayout.setColumnCount(5);
// 添加到布局中
viewGroup.addView(mGridLayout);
//默认动画全部开启
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);
}
}
大家可以运行,看下效果。
总结
进过 三篇文章的学习总结,我们对 属性动画 (Property Animator)有了全面的了解。现在,我们就可以去实战中使用他们啦。至于直播打赏动画,