服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Android - Android自定义控件实现球赛比分条效果

Android自定义控件实现球赛比分条效果

2022-09-07 15:43王世晖 Android

这篇文章主要为大家详细介绍了Android自定义控件实现球赛比分条效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android实现球赛比分条效果的具体代码,供大家参考,具体内容如下

效果图如下所示:

Android自定义控件实现球赛比分条效果

Android自定义控件实现球赛比分条效果

该控件需要输入两个参数,左边的得分数和右边的的分数

然后根据两边的得分的比例绘制中间的比分条

首先将控件的宽度平均分配为10分,第一份和最后一份分别绘制左边的比分数字和右边的比分数字

中间的8分宽度绘制比分条

根据左右两个比分所占的比例,绘制两个两条首位相连的线段即可

完整代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
public class customscorebar extends view {
 private context context;
 private typedvalue typedvalue;
 private static final int degree =10;
 private int mcolorleft, mcolorright;
 private int mscoreleft, mscoreright;
 //各种画笔
 private paint paintbar =new paint();
 private paint painttext=new paint();
 public customscorebar(context context) {
 super(context);
 this.context=context;
 init();
 }
 
 public customscorebar(context context, attributeset attrs) {
 super(context, attrs);
 this.context=context;
 init();
 }
 
 public customscorebar(context context, attributeset attrs, int defstyleattr) {
 super(context, attrs, defstyleattr);
 this.context=context;
 init();
 }
 
 private void init() {
 //初始化数据,默认属性
 mcolorleft = color.rgb(95, 112, 72);
 mcolorright = color.rgb(69, 91, 136);
 mscoreleft =5;
 mscoreright =8;
 typedvalue=new typedvalue();
 context.gettheme().resolveattribute(r.attr.maintextclor,typedvalue,true);
 }
 
 
 
 @override
 protected void ondraw(canvas canvas) {
 super.ondraw(canvas);
 float width=getwidth();
 float height=getheight();
 //文字画笔设置
 painttext.reset();
 painttext.setantialias(true);
 //文字的大小取控件宽度的十分之一和高度的二分之一的最小值
 painttext.settextsize(math.min(width / degree, height) / 2);
 painttext.setcolor(getresources().getcolor(typedvalue.resourceid));
 /*paint.align.center:the text is drawn centered horizontally on the x,y origin*/
 painttext.settextalign(paint.align.center);
 //绘制中间的横线的画笔
 paintbar.reset();
 paintbar.setantialias(true);
 paintbar.setstyle(paint.style.stroke);
 //画笔的高度为控件高度的十分之一
 paintbar.setstrokewidth(height/10);
 //测量字体
 paint.fontmetrics fontmetrics = painttext.getfontmetrics();
 float textbaselineoffset = (fontmetrics.bottom - fontmetrics.top) / 2 - fontmetrics.bottom;
 //绘制左边的比分文字
 //把控件宽度分为10份,第一份和第十份分别绘制左边和右边的文字
 //中间的8份宽度绘制比分条
 canvas.drawtext("" + mscoreleft, width / degree / 2, height / 2 + textbaselineoffset, painttext);
 paintbar.setcolor(mcolorleft);
 // drawline(float startx, float starty, float stopx, float stopy,paint paint)*/
 //按照比例绘制左边比分对应长度的比分条
 canvas.drawline(width / degree, height / 2, width / degree + width * (degree - 2) / degree * mscoreleft / (mscoreleft + mscoreright), height / 2, paintbar);
 //测量右边的比分文字
 fontmetrics = painttext.getfontmetrics();
 textbaselineoffset = (fontmetrics.bottom - fontmetrics.top) / 2 - fontmetrics.bottom;
 //在控件宽度的最后十分之一绘制右边的比分数字
 canvas.drawtext("" + mscoreright, width-width / degree / 2, height / 2 + textbaselineoffset,painttext);
 paintbar.setcolor(mcolorright);
 //绘制右边的比分对应长度的比分条
 canvas.drawline(width/ degree +width*(degree -2)/ degree * mscoreleft /(mscoreleft + mscoreright),height/2,width*(degree -1)/ degree,height/2, paintbar);
 }
 
 public void setscores(int score1, int score2) {
 this.mscoreleft =score1;
 this.mscoreright =score2;
 invalidate();
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/wangshihui512/article/details/51356897

延伸 · 阅读

精彩推荐