我是靠谱客的博主 细心太阳,这篇文章主要介绍Android MarkTipsView文字标识控件使用方法,现在分享给大家,希望可以做个参考。

本文实例为大家分享了Android MarkTipsView文字标识控件的具体代码,供大家参考,具体内容如下

效果预览

使用

使用方式跟TextView使用方式一样,背景设置直接setBackgroundColor

源码:

复制代码
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.RectF; import android.util.AttributeSet; import androidx.appcompat.widget.AppCompatTextView; /** * 绘制mark标注 */ public class MarkTipsView extends AppCompatTextView { /** * 画笔 */ private Paint paint; /** * 宽度 */ private int width; /** * 高度 */ private int height; /** * 标识宽度 */ private int markWidth = 12; /** * 标识高度 */ private int markHeight = 12; /** * 圆角大小 */ private float radius = 10; /** * 填充颜色 */ private int solid = Color.parseColor("#F1AA44"); public MarkTipsView(Context context) { super(context); initAttributeSet(context, null); } public MarkTipsView(Context context, AttributeSet attrs) { super(context, attrs); initAttributeSet(context, attrs); } public MarkTipsView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initAttributeSet(context, attrs); } private void initAttributeSet(Context context, AttributeSet attrs) { paint = new Paint(); paint.setDither(true); paint.setAntiAlias(true); paint.setStyle(Paint.Style.FILL); TypedArray array = context.obtainStyledAttributes(attrs,new int[]{android.R.attr.background}); solid = array.getInt(0,solid); array.recycle(); } @Override public void setBackgroundColor(int color) { solid = color; invalidate(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); width = getMeasuredWidth(); height = getMeasuredHeight(); } @Override protected void onFinishInflate() { super.onFinishInflate(); setPadding(getPaddingLeft(), getPaddingTop(), getPaddingRight(), getPaddingBottom() + markHeight); } @Override protected void onDraw(Canvas canvas) { paint.setColor(solid); //矩形 Path path = new Path(); RectF rect = new RectF(0, 0, width, height - markHeight); path.addRoundRect(rect, radius, radius, Path.Direction.CCW); //三角形 Path onePath = new Path(); onePath.lineTo(width / 2 - markWidth, rect.height()); onePath.lineTo(width / 2, height); onePath.lineTo(width / 2 + markWidth, rect.height()); onePath.close(); path.addPath(onePath); //绘制路径 canvas.drawPath(path, paint); super.onDraw(canvas); //重绘文字 setText(getText()); } /** * 获取底部标识箭头高度 * @return */ public int getMarkHeight() { return markHeight; } /** * 设置底部标识箭头高度 * @param markHeight */ public void setMarkHeight(int markHeight) { this.markHeight = markHeight; invalidate(); } /** * 获取底部标识箭头宽度 * @return */ public int getMarkWidth() { return markWidth; } /** * 设置底部标识箭头宽度 * @param markWidth */ public void setMarkWidth(int markWidth) { this.markWidth = markWidth; invalidate(); } /** * 获取圆角大小 * @return */ public float getRadius() { return radius; } /** * 设置圆角大小 * @param radius */ public void setRadius(float radius) { this.radius = radius; invalidate(); } }

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

最后

以上就是细心太阳最近收集整理的关于Android MarkTipsView文字标识控件使用方法的全部内容,更多相关Android内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(124)

评论列表共有 0 条评论

立即
投稿
返回
顶部