Android ListView的Item上浮动画
2016-09-05 17:26
281 查看
有时候在做列表显示时,需要给列表中的listView添加动画。在我做过的项目中大多数都是每个Item的上浮动画,动画比较简单,直接上代码。
很简单的补间动画,一个向上平移100pix的平移动画和从不显示到显示的的Alpha动画。
使用:要想让ListView中的每个Item都展示动画,那么我们需要在getView方法里,为convertView设置并启动Animation,即convertView.startAnimation(animation)。
这样同时也存在一个问题,就是getView滚动的时候会造成Item之间进行联动。因此,可以添加一个标志位来控制:
这样ListView中每个Item的上浮动画就搞定了。
AnimationSet animationSet = new AnimationSet(false); animationSet.setDuration(duration); if (listener != null) { animationSet.setAnimationListener(listener); } animationSet.setFillBefore(true); animationSet.setStartOffset(startOffset); Animation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, DrawUtils.dip2px(context, 100), Animation.RELATIVE_TO_SELF, 0); translateAnimation.setInterpolator(interpolator); animationSet.addAnimation(translateAnimation); Animation alphaAnimation = new AlphaAnimation(0, 1); animationSet.addAnimation(alphaAnimation);
很简单的补间动画,一个向上平移100pix的平移动画和从不显示到显示的的Alpha动画。
使用:要想让ListView中的每个Item都展示动画,那么我们需要在getView方法里,为convertView设置并启动Animation,即convertView.startAnimation(animation)。
@Override public View getView(int position, View convertView, ViewGroup parent) { //////////动画/////////////// if (convertView != null) { AnimationSet animationSet = new AnimationSet(false); animationSet.setDuration(mAnimDuration); animationSet.setAnimationListener(this); animationSet.setFillBefore(true); animationSet.setStartOffset(mCurrentAnimationCount * mAnimDuration / 3); Animation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, dip2px(mContext, 100), Animation.RELATIVE_TO_SELF, 0); translateAnimation.setInterpolator(mInterpolator); animationSet.addAnimation(translateAnimation); Animation alphaAnimation = new AlphaAnimation(0, 1); animationSet.addAnimation(alphaAnimation); convertView.startAnimation(animationSet); ++mCurrentAnimationCount; HANDLER.post(mRunnable); } ///////////结束////////////////////////////// return convertView; }
这样同时也存在一个问题,就是getView滚动的时候会造成Item之间进行联动。因此,可以添加一个标志位来控制:
if (convertView != null && position > mPreviousPostition) { mPreviousPostition = position; // 初始值为-1 AnimationSet animationSet = new AnimationSet(false); animationSet.setDuration(mAnimDuration); animationSet.setAnimationListener(this); animationSet.setFillBefore(true); animationSet.setStartOffset(mCurrentAnimationCount * mAnimDuration / 3); Animation translateAnimation = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.ABSOLUTE, dip2px(mContext, 100), Animation.RELATIVE_TO_SELF, 0); translateAnimation.setInterpolator(mInterpolator); animationSet.addAnimation(translateAnimation); Animation alphaAnimation = new AlphaAnimation(0, 1); animationSet.addAnimation(alphaAnimation); convertView.startAnimation(animationSet); ++mCurrentAnimationCount; HANDLER.post(mRunnable); } ///////////结束////////////////////////////// return convertView; }
这样ListView中每个Item的上浮动画就搞定了。
相关文章推荐
- android 实现listview 的item 出现在屏幕有个动画效果 类似于知乎的那个app
- android listview每个item定义动画呈现
- android笔记listview item加载动画
- Android AbsListView 的item动画类库 —— JazzyListView
- Android自定义控件:动画类(十四)----实现ListView Item进入动画
- Android中为ListView的Item选项设置出场动画
- Android listView如何设置item动画
- Android为listview的item添加动画效果
- Android AbsListView 的item动画类库 —— JazzyListView
- android listview每个item定义动画呈现
- Android listview item删除动画和item排序动画
- android listview每个item定义动画呈现
- 给Android ListView添加删除item动画
- android listview每个item定义动画呈现
- 给Android ListView添加删除item动画
- android listView 删除动画 当前Item 删了,但是动画总是从最后一行执行
- android listview每个item定义动画呈现
- Android—ListView Item 展开动画效果
- Android—ListView Item 展开动画效果
- android listview每个item定义动画呈现