Android ListView的Item上浮动画
来源:互联网 发布:php 获取 上传文件内容 编辑:程序博客网 时间:2024/04/27 14:21
有时候在做列表显示时,需要给列表中的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的上浮动画就搞定了。
0 0
- Android ListView的Item上浮动画
- listview的item动画
- listView 的item添加动画
- ListView的item删除动画
- ListView删除Item的动画
- ListView的item动画效果
- Android为listview的item添加动画效果
- android-- 如何实现ListView Item的动画?
- 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定义动画呈现
- mysql获取当前时间,格式化当前时间,增减当前时间
- Spring整合mybatis总结:
- ajax应用入门——demo制作(2)
- Java中抽象类和接口的区别
- jvm 怎么调优
- Android ListView的Item上浮动画
- 巧遇ORA-00600: 内部错误代码, 参数: [kcratr_nab_less_than_odr]
- Android下数据库的增删改查(两种方式)
- Curl.php
- Permission is only granted to system apps
- SoundPool和MediaPlayer的区别
- Android LayoutInflater原理分析,带你一步步深入了解View(一)
- mysqldump的几种常用方法:
- UE4.12.5-C++ 网络复制 Net/UnrealNetwork.h