Android SwipeRefreshLayout实现工作原理.

2024-05-11 12:58

本文主要是介绍Android SwipeRefreshLayout实现工作原理.,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

首先这个工具类是由V4包提供的,翻看里面的源码发现,原来这个工具视图[主要探讨视图]是通过自己创建了一个圆形VIew,并在圆形View里面添加了一个旋转的进度Drawable,当然旋转的并不是一个控件,仅仅是一个资源.并通过圆形View去设置了该Drawable,并将圆形图片添加到了SwieRefresh控件上.具体源码如下,通过该源码你可以找到MaterialProgressDrawable,自己手动实现一些进度,大致看了一下算法,还不错,如果你想依赖原生的,你就可以自己创建一个控件去设置这个背景.

mCircleView = new CircleImageView(getContext(), CIRCLE_BG_LIGHT, CIRCLE_DIAMETER/2);mProgress = new MaterialProgressDrawable(getContext(), this);mProgress.setBackgroundColor(CIRCLE_BG_LIGHT);mCircleView.setImageDrawable(mProgress);//设置圆形进度资源mCircleView.setVisibility(View.GONE);addView(mCircleView);
然后 MaterialProgressDrawable提供一些方便的api,
<pre name="code" class="java">setAlpha(float); // 设置渐变
showArrow(boolean);// 设置箭头
setStartEndTrim(float,float); // 开始和结束的长度这里默认是0.8f,参考源码:mProgress.setStartEndTrim(0f, Math.min(MAX_PROGRESS_ANGLE, strokeStart));
setArrowScale(float); //缩放箭头

/*关键的旋转代码*/
 
<pre name="code" class="java">float rotation = (-0.25f + .4f * adjustedPercent + tensionPercent * 2) * .5f; // ACTION_MOVE的时候计算的一个旋转值,然后给该设置,当然你也可以自己定义.
mProgress.setProgressRotation(rotation);// 设置旋转值.
mProgress.start();//开始旋转
mProgress.stop(); // 结束旋转.

 

下面是MaterialProcessDrawable部分算法源码:

// The minProgressArc is calculated from 0 to create an// angle that// matches the stroke width.final float minProgressArc = (float) Math.toRadians(ring.getStrokeWidth() / (2 * Math.PI * ring.getCenterRadius()));final float startingEndTrim = ring.getStartingEndTrim();final float startingTrim = ring.getStartingStartTrim();final float startingRotation = ring.getStartingRotation();// Offset the minProgressArc to where the endTrim is// located.final float minArc = MAX_PROGRESS_ARC - minProgressArc;final float endTrim = startingEndTrim + (minArc* START_CURVE_INTERPOLATOR.getInterpolation(interpolatedTime));ring.setEndTrim(endTrim);final float startTrim = startingTrim + (MAX_PROGRESS_ARC* END_CURVE_INTERPOLATOR.getInterpolation(interpolatedTime));ring.setStartTrim(startTrim);final float rotation = startingRotation + (0.25f * interpolatedTime);ring.setRotation(rotation);float groupRotation = ((720.0f / NUM_POINTS) * interpolatedTime)+ (720.0f * (mRotationCount / NUM_POINTS));setRotation(groupRotation);//设置旋转值
/***画箭头**/
private void drawTriangle(Canvas c, float startAngle, float sweepAngle, Rect bounds) {if (mShowArrow) {if (mArrow == null) {mArrow = new android.graphics.Path();mArrow.setFillType(android.graphics.Path.FillType.EVEN_ODD);} else {mArrow.reset();}// Adjust the position of the triangle so that it is inset as// much as the arc, but also centered on the arc.float inset = (int) mStrokeInset / 2 * mArrowScale;float x = (float) (mRingCenterRadius * Math.cos(0) + bounds.exactCenterX());float y = (float) (mRingCenterRadius * Math.sin(0) + bounds.exactCenterY());// Update the path each time. This works around an issue in SKIA// where concatenating a rotation matrix to a scale matrix// ignored a starting negative rotation. This appears to have// been fixed as of API 21.mArrow.moveTo(0, 0);mArrow.lineTo(mArrowWidth * mArrowScale, 0);mArrow.lineTo((mArrowWidth * mArrowScale / 2), (mArrowHeight* mArrowScale));mArrow.offset(x - inset, y);mArrow.close();// draw a trianglemArrowPaint.setColor(mColors[mColorIndex]);c.rotate(startAngle + sweepAngle - ARROW_OFFSET_ANGLE, bounds.exactCenterX(),bounds.exactCenterY());c.drawPath(mArrow, mArrowPaint);}}

这儿主要是画进度:

/*** Draw the progress spinner*/public void draw(Canvas c, Rect bounds) {final RectF arcBounds = mTempBounds;arcBounds.set(bounds);arcBounds.inset(mStrokeInset, mStrokeInset);final float startAngle = (mStartTrim + mRotation) * 360;final float endAngle = (mEndTrim + mRotation) * 360;float sweepAngle = endAngle - startAngle;mPaint.setColor(mColors[mColorIndex]);c.drawArc(arcBounds, startAngle, sweepAngle, false, mPaint);drawTriangle(c, startAngle, sweepAngle, bounds);if (mAlpha < 255) {mCirclePaint.setColor(mBackgroundColor);mCirclePaint.setAlpha(255 - mAlpha);c.drawCircle(bounds.exactCenterX(), bounds.exactCenterY(), bounds.width() / 2,mCirclePaint);}}


 下面是我自己定义的CircleView,算法如下.:

mRefreshStart += mIsStart ? 3 : 10;//计算距离mRefreshStop += mIsStart ? 10 : 3;mRefreshStart = mRefreshStart % 360;//还原位置mRefreshStop = mRefreshStop % 360;int swipe = mRefreshStop - mRefreshStart;swipe = swipe < 0 ? swipe + 360 : swipe; //计算扫过的角度.canvas.drawArc(rectF,mRefreshStart, swipe, false, mOutPaint);//画圆弧.if (swipe >= 330) {//恢复加速mIsStart = false;} else if (swipe <= 10) {//开始加速mIsStart = true;}

GitHub地址:https://github.com/q422013/CircleView


这篇关于Android SwipeRefreshLayout实现工作原理.的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

利用Python实现Excel文件智能合并工具

《利用Python实现Excel文件智能合并工具》有时候,我们需要将多个Excel文件按照特定顺序合并成一个文件,这样可以更方便地进行后续的数据处理和分析,下面我们看看如何使用Python实现Exce... 目录运行结果为什么需要这个工具技术实现工具的核心功能代码解析使用示例工具优化与扩展有时候,我们需要将

Python+PyQt5实现文件夹结构映射工具

《Python+PyQt5实现文件夹结构映射工具》在日常工作中,我们经常需要对文件夹结构进行复制和备份,本文将带来一款基于PyQt5开发的文件夹结构映射工具,感兴趣的小伙伴可以跟随小编一起学习一下... 目录概述功能亮点展示效果软件使用步骤代码解析1. 主窗口设计(FolderCopyApp)2. 拖拽路径

Spring AI 实现 STDIO和SSE MCP Server的过程详解

《SpringAI实现STDIO和SSEMCPServer的过程详解》STDIO方式是基于进程间通信,MCPClient和MCPServer运行在同一主机,主要用于本地集成、命令行工具等场景... 目录Spring AI 实现 STDIO和SSE MCP Server1.新建Spring Boot项目2.a

Spring Boot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)

《SpringBoot拦截器Interceptor与过滤器Filter深度解析(区别、实现与实战指南)》:本文主要介绍SpringBoot拦截器Interceptor与过滤器Filter深度解析... 目录Spring Boot拦截器(Interceptor)与过滤器(Filter)深度解析:区别、实现与实

C#实现访问远程硬盘的图文教程

《C#实现访问远程硬盘的图文教程》在现实场景中,我们经常用到远程桌面功能,而在某些场景下,我们需要使用类似的远程硬盘功能,这样能非常方便地操作对方电脑磁盘的目录、以及传送文件,这次我们将给出一个完整的... 目录引言一. 远程硬盘功能展示二. 远程硬盘代码实现1. 底层业务通信实现2. UI 实现三. De

SpringBoot后端实现小程序微信登录功能实现

《SpringBoot后端实现小程序微信登录功能实现》微信小程序登录是开发者通过微信提供的身份验证机制,获取用户唯一标识(openid)和会话密钥(session_key)的过程,这篇文章给大家介绍S... 目录SpringBoot实现微信小程序登录简介SpringBoot后端实现微信登录SpringBoo

使用Vue-ECharts实现数据可视化图表功能

《使用Vue-ECharts实现数据可视化图表功能》在前端开发中,经常会遇到需要展示数据可视化的需求,比如柱状图、折线图、饼图等,这类需求不仅要求我们准确地将数据呈现出来,还需要兼顾美观与交互体验,所... 目录前言为什么选择 vue-ECharts?1. 基于 ECharts,功能强大2. 更符合 Vue

使用WPF实现窗口抖动动画效果

《使用WPF实现窗口抖动动画效果》在用户界面设计中,适当的动画反馈可以提升用户体验,尤其是在错误提示、操作失败等场景下,窗口抖动作为一种常见且直观的视觉反馈方式,常用于提醒用户注意当前状态,本文将详细... 目录前言实现思路概述核心代码实现1、 获取目标窗口2、初始化基础位置值3、创建抖动动画4、动画完成后

uniapp小程序中实现无缝衔接滚动效果代码示例

《uniapp小程序中实现无缝衔接滚动效果代码示例》:本文主要介绍uniapp小程序中实现无缝衔接滚动效果的相关资料,该方法可以实现滚动内容中字的不同的颜色更改,并且可以根据需要进行艺术化更改和自... 组件滚动通知只能实现简单的滚动效果,不能实现滚动内容中的字进行不同颜色的更改,下面实现一个无缝衔接的滚动

C#通过进程调用外部应用的实现示例

《C#通过进程调用外部应用的实现示例》本文主要介绍了C#通过进程调用外部应用的实现示例,以WINFORM应用程序为例,在C#应用程序中调用PYTHON程序,具有一定的参考价值,感兴趣的可以了解一下... 目录窗口程序类进程信息类 系统设置类 以WINFORM应用程序为例,在C#应用程序中调用python程序