【问题标题】:set animation on listview scrolling in android在android中的listview滚动上设置动画
【发布时间】:2016-02-29 10:02:00
【问题描述】:

当用户滚动ListView 时,我想为ListView 设置动画。我在加载时为ListView 设置动画,但我想为滚动设置动画。

这是我在 eclipse 中的动画文件:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="schemas.android.com/apk/res/android";   
    android:duration="900" android:fromXDelta="-100%p" 
    android:interpolator="@android:anim/linear_interpolator" 
    android:repeatCount="0" android:repeatMode="reverse" 
    android:fillEnabled="true" android:fillAfter="true" android:toXDelta="0%p" >
</translate>

此代码设置项目到我的列表视图:

@Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    StructNote item = getItem(position);
    if (convertView == null) {
        convertView = G.inflater.inflate(R.layout.adapter_notes, parent, false);
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    holder.fill(this, item, position); return convertView;
}

这是一个将动画设置为列表视图的 ActivityMain

ListView lstContent = (ListView) findViewById(R.id.lstContent);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);    
lstContent.setLayoutAnimation(new LayoutAnimationController(animation));

谁能帮帮我??

【问题讨论】:

  • schemas.android.com/apk/res/android" android:duration="900" android:fromXDelta="-100 %p" android:interpolator="@android:anim/linear_interpolator" android:repeatCount="0" android:repeatMode="reverse" android:fillEnabled="true" android:fillAfter="true" android:toXDelta="0% p" >
  • 此代码设置项目到我的列表视图:@Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; StructNote item = getItem(position); if (convertView == null) { convertView = G.inflater.inflate(R.layout.adapter_notes, parent, false);持有人 = 新 ViewHolder(convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.fill(这个,项目,位置);返回转换视图; }
  • 这是一个将动画设置为 listview 的 ActivityMain : ListView lstContent = (ListView) findViewById(R.id.lstContent);动画动画 = AnimationUtils.loadAnimation(this, R.anim.translate); lstContent.setLayoutAnimation(new LayoutAnimationController(animation));
  • 您应该在帖子中发布您的代码,而不是在 cmets 中。你应该看看 Recyclerview 和 ItemAnimator
  • 你要为滚动设置哪种类型的动画???

标签: android listview animation scroll


【解决方案1】:

ListView 适配器:

Animation scaleUp;

 public MyAdapter(...) {
           //...
     scaleUp = AnimationUtils.loadAnimation(activity, R.anim.scale_up_fast);
 }

 public View getView(final int position, View convertView, ViewGroup parent) {

    CardView cv;

    if (convertView == null){
      inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      convertView = inflater.inflate(R.layout.your_layout, parent, false);
    }
    cv = (CardView) convertView.findById(R.id.yourView);//Change this to your view      
    cv.startAnimation(scaleUp);
 }

动画:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <scale
        android:duration="300"
        android:fromXScale="0"
        android:fromYScale="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1"
        android:toYScale="1" />
</set>

【讨论】:

  • 不错,但是如何避免以前加载的项目的动画?我的意思是在向下滚动时,为列表项提供动画是可以的,但如果我向上滚动,它不应该动画,因为这些项目已经加载。
【解决方案2】:

有很多方法可以完成这种任务,我建议您知道示例,您可以创建自己的动画,也可以为此使用预定义库。 example 1,Some listview which have effects extra than the listview widget

【讨论】:

  • 亲爱的朋友,你有什么建议?
  • 有很多方法可以用不同的方式制作动画,比如缩放视图或视差背景,只需在 github 上查看
【解决方案3】:

首先从动画 xml 中删除 semicolon(;) 然后我看到了像这个应用程序这样的滚动动画https://play.google.com/store/apps/details?id=com.nilstechsys.myquotes

这是一个简单的listview adpter中的滚动动画代码

 Animation animation = null;
        animation = new TranslateAnimation(metrics_.widthPixels / 2, 0, 0, 0);
        //animation = AnimationUtils.loadAnimation(activity, R.anim.your_animation);
        animation.setDuration(400);
        convertView.startAnimation(animation);
        animation = null;

【讨论】:

    【解决方案4】:

    这是我可以根据您的要求为您提供的最佳解决方案!请通过此适配器一次。

     public class MyAdapter extends ArrayAdapter<String>{
    
                private int mLastPosition;
    
                public MyAdapter(Context context, ArrayList<String> objects) {
                    super(context, 0, objects);
                }
    
                private class ViewHolder{
                    public TextView mTextView;
                }
    
                @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
                @Override
                public View getView(int position, View convertView, ViewGroup parent) {
    
                    ViewHolder holder;
    
                    if (convertView == null) {
                        holder = new ViewHolder();
                        convertView = LayoutInflater.from(getContext()).inflate(R.layout.grid_item, parent, false);
                        holder.mTextView = (TextView) convertView.findViewById(R.id.checkbox);
                        convertView.setTag(holder);
                    } else {
                        holder = (ViewHolder) convertView.getTag();
                    }
    
                    holder.mTextView.setText(getItem(position));
    
                    // This tells the view where to start based on the direction of the scroll.
                    // If the last position to be loaded is <= the current position, we want
                    // the views to start below their ending point (500f further down).
                    // Otherwise, we start above the ending point.
                    float initialTranslation = (mLastPosition <= position ? 500f : -500f);
    
                    convertView.setTranslationY(initialTranslation);
                    convertView.animate()
                            .setInterpolator(new DecelerateInterpolator(1.0f))
                            .translationY(0f)
                            .setDuration(300l)
                            .setListener(null);
    
                    // Keep track of the last position we loaded
                    mLastPosition = position;
    
                    return convertView;
                }
    
    
            }
    

    【讨论】:

      【解决方案5】:

      列表视图滚动的简单而美妙的动画

       @Override`
      public View getView(final int i, View view, ViewGroup viewGroup) {
          View converview = view;`
      // other coding related to view
      
          if (animateOrNot) {    //boolean if want to animate
              doCards(converview, i, prevPos);}
             `prevPos=i; //take prevPos as global variable of int
          return converview;
      }`
      
      
      
          public static void doCards(View view, int position, int prevPosition) {
          view.setScaleX(0.5f);
          if (position > prevPosition) {
              view.setRotationX(90);
          } else {
              view.setRotationX(-90);
          }
          view.animate().rotationX(0).scaleX(1.0f).start();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-02-05
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-06-08
        相关资源
        最近更新 更多