这篇文章主要讲解了“Android怎么自定义view实现科技感的仪表盘”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Android怎么自定义view实现科技感的仪表盘”吧!
View 需要用到发光的效果,我们采用 shader 来实现,那么我们需要首先关闭硬件加速。
// 关闭硬件加速 setLayerType(LAYER_TYPE_SOFTWARE, null);
1. 绘制一个发光的弧形
canvas.translate(getPaddingLeft() + radiusDial, getPaddingTop() + radiusDia arcPaint.setShader(null); arcPaint.setStyle(Paint.Style.STROKE); arcPaint.setAntiAlias(true); arcPaint.setAlpha(70); arcPaint.setStyle(Paint.Style.STROKE); arcPaint.setStrokeWidth(strokeWidthDial); arcPaint.setShadowLayer(10, 0, 0, Color.parseColor("#FFFFFF")); arcPaint.setColor(Color.parseColor("#38F9FD")); canvas.drawArc(mRect, 150, (360 - openAngle), false, arcPaint);
2. 绘制刻度和数字
canvas.rotate(150); for (int i = 0; i < clockPointNum + 1; i++) { pointerPaint.setColor(colorDialMiddle); if (i % 10 == 0) { //长表针 pointerPaint.setStrokeWidth(3); canvas.drawLine(radiusDial - DEFAULT_border - strokeWidthDial, 0, radiusDial - strokeWidthDial - dp2px(15), 0, pointerPaint); drawPointerText(canvas, i); } else if (i % 5 == 0f) { //短表针 pointerPaint.setStrokeWidth(2); canvas.drawLine(radiusDial - DEFAULT_border - strokeWidthDial, 0, radiusDial - strokeWidthDial - dp2px(9), 0, pointerPaint); } canvas.rotate((360 - openAngle) / clockPointNum); } canvas.rotate(-((180 - openAngle) / 2 + ((360 - openAngle) / clockPointNum)));
/** * 绘制刻度数字 */ private void drawPointerText(Canvas canvas, int i) { canvas.save(); pointerPaint.setColor(getResources().getColor(R.color.white)); int currentCenterX = (int) (radiusDial - strokeWidthDial - dp2px(21) - pointerPaint.measureText(String.valueOf(i)) / 2); canvas.translate(currentCenterX, 0); canvas.rotate(360 - 150 - ((360 - openAngle) / clockPointNum) * i); //坐标系总旋转角度为360度 int textBaseLine = (int) (0 + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom); canvas.drawText(String.valueOf(i + clockMinValue), 0, textBaseLine, pointerPaint); }
3. 绘制指针阴影
int currentDegree = (int) ((currentValue - clockMinValue) * ((360 - openAngle) / clockPointNum) + 150); canvas.rotate(currentDegree); int[] colorSweep = {0xAAFFE9EC, 0x0028E9EC, 0xAA28E9EC}; float[] position = {0f, 0.9f, 1f}; SweepGradient mShader = new SweepGradient(0, 0, colorSweep, position); arcPaint.setShader(mShader); arcPaint.setStyle(Paint.Style.STROKE); arcPaint.setStrokeWidth((float) (radiusDial * 0.4)); arcPaint.clearShadowLayer(); RectF mRect = new RectF((float) (-mRealRadius - DEFAULT_border + radiusDial * 0.2), (float) (-mRealRadius - DEFAULT_border + radiusDial * 0.2), (float) (mRealRadius + DEFAULT_border - radiusDial * 0.2), (float) (mRealRadius + DEFAULT_border - radiusDial * 0.2)); canvas.drawArc(mRect, 360 - (currentDegree - 150), (currentDegree - 150), false, arcPaint);
</pre>
4. 绘制中间黑色圆形背景
canvas.restore(); canvas.translate(getPaddingLeft() + radiusDial, getPaddingTop() + radiusDial); Paint pointerPaint = new Paint(); pointerPaint.setAntiAlias(true); pointerPaint.setStyle(Paint.Style.FILL); pointerPaint.setColor(Color.parseColor("#05002D")); canvas.drawCircle(0, 0, (float) (radiusDial * 0.6), pointerPaint);
5. 绘制表针
canvas.save(); int currentDegree = (int) ((currentValue - clockMinValue) * ((360 - openAngle) / clockPointNum) + 150); canvas.rotate(currentDegree); titlePaint.setColor(Color.WHITE); titlePaint.setAntiAlias(true); pointerPath.moveTo(radiusDial - dp2px(12), 0); pointerPath.lineTo(0, -dp2px(5)); pointerPath.lineTo(-12, 0); pointerPath.lineTo(0, dp2px(5)); pointerPath.close(); canvas.drawPath(pointerPath, titlePaint); canvas.save(); canvas.restore();
6. 绘制深蓝色发光圆形
canvas.rotate(0); canvas.restore(); Paint pointerPaint = new Paint(); pointerPaint.setAntiAlias(true); pointerPaint.setStyle(Paint.Style.FILL); pointerPaint.setColor(Color.parseColor("#050D3D")); pointerPaint.setShadowLayer(15, 0, 0, Color.parseColor("#006EC6")); canvas.drawCircle(0, 0, (float) (radiusDial * 0.4), pointerPaint);
7. 绘制表盘文字
titlePaint.setColor(Color.WHITE); titlePaint.setColor(titleDialColor); titlePaint.setTextSize(titleDialSize); canvas.drawText(formatData(currentValue), 0, 0, titlePaint); titlePaint.setColor(Color.parseColor("#38F9FD")); titlePaint.setTextSize(sp2px(14)); canvas.drawText("(" + dataUnit + ")", 0, dp2px(18), titlePaint);
8. 添加底部控件
这部分代码就比较灵活了,仪表盘主体绘制出来以后,可以在布局文件中增加其它底部控件。并设置相应点击事件等。在此略过不表
感谢各位的阅读,以上就是“Android怎么自定义view实现科技感的仪表盘”的内容了,经过本文的学习后,相信大家对Android怎么自定义view实现科技感的仪表盘这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。