【问题标题】:Android animation only when ListView scrolls down仅当 ListView 向下滚动时的 Android 动画
【发布时间】:2016-06-08 00:49:00
【问题描述】:

我在布局中有一个 ListView, 仅当列表向下滚动时,我才需要应用动画(从下到上出现)。

我该怎么做?

请帮帮我。

【问题讨论】:

  • 添加动画到列表适配器,可以在特定位置开始动画
  • 试试 this 动画教程。

标签: android android-layout user-interface listview android-animation


【解决方案1】:

希望这对您有所帮助。 谷歌加动画

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>

代码:

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;
}

【讨论】:

  • 欢迎@MirzakhmetSyzdykov
【解决方案2】:

将动画添加到特定视图(可以是 ImageView ,LinearLayout ...),当适配器为任何新项目充气时,getView 方法调用 mean 时,动画将开始

 @Override
      public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.rowlayout, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.label);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        setAnimation(YOUR_VIEW, position);
    }

 private void setAnimation(View viewToAnimate, int position)
    {
        // If the bound view wasn't previously displayed on screen, it's animated
        if (position > lastPosition) // you can put any rule you need 
        {
            Animation animation = AnimationUtils.loadAnimation(_Context, R.anim.push_left_in); // add any animation you want 
            viewToAnimate.startAnimation(animation);
            lastPosition = position;
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-06-27
    • 1970-01-01
    • 2015-07-17
    • 2019-11-21
    • 2018-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多