用LayoutAnimationController给ViewGroup的item添加动画,以GridView为例

demo地址http://download.csdn.net/detail/tingfengzheshuo/9552593

关于LayoutAnimation的使用基础可以参考

实现的效果类似下图,不过只有扩大的过程,没有缩小的过程。


LayoutAnimation是用来控制一个ViewGroup的里面的子控件的动画的类,使用这个类非常简单
1,new一个 LayoutAnimationController对象。
1,调用ViewGroup对象的 setLayoutAnimation方法,把new出来的 LayoutAnimationController对象设置给ViewGroup对象。

下面说一说LayoutAnimationController对象的特性。
如何构建一些很特别的动画,就需要我们分析下LayoutAnimationController的特性了。
LayoutAnimationController在初始化的时候,需要传入一个动画对象,这个动画就是每一个item展现的动画了。需要关注的还有另外一个参数,在LayoutAnimationController类的一个构造方法中可以发现,我们能传一个float值,这个值就是delay值,这个参数控制每个item开始动画前的延迟时间。经过笔者的测试和查看源代码发现,viewGroup对象在绘制动画之前会先把每个item开始动画之前的延迟时间都测量好,android源码是这样的
final float delay = mDelay * mAnimation.getDuration();
final long viewDelay = (long) (getTransformedIndex(params) * delay);
这两行代码在LayoutAnimationController类的getDelayForView方法中,这样我们就知道了,每个item的延迟时间由三个参数控制,动画本身的持续时间,delay系数和getTransformedIndex方法获取的值,这三个参数恰恰是我们自己能控制的,所以我们可以试着通过设置这三个参数来控制item动画的延迟时间达到上面动画效果。因为在LayoutAnimationController类中getTransformedIndex方法是protected方法,所以我们可以在它的子类中重载此方法。
1,首先,布局文件里要放一个GridView对象,列数设置为4。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white">

<GridView
android:numColumns="4"
android:id="@+id/gv_content"
android:verticalSpacing="3dp"
android:horizontalSpacing="3dp"
android:layout_width="match_parent"
android:layout_height="match_parent"></GridView>
</LinearLayout>

2, 自定义一个LayoutAnimationController类的子类,子类中重载getTransformedIndex方法。
public class MLayoutAnimationController extends LayoutAnimationController {
// 7 just lucky number
public static final int ORDER_CUSTOM = 7;

private Callback onIndexListener;

public void setOnIndexListener(Callback onIndexListener) {
this.onIndexListener = onIndexListener;
}

public MLayoutAnimationController(Context context, AttributeSet attrs) {
super(context, attrs);
}

public MLayoutAnimationController(Animation animation, float delay) {
super(animation, delay);
}

public MLayoutAnimationController(Animation animation) {
super(animation);
}

/**
* override method for custom play child view animation order
*/
protected int getTransformedIndex(AnimationParameters params) {
if(getOrder() == ORDER_CUSTOM && onIndexListener != null) {
return onIndexListener.onIndex(this, params.count, params.index);
} else {
return super.getTransformedIndex(params);
}
}

/**
* callback for get play animation order
*
*/
public static interface Callback{
public int onIndex(MLayoutAnimationController controller, int count, int index);
}

}
这儿自定义了一个Callback接口,用于自己可以创建不同的接口对象以填写不同的计算逻辑。
3,在Activity中new一个自定义的LayoutAnimationController子类的对象。子类中设置一个Callback接口对象,这个对象中的方法填写真正计算逻辑,计算出来的参数就是前面提到的三个参数之一,另外两个参数,delay系数,动画持续时间都在这儿定义了。每个item延迟就是这三个参数的乘运算结果。

protected LayoutAnimationController getAnimationController() {
int duration = 500;
        AnimationSet set = new AnimationSet(true);
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.anim_scale);
animation.setDuration(duration);
set.addAnimation(animation);

MLayoutAnimationController controller = new MLayoutAnimationController(set, 0.2f);
controller.setOrder(MLayoutAnimationController.ORDER_CUSTOM);
controller.setOnIndexListener(new MLayoutAnimationController.Callback() {
@Override
public int onIndex(MLayoutAnimationController controller, int count, int index) {
// Log.d("LOL","count:" + count + "index" + index);
switch (index % 4){
case 0:
return 0 + index / 4;
case 1:
return 1 + index / 4;
case 2:
return 2 + index / 4;
case 3:
return 3 + index / 4;
}
return index;
}
});
return controller;
}
4,把new出来的子类对象设置给ViewGroup对象。
5,VIewGroup设置adapter对象填充数据。
第三步的onIndex方法中,count为item总数,index为当前计算延迟的item的索引。因为gridView为4列,而动画效果中每一行的动画都比上一行动画延迟一点,但是也不是上一行结束时才结束。所以这儿对4做整除运算,这样每一行,右边的参数都比前一个大1,后面加上index / 4,这样每一列,下边的都比上边的大1,这样就能实现图片中类似的效果了。


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值