Android scrollTo、scrollBy、以及scroller详解 自定义ViewPager

Scroller

在这里插入图片描述

VelocityTracker

VelocityTracker 是一个速度跟踪器,通过用户操作时(通常在 View 的 onTouchEvent 方法中)传进去一系列的 Event,该类就可以计算出用户手指滑动的速度,开发者可以方便地获取这些参数去做其他事情。或者手指滑动超过一定速度并松手,就触发翻页。

CustomViewPager

package com.flannery.androidtools.widgets;import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Scroller;/*** VelocityTracker is a concept commonly used in computer graphics, physics simulations, and user interface frameworks to calculate the velocity of an object or pointer based on its position over time. It's particularly useful for creating responsive and realistic interactions in applications like games or touch-based user interfaces.* In the context of Android development, VelocityTracker refers to a class provided by the Android framework. It's used to track the velocity of motion events, such as touch events, on the screen. This can be useful for implementing various gestures and animations that require knowledge of how quickly a user is moving their finger across the screen.* Here's a basic overview of how VelocityTracker works in Android:* Initialization: To use VelocityTracker, you need to create an instance of it and associate it with a specific motion event, typically the ACTION_MOVE events in the case of touch gestures.* Tracking: As the user interacts with the screen, you feed the VelocityTracker instance with the motion events, which contain the current position of the pointer. The VelocityTracker class calculates the velocity based on the change in position over time.* Velocity Retrieval: After you've collected enough motion events, you can retrieve the calculated velocity using the computeCurrentVelocity(int units) method. The units parameter allows you to specify the desired units for the velocity, such as pixels per second.** https://www.nhooo.com/note/qadf7m.html*/
public class CustomViewPager extends ViewGroup {private static final String TAG = CustomViewPager.class.getSimpleName();private int screenWidth;private int screenHeight;private int lastMoveX = 0;private Scroller scroller; // 滚动计算器private VelocityTracker velocityTracker; // 速度跟踪器private int MAX_VELOCITY = 600;private int curScreen = 0;public CustomViewPager(Context context) {super(context);init(context);}public CustomViewPager(Context context, AttributeSet attrs) {super(context, attrs);init(context);}public CustomViewPager(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context);}private void init(Context context) {scroller = new Scroller(context); // 初始化滚动计算器// 添加三个ViewLinearLayout layout1 = new LinearLayout(context);layout1.setBackgroundColor(Color.RED);addView(layout1);LinearLayout layout2 = new LinearLayout(context);layout2.setBackgroundColor(Color.GREEN);addView(layout2);LinearLayout layout3 = new LinearLayout(context);layout3.setBackgroundColor(Color.BLUE);addView(layout3);}@Overridepublic boolean onTouchEvent(MotionEvent event) {Log.i(TAG, "onTouchEvent: onTouchEvent=" + event);if (velocityTracker == null) {velocityTracker = VelocityTracker.obtain(); // 初始化滚动速度跟踪器}velocityTracker.addMovement(event);int x = (int) event.getX();switch (event.getAction()) {case MotionEvent.ACTION_DOWN:lastMoveX = x; // 记录下按下的点break;case MotionEvent.ACTION_MOVE:int dis = lastMoveX - x; // 移动的偏移量Log.i(TAG, "onTouchEvent: dis=" + dis);scrollBy(dis, 0); // 位置滚动lastMoveX = x;break;case MotionEvent.ACTION_UP:velocityTracker.computeCurrentVelocity(1000); // 计算需要的位置int velocityX = (int) velocityTracker.getXVelocity(); // X轴上的速度if (velocityX > MAX_VELOCITY && curScreen > 0) {jump2Screen(curScreen - 1);} else if (velocityX < -MAX_VELOCITY && curScreen < getChildCount() - 1) {jump2Screen(curScreen + 1);} else {int screen = (getScrollX() + screenWidth / 2) / screenWidth;jump2Screen(screen);}if (velocityTracker != null) {velocityTracker.recycle();velocityTracker = null;}break;}return true;}public void jump2Screen(int screen) {curScreen = screen;if (curScreen < 0) {curScreen = 0;}if (curScreen > getChildCount() - 1) {curScreen = getChildCount() - 1;}int dis = curScreen * screenWidth - getScrollX();scroller.startScroll(getScrollX(), 0, dis, 0); // 开始滚动invalidate();}@Overridepublic void computeScroll() {super.computeScroll();if (scroller.computeScrollOffset()) { // 是否处于偏移量的位置scrollTo(scroller.getCurrX(), 0); // 滚动到指定的位置postInvalidate(); // 继续滚动}}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);screenWidth = MeasureSpec.getSize(widthMeasureSpec);screenHeight = MeasureSpec.getSize(heightMeasureSpec);setMeasuredDimension(screenWidth, screenHeight);// 给子View设置大小for (int i = 0; i < getChildCount(); i++) {View view = getChildAt(i);view.measure(screenWidth, screenHeight);}}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {int leftWidth = 0;// 给子View排班for (int i = 0; i < getChildCount(); i++) {View view = getChildAt(i);view.layout(leftWidth, 0, leftWidth + screenWidth, screenHeight);leftWidth = leftWidth + screenWidth;}}
}

资料

Android scrollTo、scrollBy、以及scroller详解
Android Scroller详解
Android自定义ViewPager实例
Android View 的滚动原理和 Scroller、VelocityTracker 类的使用

在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/84234.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【C++】C++ 引用详解 ⑤ ( 函数 “ 引用类型返回值 “ 当左值被赋值 )

文章目录 一、函数返回值不能是 " 局部变量 " 的引用或指针1、函数返回值常用用法2、分析函数 " 普通返回值 " 做左值的情况3、分析函数 " 引用返回值 " 做左值的情况 函数返回值 能作为 左值 , 是很重要的概念 , 这是实现 " 链式编程 &quo…

从“芯”出发,国产IDE来了?网友:VS Code 姊妹款?

点击上方“程序猿技术大咖”&#xff0c;关注并选择“设为星标” 回复“加群”获取入群讨论资格&#xff01; 昨天日本核污水排放刷屏&#xff0c;今天却被一条github issues 霸屏&#xff1a;Vscode&#xff0c;你们是否与中国合作过&#xff1f;&#xff08;Vscode, have you…

渗透测试漏洞原理之---【XSS 跨站脚本攻击】

文章目录 1、跨站 脚本攻击1.1、漏洞描述1.2、漏洞原理1.3、漏洞危害1.4、漏洞验证1.5、漏洞分类1.5.1、反射性XSS1.5.2、存储型XSS1.5.3、DOM型XSS 2、XSS攻防2.1、XSS构造2.1.1、利用<>2.1.2、JavaScript伪协议2.1.3、时间响应 2.2、XSS变形方式2.2.1、大小写转换2.2.2…

ServiceManager接收APP的跨进程Binder通信流程分析

现在一起来分析Server端接收&#xff08;来自APP端&#xff09;Binder数据的整个过程&#xff0c;还是以ServiceManager这个Server为例进行分析,这是一个至下而上的分析过程。 在分析之前先思考ServiceManager是什么&#xff1f;它其实是一个独立的进程&#xff0c;由init解析i…

Stable Diffusion 系列教程 | 如何获得更高清优质的AI绘画

目录 1 高清修复 1.1 原理 1.2 基本操作 1.3 优缺点 2 UpScale 放大脚本 2.1 原理 2.2 基本操作 2.3 优缺点 3 附加功能放大 3.1 原理 3.2 基本操作 3.3 优缺点 优化出图质量&#xff0c;产出更高清&#xff0c;分辨率更高&#xff0c;更有细节的绘画作品呢&#x…

算法笔记:KD树

1 引入原因 K近邻算法需要在整个数据集中搜索和测试数据x最近的k个点&#xff0c;如果一一计算&#xff0c;然后再排序&#xff0c;开销过大 引入KD树的作用就是对KNN搜索和排序的耗时进行改进 2 KD树 2.1 主体思路 以空间换时间&#xff0c;利用训练样本集中的样本点&…

[C#][原创]操作注册表一些注意点

C#注册表只需要引入 using Microsoft.Win32; C#注册表操作都是通过2个类Registry和RegistryKey进行所有操作。但是有些基本注意事项经常忘记&#xff0c;不常用就很容易忘记。 第一&#xff0c;打开注册表&#xff0c;第2个bool参数问题&#xff1a; RegistryKey key Regi…

Redis7之介绍(一)

1. 是什么 Redis:REmote Dictionary Server(远程字典服务器&#xff09; Remote Dictionary Server( 远程字典服务)是完全开源的&#xff0c;使用ANSIC语言编写遵守BSD协议&#xff0c;是一个高性能的Key-Value数据库提供了丰富的数据结构&#xff0c;例如String、Hash、List、…

K8S如何部署ZooKeeper以及如何进行ZooKeeper的平滑替换

前言 在之前的章节中&#xff0c;我们已经成功地将Dubbo项目迁移到了云环境。在这个过程中&#xff0c;我们选择了单机ZooKeeper作为注册中心。接下来&#xff0c;我们将探讨如何将单机ZooKeeper部署到云端&#xff0c;以及在上云过程中可能遇到的问题及解决方案。 ZooKeeper…

设计模式三原则

1.1单一职责原则 C 面向对象三大特性之一的封装指的就是将单一事物抽象出来组合成一个类&#xff0c;所以我们在设计类的时候每个类中处理的是单一事物而不是某些事物的集合。 设计模式中所谓的单一职责原则&#xff0c;就是对一个类而言&#xff0c;应该仅有一个引起它变化的原…

Docker容器:dockerfile创建 LNMP 服务+Wordpress 网站平台

文章目录 一.环境及准备工作1.项目环境2.服务器环境3.任务需求 二.Linux 系统基础镜像三.docker构建Nginx1.建立工作目录上传安装包2.编写 Dockerfile 脚本3.准备 nginx.conf 配置文件4.生成镜像5.创建自定义网络6.启动镜像容器7.验证 nginx 四.docker构建Mysql1. 建立工作目录…

剪枝基础与实战(2): L1和L2正则化及BatchNormalization讲解

1. CIFAR10 数据集 CIFAR10 是深度学习入门最先接触到的数据集之一,主要用于图像分类任务中,该数据集总共有10个类别。 图片数量:6w 张图片宽高:32x32图片类别:10Trainset: 5w 张,5 个训练块Testset: 1w 张,1 个测试块Pytorch 集成了很多常见数据集的API, 可以通过py…