本文实例讲述了Android编程使用自定义View实现水波进度效果。分享给大家供大家参考,具体如下:
首先上效果图:
简介:
自动适应屏幕大小;
2.水波自动横向滚动;
3.各种绘制参数可通过修改常量进行控制。
代码不多,注释也比较详细,全部贴上:
(一)自定义组件:
public class WaterWaveView extends View { //边框宽度 private int STROKE_WIDTH; //组件的宽,高 private int width, height; private float max, progress; private Paint progressPaint; //波纹振幅与半径之比。(建议设置:<0.1) private static final float A = 0.05f; //绘制文字的画笔 private Paint textPaint; //绘制边框的画笔 private Paint circlePaint; private int centerX, centerY; //内圆所在的矩形 private RectF circleRectF; public WaterWaveView(Context context) { super(context); init(); } public WaterWaveView(Context context, AttributeSet attrs) { super(context, attrs); init(); } public WaterWaveView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } //初始化 private void init() { progressPaint = new Paint(); progressPaint.setColor(Color.parseColor("#77cccc88")); progressPaint.setAntiAlias(true); textPaint = new Paint(); textPaint.setColor(Color.WHITE); textPaint.setAntiAlias(true); circlePaint = new Paint(); circlePaint.setStyle(Paint.Style.STROKE); circlePaint.setAntiAlias(true); circlePaint.setColor(Color.parseColor("#33333333")); autoRefresh(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); if (width == 0 || height == 0) { width = getWidth(); height = getHeight(); //计算圆弧半径和圆心点 int circleRadius = Math.min(width, height) >> 1; STROKE_WIDTH = circleRadius / 10; circlePaint.setStrokeWidth(STROKE_WIDTH); centerX = width / 2; centerY = height / 2; VALID_RADIUS = circleRadius - STROKE_WIDTH; RADIANS_PER_X = (float) (Math.PI / VALID_RADIUS); circleRectF = new RectF(centerX - VALID_RADIUS, centerY - VALID_RADIUS, centerX + VALID_RADIUS, centerY + VALID_RADIUS); } } private Rect textBounds = new Rect(); //x方向偏移量 private int xOffset; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制圆形边框 canvas.drawCircle(centerX, centerY, VALID_RADIUS + (STROKE_WIDTH >> 1), circlePaint); //绘制水波曲线 canvas.drawPath(getWavePath(xOffset), progressPaint); //绘制文字 textPaint.setTextSize(VALID_RADIUS >> 1); String text1 = String.valueOf(progress); //测量文字长度 float w1 = textPaint.measureText(text1); //测量文字高度 textPaint.getTextBounds("8", 0, 1, textBounds); float h2 = textBounds.height(); float extraW = textPaint.measureText("8") / 3; canvas.drawText(text1, centerX - w1 / 2 - extraW, centerY + h2 / 2, textPaint); textPaint.setTextSize(VALID_RADIUS / 6); textPaint.getTextBounds("M", 0, 1, textBounds); float h3 = textBounds.height(); canvas.drawText("M", centerX + w1 / 2 - extraW + 5, centerY - (h2 / 2 - h3), textPaint); String text3 = "共" + String.valueOf(max) + "M"; float w3 = textPaint.measureText(text3, 0, text3.length()); textPaint.getTextBounds("M", 0, 1, textBounds); float h4 = textBounds.height(); canvas.drawText(text3, centerX - w3 / 2, centerY + (VALID_RADIUS >> 1) + h4 / 2, textPaint); String text4 = "流量剩余"; float w4 = textPaint.measureText(text4, 0, text4.length()); textPaint.getTextBounds(text4, 0, text4.length(), textBounds); float h5 = textBounds.height(); canvas.drawText(text4, centerX - w4 / 2, centerY - (VALID_RADIUS >> 1) + h5 / 2, textPaint); } //绘制水波的路径 private Path wavePath; //每一个像素对应的弧度数 private float RADIANS_PER_X; //去除边框后的半径(即内圆半径) private int VALID_RADIUS; private Path getWavePath(int xOffset) { if (wavePath == null) { wavePath = new Path(); } else { wavePath.reset(); } float[] startPoint = new float[2]; //波浪线起点 float[] endPoint = new float[2]; //波浪线终点 for (int i = 0; i <= VALID_RADIUS * 2; i += 2) { float x = centerX - VALID_RADIUS + i; float y = (float) (centerY + VALID_RADIUS * (1.0f + A) * 2 * (0.5f - progress / max) + VALID_RADIUS * A * Math.sin((xOffset + i) * RADIANS_PER_X)); //只计算内圆内部的点,边框上的忽略 if (calDistance(x, y, centerX, centerY) > VALID_RADIUS) { if (x < centerX) { continue; //左边框,继续循环 } else { break; //右边框,结束循环 } } //第1个点 if (wavePath.isEmpty()) { startPoint[0] = x; startPoint[1] = y; wavePath.moveTo(x, y); } else { wavePath.lineTo(x, y); } endPoint[0] = x; endPoint[1] = y; } if (wavePath.isEmpty()) { if (progress / max >= 0.5f) { //满格 wavePath.moveTo(centerX, centerY - VALID_RADIUS); wavePath.addCircle(centerX, centerY, VALID_RADIUS, Path.Direction.CW); } else { //空格 return wavePath; } } else { //添加圆弧部分 float startDegree = calDegreeByPosition(startPoint[0], startPoint[1]); //0~180 float endDegree = calDegreeByPosition(endPoint[0], endPoint[1]); //180~360 wavePath.arcTo(circleRectF, endDegree - 360, startDegree - (endDegree - 360)); } return wavePath; } private float calDistance(float x1, float y1, float x2, float y2) { return (float) Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)); } //根据当前位置,计算出进度条已经转过的角度。 private float calDegreeByPosition(float currentX, float currentY) { float a1 = (float) (Math.atan(1.0f * (centerX - currentX) / (currentY - centerY)) / Math.PI * 180); if (currentY < centerY) { a1 += 180; } else if (currentY > centerY && currentX > centerX) { a1 += 360; } return a1 + 90; } public void setMax(int max) { this.max = max; invalidate(); } //直接设置进度值(同步) public void setProgressSync(float progress) { this.progress = progress; invalidate(); } private void autoRefresh() { new Thread(new Runnable() { @Override public void run() { while (!detached) { xOffset += (VALID_RADIUS >> 4); SystemClock.sleep(100); postInvalidate(); } } }).start(); } //标记View是否已经销毁 private boolean detached = false; @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); detached = true; }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容- Java 如何通过调用方法来输出数据?(java怎么调用方法输出数据)
- uncomtrade数据库支持的格式大全
- Java OGNL 表达式的解析原理究竟是什么?(java ognl表达式的解析原理是什么 )
- Java泛型中的 extends 操作符对性能有哪些影响?(Java泛型extends的性能影响如何)
- 如何掌握 Java 图形化界面设计原则?(java图形化界面设计原则)
- 在 Java 中如何获取栈顶元素?(java怎么获取栈顶元素)
- 软考高级考试可以自己选批次吗?
- Java 中如何获取字符数组中的字符?(java字符数组怎么获取字符)
- 在 Java 中,JLabel 的文本是否能够实现旋转?(java中jlabel的文本能否实现旋转)
- Java编程中 abstract 类和方法的详细解析与应用指南(java编程abstract类和方法详解)
猜你喜欢
AI推送时光机Android编程使用自定义View实现水波进度效果示例
后端开发2023-05-31
Android自定义view实现水波纹进度球效果
后端开发2022-06-06
Android自定义View实现水波纹效果
后端开发2023-05-30
Android 自定义view实现水波纹动画效果
后端开发2023-05-31
Android自定义View实现水波纹扩散效果
后端开发2024-04-02
Android自定义view实现进度条指示效果
后端开发2022-06-06
Android 通过自定义view实现水波纹效果案例详解
后端开发2024-04-02
Android自定义View 实现水波纹动画引导效果
后端开发2022-06-06
Android自定义WaveView实现波浪进度效果
后端开发2022-06-06
Android自定义view实现圆环进度条效果
后端开发2024-04-02
Android自定义view实现圆形进度条效果
后端开发2024-04-02
Android如何自定义View实现横向的双水波纹进度条
后端开发2024-04-02
Android怎么自定义View实现横向的双水波纹进度条
后端开发2023-06-25
Android自定义View实现钟摆效果进度条PendulumView
后端开发2022-06-06
Android怎么自定义View实现圆弧进度效果
后端开发2023-07-06
Android中怎么自定义view实现圆环进度条效果
后端开发2023-06-29
Android自定义View实现圆弧进度效果逐步完成过程
后端开发2023-05-16
Android利用Paint自定义View实现进度条控件方法示例
后端开发2023-05-30
Android编程基于自定义View实现绚丽的圆形进度条功能示例
后端开发2022-06-06
Python编程使用PyQt5库实现动态水波进度条示例
后端开发2024-04-02
咦!没有更多了?去看看其它编程学习网 内容吧