当前位置: 首页 > news >正文

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.lryc.cn/news/141100.html

相关文章:

  • Aidex 移动端快速开发框架# RuoYi-Uniapp项目,uniapp vue app项目跨域问题
  • JVM7:垃圾回收是什么?从运行时数据区看垃圾回收到底回收哪块区域?垃圾回收如何去回收?垃圾回收策略,引用计数算法及循环引用问题,可达性分析算法
  • NFT Insider #104:The Sandbox:全新土地销售活动 Turkishverse 来袭
  • Docker架构及原理
  • VScode使用SSH连接linux
  • 多线程的创建与同步
  • LLMs 缩放指令模型Scaling instruct models FLAN(Fine-tuned LAnguage Net,微调语言网络)
  • 政务大厅人员睡岗离岗玩手机识别算法
  • 认识Mybatis的关联关系映射,灵活关联表对象之间的关系
  • 如何开发一款唯一艺术平台 区块链 /数字藏品
  • 【UniApp开发小程序】私聊功能后端实现 (买家、卖家 沟通商品信息)【后端基于若依管理系统开发】
  • 运维高级学习--Kubernetes(K8s 1.28.x)部署
  • Apache zookeeper kafka 开启SASL安全认证 —— 筑梦之路
  • lintcode 1017 · 相似的RGB颜色【进制计算】
  • 全国首台!浙江机器人产业集团发布垂起固定翼无人机-机器人自动换电机巢
  • 采用 UML 对软件系统进行建模的基本框架
  • 编译tiny4412 Linux 内核
  • Ubuntu22.04安装中文输入法►由踩坑到上岸版◄
  • SpringBoot简单上手
  • git及GitHub的使用
  • 【考研数学】线性代数第四章 —— 线性方程组(1,基本概念 | 基本定理 | 解的结构)
  • 使用Python写入数据到Excel:实战指南
  • 接口测试总结分享(http与rpc)
  • 数据结构(Java实现)LinkedList与链表(下)
  • linux查看正在运行的nginx在哪个文件夹当中
  • Vue实现Excel表格中按钮增加小数位数,减少小数位数功能,多用于处理金融数据
  • 自然语言处理(一):词嵌入
  • 【HSPCIE仿真】HSPICE仿真基础
  • 二、前端监控之方案调研
  • npm 创建 node.js 项目