Android 9.0效果图:
Android 10.0效果图:
据以上两图显示此方法只用于android9.0可行,第二种方法见Android 中TextView文字描边实现(二)
1.attrs.xml文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16<?xml version="1.0" encoding="utf-8"?> <resources> <!-- 自定义控件的名称 --> <declare-styleable name="StrokeTextView"> <!-- 自定义的属性名称 和对应的单位 --> <attr name="outerColor" format="color|reference" /> <attr name="innnerColor" format="color|reference" /> </resources>
2.StrokeTextView的实现
复制代码
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182package com.app.animalchess.widget; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.text.TextPaint; import android.util.AttributeSet; import android.widget.TextView; import com.app.animalchess.R; import java.lang.reflect.Field; /** * @Author : XiaoXred * @Time : On 2020/10/22 15:59 * @Description : StrokeTextView 文字内容有描边的TextView */ public class StrokeTextViewextends TextView { TextPaintm_TextPaint; int mInnerColor; int mOuterColor; public StrokeTextView(Context context,int outerColor,int innnerColor) { super(context); m_TextPaint =this.getPaint(); this.mInnerColor = innnerColor; this.mOuterColor = outerColor; // TODO Auto-generated constructor stub } public StrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); m_TextPaint =this.getPaint(); //获取自定义的XML属性名称 TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.StrokeTextView); //获取对应的属性值 this.mInnerColor = a.getColor(R.styleable.StrokeTextView_innnerColor,0xffffff); this.mOuterColor = a.getColor(R.styleable.StrokeTextView_outerColor,0xffffff); } public StrokeTextView(Context context, AttributeSet attrs, int defStyle,int outerColor,int innnerColor) { super(context, attrs, defStyle); m_TextPaint =this.getPaint(); this.mInnerColor = innnerColor; this.mOuterColor = outerColor; // TODO Auto-generated constructor stub } private boolean m_bDrawSideLine =true; // 默认采用描边 /** * */ @Override protected void onDraw(Canvas canvas) { if (m_bDrawSideLine) { // 描外层 // super.setTextColor(Color.BLUE); // 不能直接这么设,如此会导致递归 // setTextColorUseReflection(Color.YELLOW); setTextColorUseReflection(getResources().getColor(R.color.color_CC7B02)); m_TextPaint.setStrokeWidth(10); // 描边宽度 m_TextPaint.setStyle(Paint.Style.FILL_AND_STROKE); // 描边种类 m_TextPaint.setFakeBoldText(true); // 外层text采用粗体 //radius:阴影半径 dx:X轴方向的偏移量 dy:Y轴方向的偏移量 color:阴影颜色 m_TextPaint.setShadowLayer(5, 0,0,0); // 字体的阴影效果,可以忽略 super.onDraw(canvas); // 描内层,恢复原先的画笔 // super.setTextColor(Color.BLUE); // 不能直接这么设,如此会导致递归 setTextColorUseReflection(Color.WHITE); m_TextPaint.setStrokeWidth(0); // m_TextPaint.setStyle(Style.FILL_AND_STROKE); m_TextPaint.setFakeBoldText(false); m_TextPaint.setShadowLayer(0, 0, 0, 0); } super.onDraw(canvas); } /** * 使用反射的方法进行字体颜色的设置 * @param color */ private void setTextColorUseReflection(int color) { Field textColorField; try { textColorField = TextView.class.getDeclaredField("mCurTextColor"); textColorField.setAccessible(true); textColorField.set(this, color); textColorField.setAccessible(false); }catch (NoSuchFieldException e) { e.printStackTrace(); }catch (IllegalArgumentException e) { e.printStackTrace(); }catch (IllegalAccessException e) { e.printStackTrace(); } m_TextPaint.setColor(color); } }
3.布局文件中StrokeTextView的使用
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22<com.app.animalchess.widget.StrokeTextView android:layout_width="260dp" android:layout_height="50dp" android:text="登录" android:background="@drawable/login_btn_wchat_nor" android:textSize="18sp" android:gravity="center" android:layout_marginTop="140dp" android:layout_gravity="center" app:outerColor="@color/color_CC7B02" app:innnerColor="@color/color_ffffff" />
最后
以上就是爱笑手链最近收集整理的关于Android 中TextView文字描边实现(一)的全部内容,更多相关Android内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复