Android自定义弹性ScrollView

2024-08-26 06:48

本文主要是介绍Android自定义弹性ScrollView,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Android自定义弹性ScrollView

总结了下最近写的弹性ScrollView,如下代码主要是通过触摸事件加动态更改布局实现的弹性ScrollView,具体分析都在注解中!


package ljh.android.view;import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;/*** 弹性ScrollView 实现下拉弹回和上拉弹回* * @author Ljh* 2015年8月26日*/public class ReboundScrollView extends ScrollView {// 保存ScrollView中子控件private View contentView = null;// 用来保存唯一子控件的布局信息private Rect contentViewRect = new Rect();// 移动开始时候的Y坐标private float startY;// 线性阻尼 缓冲过量移动的移动速度private static float MOVE_FACTOR = 0.5f;//过度位移恢复的动画持续时间private static long DURATION_MILLIS = 280;public ReboundScrollView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public ReboundScrollView(Context context, AttributeSet attrs) {super(context, attrs);}public ReboundScrollView(Context context) {super(context);}/*** 在布局完成后得到ScrollView的唯一子View,并存在contentView中*/@Overrideprotected void onFinishInflate() {if (getChildCount() > 0) {contentView = getChildAt(0);}}/*** 在事件分发其中处理触摸事件* 根据android中事件分发的机制判断,个人觉得把事件处理逻辑写在分发器中比写在onTouchEvent中好些,* 因为在其子View没有接收到该触摸事件之前自己就处理了触摸事件。*/@Overridepublic boolean dispatchTouchEvent(MotionEvent ev) {if (contentView != null)switch (ev.getAction()) {case MotionEvent.ACTION_DOWN:startY = ev.getY();break;case MotionEvent.ACTION_UP:if (isNeedAnimation()) {playAnimation();}break;case MotionEvent.ACTION_MOVE:float nowY = ev.getY();int detailY = (int) (nowY - startY);if (isNeedMove(detailY)) {// 超出屏幕后滚动的View移动的距离为滑动位移的MOVE_FACTOR倍detailY = (int) (detailY * MOVE_FACTOR);//重新布局子View,并且只修改顶部与底部的位置contentView.layout(contentViewRect.left, contentViewRect.top + detailY, contentViewRect.right,contentViewRect.bottom + detailY);}break;default:break;}return super.dispatchTouchEvent(ev);}/*** 在布局都完成后contentView的布局也就确定了*/@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {super.onLayout(changed, l, t, r, b);//在未超出移动前contentView的布局没有发生变化 即全局中contentView的布局不变if(contentView != null){contentViewRect.set(contentView.getLeft(), contentView.getTop(), contentView.getRight(),contentView.getBottom());}}/*** 判断是否需要超出屏幕移动* * 通过三个量来判断是否需要移动及如何移动,这三个量分别为scrollY、* contentViewHeight和scrollViewHeight外加辅助detailY手指移动的位移。分三种情况:* * 其中两种均为contentViewHeight>scrollViewHeight:* 1、当contentView的顶部处于ScrollView顶部且向下滑动手指时候需要超出屏幕移动条件为:* scrollY == 0 && detailY > 0, 如图:* |-----scrollViewHeight-----|* |----------contentViewHeight--------|*  -----detailY---->*  * 2、当contentView的底部处于ScrollView底部且向上滑动手指时候需要超出屏幕移动条件为:* scrollY + scrollViewHeight >= contentViewHeight && detailY < 0, 如图:* |--scrollY--|*             |-----scrollViewHeight-----|* |-----------contentViewHeight----------|*                       <-----detailY----*                       * 另外一种情况是contentViewHeight<=scrollViewHeight上下滑动都需要做超出屏幕移动* 3、当contentView的本身处于ScrollView内部时候无论向上或向下滑动手指时候都需要超出屏幕移动条件为:* contentViewHeight <= scrollViewHeight,如图:* |-----scrollViewHeight-----|* |---contentViewHeight---|*  <-----detailY---->* * @param detailY*            手指移动的位移(向下或向右滑动为正方向)* @return 是否需要移动*/private boolean isNeedMove(int detailY) {int scrollY = getScrollY();int contentViewHeight = contentView.getHeight();int scrollViewHeight = getHeight();return (scrollY == 0 && detailY > 0)|| (scrollY + scrollViewHeight >= contentViewHeight && detailY < 0)|| (contentViewHeight <= scrollViewHeight);}/*** 播放contentView复位的动画并将contentView复位* 动画可以自定义* 动画执行时间随拉伸的距离增加而减少*/private void playAnimation() {int contentViewTop = contentView.getTop();int scrollViewHeight = this.getHeight();float factor = 1-Math.abs(contentViewTop - contentViewRect.top)/(scrollViewHeight*1.0f);TranslateAnimation ta = new TranslateAnimation(0,0,contentViewTop,contentViewRect.top);ta.setDuration((long) (DURATION_MILLIS*factor));contentView.startAnimation(ta);contentView.layout(contentViewRect.left, contentViewRect.top,contentViewRect.right,contentViewRect.bottom);}/*** 判断是否需要动画效果* @return*/private boolean isNeedAnimation() {return contentView.getTop() != contentViewRect.top;}}


该实现方式中存在的问题或者是有待优化的问题,当ReboundScrollView处在最顶端或最低端拖动实现过量位移中不松开手指再反向减小偏移量时,滚动条会滚动!如果还有其他问题可以留言看到了一定回复!小伙伴有更好的弹性ScrollView的话可以分享给我!邮箱1075209054@qq.com 谢谢啦。





这篇关于Android自定义弹性ScrollView的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1107845

相关文章

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各