彩票View的实现

2024-02-22 23:40
文章标签 实现 view 彩票

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

Android 实现Lotto彩票池View的实现

项目地址:https://github.com/Warkey1991/Jackview

最近项目中需要用到一个类似彩票池的效果View
在这里插入图片描述
上图看上去还是很简单的,下面我就分析一下实现的原理:

  • 绘制黑色背景圆
  • 绘制中间的圆弧以及文字
  • 绘制内边圆
  • 绘制里面的小球(此处比较复杂点)
  • 绘制进度圆弧
1.绘制黑色圆背景

就是简单的绘制圆的方法,实心圆只需要将画笔设置为

paint.setStyle(Paint.Style.FILL_AND_STROKE);

即可。

2.绘制中间圆弧

此处需要计算每段圆弧的间距,此处我将每段间距设置为10度,见代码

//绘制圆弧rectF.set(-midRadius, -midRadius, midRadius, midRadius);canvas.drawArc(rectF, -85, 80, false, linePaint); //5canvas.drawArc(rectF, 5, 80, false, linePaint);  //10---- 85canvas.drawArc(rectF, 95, 80, false, linePaint);canvas.drawArc(rectF, 185, 80, false, linePaint);
3.绘制文字,

设计绘制文本的时候,需要注意的是确定x,y的值。

canvas.drawText("0", -textPaint.measureText("0") / 2, -midRadius + getTextDiffY(textPaint), textPaint);canvas.drawText("30", midRadius - textPaint.measureText("30") / 2, getTextDiffY(textPaint), textPaint);canvas.drawText("60", -textPaint.measureText("60") / 2, midRadius + getTextDiffY(textPaint), textPaint);canvas.drawText("90", -midRadius - textPaint.measureText("90") / 2, getTextDiffY(textPaint), textPaint);

这里,先确定x的位置,只要将x的坐标减去文本的宽度的一半就OK了;y 的位置稍微复杂点,

Paint.FontMetrics fontMetrics = paint.getFontMetrics();return Math.abs(fontMetrics.descent - fontMetrics.ascent) / 2 - fontMetrics.descent;

确定文本的基线,然后进行绘制就OK了。

4.绘制文本处的短线,

简单粗暴直接代码之

//绘制直线canvas.drawLine(0, -innerRaduis, 0, -midRadius + lineLength, linePaint);canvas.drawLine(innerRaduis, 0, midRadius - lineLength, 0, linePaint);canvas.drawLine(0, innerRaduis, 0, midRadius - lineLength, linePaint);canvas.drawLine(-innerRaduis, 0, -midRadius + lineLength, 0, linePaint);
5.绘制内圆圈

就是绘制圆,将画笔设置为只绘制边框即可

linePaint.setStyle(Paint.Style.STROKE);
6.绘制内部的圆球

以最下面最居中的那个球为基点,先设置球的半径,绘制下面一圈的球,根据球的半径算出每个球所占的角度,涉及到三角函数的知识,(后期补上图做说明)。

 float r = dip2px(18);float midBallDiffY = innerRaduis - dip2px(2) - r;float minInnerRadius = midBallDiffY - r * 2;double eachMidArc = Math.toDegrees(Math.asin(r * 1.0d / midBallDiffY)) * 2;double eachInnerArc = Math.toDegrees(Math.asin(r * 1.0d / minInnerRadius)) * 2;

先绘制最下面一圈

for (int i = 5; i > 0; i--) {Ball ballX = new Ball();float x = (float) ((innerRaduis - dip2px(2) - r) * (Math.sin(Math.toRadians(eachMidArc * (5 - i)))));float y = (float) ((innerRaduis - dip2px(2) - r) * (Math.cos(Math.toRadians(eachMidArc * (5 - i)))));ballX.cx = x;ballX.cy = y;ballX.num = String.valueOf(random.nextInt(69) + 1);ballX.rotateDegress = random.nextInt(90) + 10;canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);canvas.rotate(ballX.rotateDegress, x, y);canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);canvas.rotate(-ballX.rotateDegress, x, y);}for (int i = 4; i > 0; i--) {Ball ballX = new Ball();float x = -(float) ((innerRaduis - dip2px(2) - r) * (Math.sin(Math.toRadians(eachMidArc * (5 - i)))));float y = (float) ((innerRaduis - dip2px(2) - r) * (Math.cos(Math.toRadians(eachMidArc * (5 - i)))));ballX.cx = x;ballX.cy = y;ballX.num = String.valueOf(random.nextInt(69) + 1);canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);ballX.rotateDegress = random.nextInt(90) + 10;canvas.rotate(ballX.rotateDegress, x, y);canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);canvas.rotate(-ballX.rotateDegress, x, y);}

然后绘制上面的一圈

for (int i = 3; i > 0; i--) {Ball ballX = new Ball();float x = (float) (minInnerRadius * (Math.sin(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));float y = (float) (minInnerRadius * (Math.cos(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));ballX.cx = x;ballX.cy = y;ballX.num = String.valueOf(random.nextInt(69) + 1);ballX.rotateDegress = random.nextInt(90) + 10;canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);canvas.rotate(ballX.rotateDegress, x, y);canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);canvas.rotate(-ballX.rotateDegress, x, y);}for (int i = 2; i > 0; i--) {Ball ballX = new Ball();float x = -(float) (minInnerRadius * (Math.sin(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));float y = (float) (minInnerRadius * (Math.cos(Math.PI * (eachInnerArc * (3 - i) / 180.0f))));ballX.cx = x;ballX.cy = y;ballX.num = String.valueOf(random.nextInt(69) + 1);ballX.rotateDegress = random.nextInt(90) + 10;canvas.drawCircle(ballX.cx, ballX.cy, ballX.radius, dashPaint);canvas.rotate(ballX.rotateDegress, x, y);canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);canvas.rotate(-ballX.rotateDegress, x, y);}

看到效果图,每个球的文本都有一个角度的,关键代码将画笔旋转,绘制后再回位。

 canvas.rotate(ballX.rotateDegress, x, y);canvas.drawText(ballX.num, x - textPaint.measureText(ballX.num) / 2, y + getTextDiffY(textPaint), textPaint);canvas.rotate(-ballX.rotateDegress, x, y);
7.绘制进度条
public class LottoCircleProgressView extends View {private int innerRaduis = 200;private Paint progressPaint = new Paint(Paint.ANTI_ALIAS_FLAG);private int progress = 0;private RectF arcRectf = new RectF();private Bitmap iconBitmap;private Rect bitmapRect;public LottoCircleProgressView(Context context) {this(context, null);}public LottoCircleProgressView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public LottoCircleProgressView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);progressPaint.setColor(0xFFF7A400);progressPaint.setAntiAlias(true);progressPaint.setStrokeCap(Paint.Cap.ROUND);progressPaint.setStrokeJoin(Paint.Join.ROUND);progressPaint.setStyle(Paint.Style.STROKE);progressPaint.setStrokeWidth(dip2px(4));iconBitmap = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_progress_ball);bitmapRect = new Rect();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);int size = Math.min(width, height);setMeasuredDimension(size, size);innerRaduis = size / 2 - dip2px(30);}@Overrideprotected void onDraw(Canvas canvas) {canvas.translate(getWidth() / 2, getHeight() / 2);arcRectf.set(-innerRaduis, -innerRaduis, innerRaduis, innerRaduis);canvas.drawArc(arcRectf, -90, progress, false, progressPaint);int left = (int) (innerRaduis * Math.sin(Math.PI * (double) (progress * 1.0f / 180)) - (iconBitmap.getWidth() + 20) / 2);int top = (int) (-innerRaduis * Math.cos(Math.PI * (double) (progress * 1.0f / 180)) - (iconBitmap.getHeight() + 20) / 2);int right = (int) (innerRaduis * Math.sin(Math.PI * (double) (progress * 1.0f / 180)) + (iconBitmap.getWidth() + 20) / 2);int bottom = (int) (-innerRaduis * Math.cos(Math.PI * (double) (progress * 1.0f / 180)) + (iconBitmap.getHeight() + 20) / 2);bitmapRect.set(left, top, right, bottom);canvas.drawBitmap(iconBitmap, null, bitmapRect, null);super.onDraw(canvas);}public static int dip2px(float dipValue) {final float scale = Resources.getSystem().getDisplayMetrics().density;return (int) (dipValue * scale + 0.5f);}@Overrideprotected void onAttachedToWindow() {super.onAttachedToWindow();startProgressAnim();}@Overrideprotected void onDetachedFromWindow() {super.onDetachedFromWindow();clearAnimation();}private void startProgressAnim() {ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);valueAnimator.setDuration(60000);valueAnimator.setInterpolator(new LinearInterpolator());valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {progress = ((int) animation.getAnimatedValue());invalidate();}});valueAnimator.setRepeatCount(ValueAnimator.INFINITE);valueAnimator.setRepeatMode(ValueAnimator.RESTART);valueAnimator.start();}}

这篇关于彩票View的实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

Flutter实现文字镂空效果的详细步骤

《Flutter实现文字镂空效果的详细步骤》:本文主要介绍如何使用Flutter实现文字镂空效果,包括创建基础应用结构、实现自定义绘制器、构建UI界面以及实现颜色选择按钮等步骤,并详细解析了混合模... 目录引言实现原理开始实现步骤1:创建基础应用结构步骤2:创建主屏幕步骤3:实现自定义绘制器步骤4:构建U

SpringBoot中四种AOP实战应用场景及代码实现

《SpringBoot中四种AOP实战应用场景及代码实现》面向切面编程(AOP)是Spring框架的核心功能之一,它通过预编译和运行期动态代理实现程序功能的统一维护,在SpringBoot应用中,AO... 目录引言场景一:日志记录与性能监控业务需求实现方案使用示例扩展:MDC实现请求跟踪场景二:权限控制与

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义