ListView 条目加载上滑下滑首尾缩放动画实现

要实现这个效果,只需要再适配器getView之前,给每个条目的view设置相应的动画即可。

首先需要2个动画的xml文件。

在res下新建anim文件夹:(res/anim)

第一个动画xml文件:

up_from_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="100%" android:toYDelta="0%"
        android:duration="400" />
</set>

  

down_from_top.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:shareInterpolator="@android:anim/decelerate_interpolator">
    <translate
        android:fromXDelta="0%" android:toXDelta="0%"
        android:fromYDelta="-100%" android:toYDelta="0%"
        android:duration="400" />
</set>

  

在listview的适配器中使用定义的动画:

代码如下:

private int lastPosition = -1;

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    //Load your view, populate it, etc...
    View view = ...;

    Animation animation = AnimationUtils.loadAnimation(getContext(), (position > lastPosition) ? R.anim.up_from_bottom : R.anim.down_from_top);
    view.startAnimation(animation);
    lastPosition = position;

    return view;
}

  

参考: http://kylewbanks.com/blog/Implementing-Google-Plus-Style-ListView-Animations-on-Android

If you are interested the animation,

you can check out the full source on GitHub, or download the project and try it out.

原文地址:https://www.cnblogs.com/spring87/p/4626261.html