随着ScrollView的滑动,渐渐的执行动画View
2024-10-19 04:21:56
今天是实现了一个小功能的东西。看看效果图:
实现方式:
1.自定义ScrollView 复写onScrollChange方法,来计算滑动的位置。
2.自定义接口,通过接口来在ScrollView中控制,滑动的高度的进度。
3.在自定义View中去执行动画。
代码实现:
1.ScrollView 最主要的代码只有计算滑动位置的代码了,其实也是很简单的,获取子View的个数,每次都去for循环,去计算字View的位置,以及当前ScrollView的top bottom
代码:
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
int currentBottom = t + height ;
int currentTop = t ;
Log.e("Slide", "onScrollChange") ;
for (int i = 0; i < childCount; i++) {
View childView = contentLayout.getChildAt(i ) ;
if (!(childView instanceof EasySlideInter)) {
continue ;
}
int childTop = childView.getTop() ;
int childBottom = childView.getBottom() ;
int childHeight = childView.getHeight() ;
EasySlideInter inter = (EasySlideInter) childView ;
if ( currentTop > childTop && currentTop < childBottom ) {
inter.contentSlide(countProgress(currentTop, childBottom, childHeight));
}else if (currentBottom > childTop && currentBottom < childBottom ) {
inter.contentSlide(100 - countProgress(currentBottom, childBottom, childHeight));
}else if(childTop >= currentTop && childBottom <= currentBottom){
inter.resetContent();
}
}
}
通过childView的top位置与ScrollView的当前的top位置来判断是哪个子View正在慢慢的出现,计算出progress 传递给子View中去。
其实最终要的代码就是这么一段,动画的执行都在子View的接口方法中去做的。
我贴上一个子View的实现:
@Override
public void contentSlide(int progress) {
textAnimator.setCurrentPlayTime(progress);
backAnimator.setCurrentPlayTime(progress);
}
@Override
public void resetContent() {
textAnimator.setCurrentPlayTime(100);
backAnimator.setCurrentPlayTime(100);
}
private void initAnimation(){
textAnimator = ObjectAnimator.ofInt(text, "textColor", Color.BLUE , Color.RED);
textAnimator.setEvaluator(new ArgbEvaluator());
textAnimator.setDuration(100) ;
textAnimator.setInterpolator(new LinearInterpolator()) ;
backAnimator = ObjectAnimator.ofInt(this, "backgroundColor", Color.BLACK , Color.BLUE , Color.BLACK);
backAnimator.setEvaluator(new ArgbEvaluator());
backAnimator.setDuration(100) ;
backAnimator.setInterpolator(new LinearInterpolator()) ;
}
实现两个接口的方法,在这两个方法中,去控制动画的进度。
很简单的,不再累赘叙述了。
源码下载:
github地址 : https://github.com/flyme2012/EasySlide
附件列表
最新文章
- Markdown的使用---现学现用
- 【BZOJ1251】序列终结者 Splay
- gtest
- startup.c
- 前端里神奇的BFC 原理剖析
- Win10如何隐藏Windows Defender任务栏图标
- [设计模式]Netd中的命令设计模式
- Embedded Linux Primer----嵌入式Linux基础教程--2.4节--嵌入式Linux发行版
- ListActivity的注意点
- 利用Xtrabackup备份集合恢复一台从库的过程
- “MVC+Nhibernate+Jquery-EasyUI” 信息发布系统 第四篇(用户管理功能的实现)
- <;Video>; in HTML5
- win7 64位安装redis 及Redis Desktop Manager使用(转载的)
- 面试北京XX数通总结
- Android为TV端助力 deep link(深度链接)与自定义协议!
- Java语法基础学习DayEighteen(常用类)
- setfacl命令
- 用idea简单创建web项目——两种方式
- Linux----CentOS-7搭建免流服务器(iOS 端)
- 科学计算三维可视化---Mlab基础(常用控制函数)
热门文章
- 今日例子border
- libuv在cocos2d-x中的使用
- 携程Android App插件化和动态加载实践
- svchost占用内存达1-2G的问题
- 上海邮政EMS海关清关(个人) 流程
- HTTP请求报文和HTTP响应报文(转)
- Android 中的AIDL,Parcelable和远程服务
- javaweb 学习总结
- .NET错误The &#39;targetFramework&#39; attribute in the <;compilation>; element of the Web.config file is used only to target version 4.0 and later of the .NET Framework
- Scala 深入浅出实战经典 第61讲:Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析