取自https://github.com/m5/MagicTextView,感谢m5
设置一个attrs.xml
复制代码
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<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MagicTextView"> <attr name="innerShadowColor" format="color"/> <attr name="innerShadowRadius" format="float"/> <attr name="innerShadowDx" format="float"/> <attr name="innerShadowDy" format="float"/> <attr name="outerShadowColor" format="color"/> <attr name="outerShadowRadius" format="float"/> <attr name="outerShadowDx" format="float"/> <attr name="outerShadowDy" format="float"/> <attr name="typeface" format="string" /> <attr name="foreground" format="reference|color"/> <attr name="background" format="reference|color"/> <attr name="strokeWidth" format="float" /> <attr name="strokeMiter" format="float" /> <attr name="strokeColor" format="color" /> <attr name="strokeJoinStyle"> <enum name="miter" value="0" /> <enum name="bevel" value="1" /> <enum name="round" value="2" /> </attr> </declare-styleable> </resources>
然后自定义我们的TextView类:
复制代码
这类中的描边是内描边,如果要外描边的话
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329import java.util.ArrayList; import java.util.WeakHashMap; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.BlurMaskFilter; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Join; import android.graphics.Paint.Style; import android.graphics.PorterDuff; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.Typeface; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.ColorDrawable; import android.graphics.drawable.Drawable; import android.text.TextPaint; import android.util.AttributeSet; import android.util.Pair; import android.widget.TextView; import com.carcon.navi.uitools.ScreenMeasure; import com.carcon.ui.R; public class MagicTextView extends TextView { private ArrayList<Shadow> outerShadows; private ArrayList<Shadow> innerShadows; private WeakHashMap<String, Pair<Canvas, Bitmap>> canvasStore; private Canvas tempCanvas; private Bitmap tempBitmap; private Drawable foregroundDrawable; private float strokeWidth; private Integer strokeColor; private Join strokeJoin; private float strokeMiter; private int[] lockedCompoundPadding; private boolean frozen = false; public MagicTextView(Context context) { super(context); init(null); } public MagicTextView(Context context, AttributeSet attrs) { super(context, attrs); init(attrs); } public MagicTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(attrs); } public void init(AttributeSet attrs){ outerShadows = new ArrayList<Shadow>(); innerShadows = new ArrayList<Shadow>(); if(canvasStore == null){ canvasStore = new WeakHashMap<String, Pair<Canvas, Bitmap>>(); } if(attrs != null){ TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.MagicTextView); String typefaceName = a.getString( R.styleable.MagicTextView_typeface); if(typefaceName != null) { Typeface tf = Typeface.createFromAsset(getContext().getAssets(), String.format("fonts/%s.ttf", typefaceName)); setTypeface(tf); } if(a.hasValue(R.styleable.MagicTextView_foreground)){ Drawable foreground = a.getDrawable(R.styleable.MagicTextView_foreground); if(foreground != null){ this.setForegroundDrawable(foreground); }else{ this.setTextColor(a.getColor(R.styleable.MagicTextView_foreground, 0xff000000)); } } if(a.hasValue(R.styleable.MagicTextView_background)){ Drawable background = a.getDrawable(R.styleable.MagicTextView_background); if(background != null){ this.setBackgroundDrawable(background); }else{ this.setBackgroundColor(a.getColor(R.styleable.MagicTextView_background, 0xff000000)); } } if(a.hasValue(R.styleable.MagicTextView_innerShadowColor)){ this.addInnerShadow(a.getFloat(R.styleable.MagicTextView_innerShadowRadius, 0), a.getFloat(R.styleable.MagicTextView_innerShadowDx, 0), a.getFloat(R.styleable.MagicTextView_innerShadowDy, 0), a.getColor(R.styleable.MagicTextView_innerShadowColor, 0xff000000)); } if(a.hasValue(R.styleable.MagicTextView_outerShadowColor)){ this.addOuterShadow(a.getFloat(R.styleable.MagicTextView_outerShadowRadius, 0), a.getFloat(R.styleable.MagicTextView_outerShadowDx, 0), a.getFloat(R.styleable.MagicTextView_outerShadowDy, 0), a.getColor(R.styleable.MagicTextView_outerShadowColor, 0xff000000)); } if(a.hasValue(R.styleable.MagicTextView_strokeColor)){ float strokeWidth = a.getFloat(R.styleable.MagicTextView_strokeWidth, 1); int strokeColor = a.getColor(R.styleable.MagicTextView_strokeColor, 0xff000000); float strokeMiter = a.getFloat(R.styleable.MagicTextView_strokeMiter, 10); Join strokeJoin = null; switch(a.getInt(R.styleable.MagicTextView_strokeJoinStyle, 0)){ case(0): strokeJoin = Join.MITER; break; case(1): strokeJoin = Join.BEVEL; break; case(2): strokeJoin = Join.ROUND; break; } this.setStroke(strokeWidth, strokeColor, strokeJoin, strokeMiter); } } } public void setStroke(float width, int color, Join join, float miter){ strokeWidth = width; strokeColor = color; strokeJoin = join; strokeMiter = miter; } public void setStroke(float width, int color){ setStroke(width, color, Join.MITER, 10); } public void addOuterShadow(float r, float dx, float dy, int color){ if(r == 0){ r = 0.0001f; } outerShadows.add(new Shadow(r,dx,dy,color)); } public void addInnerShadow(float r, float dx, float dy, int color){ if(r == 0){ r = 0.0001f; } innerShadows.add(new Shadow(r,dx,dy,color)); } public void clearInnerShadows(){ innerShadows.clear(); } public void clearOuterShadows(){ outerShadows.clear(); } public void setForegroundDrawable(Drawable d){ this.foregroundDrawable = d; } public Drawable getForeground(){ return this.foregroundDrawable == null ? this.foregroundDrawable : new ColorDrawable(this.getCurrentTextColor()); } @Override public void onDraw(Canvas canvas){ super.onDraw(canvas); freeze(); Drawable restoreBackground = this.getBackground(); Drawable[] restoreDrawables = this.getCompoundDrawables(); int restoreColor = this.getCurrentTextColor(); this.setCompoundDrawables(null, null, null, null); for(Shadow shadow : outerShadows){ this.setShadowLayer(shadow.r, shadow.dx, shadow.dy, shadow.color); super.onDraw(canvas); } this.setShadowLayer(0,0,0,0); this.setTextColor(restoreColor); if(this.foregroundDrawable != null && this.foregroundDrawable instanceof BitmapDrawable){ generateTempCanvas(); super.onDraw(tempCanvas); Paint paint = ((BitmapDrawable) this.foregroundDrawable).getPaint(); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP)); this.foregroundDrawable.setBounds(canvas.getClipBounds()); this.foregroundDrawable.draw(tempCanvas); canvas.drawBitmap(tempBitmap, 0, 0, null); tempCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); } if(strokeColor != null){ TextPaint paint = this.getPaint(); // paint.setTextAlign(Paint.Align.CENTER); paint.setStyle(Style.STROKE); paint.setStrokeJoin(strokeJoin); paint.setStrokeMiter(strokeMiter); this.setTextColor(strokeColor); paint.setStrokeWidth(strokeWidth); super.onDraw(canvas); paint.setStyle(Style.FILL); this.setTextColor(restoreColor); } if(innerShadows.size() > 0){ generateTempCanvas(); TextPaint paint = this.getPaint(); for(Shadow shadow : innerShadows){ this.setTextColor(shadow.color); super.onDraw(tempCanvas); this.setTextColor(0xFF000000); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT)); paint.setMaskFilter(new BlurMaskFilter(shadow.r, BlurMaskFilter.Blur.NORMAL)); tempCanvas.save(); tempCanvas.translate(shadow.dx, shadow.dy); super.onDraw(tempCanvas); tempCanvas.restore(); canvas.drawBitmap(tempBitmap, 0, 0, null); tempCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR); paint.setXfermode(null); paint.setMaskFilter(null); this.setTextColor(restoreColor); this.setShadowLayer(0,0,0,0); } } if(restoreDrawables != null){ this.setCompoundDrawablesWithIntrinsicBounds(restoreDrawables[0], restoreDrawables[1], restoreDrawables[2], restoreDrawables[3]); } this.setBackgroundDrawable(restoreBackground); this.setTextColor(restoreColor); unfreeze(); } private void generateTempCanvas(){ String key = String.format("%dx%d", getWidth(), getHeight()); Pair<Canvas, Bitmap> stored = canvasStore.get(key); if(stored != null){ tempCanvas = stored.first; tempBitmap = stored.second; }else{ tempCanvas = new Canvas(); tempBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); tempCanvas.setBitmap(tempBitmap); canvasStore.put(key, new Pair<Canvas, Bitmap>(tempCanvas, tempBitmap)); } } // Keep these things locked while onDraw in processing public void freeze(){ lockedCompoundPadding = new int[]{ getCompoundPaddingLeft(), getCompoundPaddingRight(), getCompoundPaddingTop(), getCompoundPaddingBottom() }; frozen = true; } public void unfreeze(){ frozen = false; } @Override public void requestLayout(){ if(!frozen) super.requestLayout(); } @Override public void postInvalidate(){ if(!frozen) super.postInvalidate(); } @Override public void postInvalidate(int left, int top, int right, int bottom){ if(!frozen) super.postInvalidate(left, top, right, bottom); } @Override public void invalidate(){ if(!frozen) super.invalidate(); } @Override public void invalidate(Rect rect){ if(!frozen) super.invalidate(rect); } @Override public void invalidate(int l, int t, int r, int b){ if(!frozen) super.invalidate(l,t,r,b); } @Override public int getCompoundPaddingLeft(){ return !frozen ? super.getCompoundPaddingLeft() : lockedCompoundPadding[0]; } @Override public int getCompoundPaddingRight(){ return !frozen ? super.getCompoundPaddingRight() : lockedCompoundPadding[1]; } @Override public int getCompoundPaddingTop(){ return !frozen ? super.getCompoundPaddingTop() : lockedCompoundPadding[2]; } @Override public int getCompoundPaddingBottom(){ return !frozen ? super.getCompoundPaddingBottom() : lockedCompoundPadding[3]; } public static class Shadow{ float r; float dx; float dy; int color; public Shadow(float r, float dx, float dy, int color){ this.r = r; this.dx = dx; this.dy = dy; this.color = color; } } }
在onDraw里面画完描边字体以后加上
复制代码
1
2paint.setStrokeWidth(0); super.onDraw(canvas);
就ok了
调用方发如下:
xml:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20<com.qwerjk.better_text.MagicTextView xmlns:qwerjk="http://schemas.android.com/apk/res/com.qwerjk.better_text" android:textSize="78dp" android:textColor="#ff333333" android:layout_width="fill_parent" android:layout_height="wrap_content" android:drawableLeft="@android:drawable/btn_star" android:textStyle="bold" android:padding="10dp" qwerjk:foreground="@drawable/fake_luxury_tiled" qwerjk:innerShadowDy="2" qwerjk:innerShadowColor="#FF000000" qwerjk:innerShadowRadius="1" qwerjk:outerShadowDy="3" qwerjk:outerShadowColor="#FF0088ff" qwerjk:outerShadowRadius="10" qwerjk:strokeColor="#FFff0000" qwerjk:strokeJoinStyle="miter" qwerjk:strokeWidth="5" android:text="Sample" />
java:
复制代码
1
2
3
4
5view = new MagicTextView(context); view.addInnerShadow(0, -1, 0, 0xFFffffff); view.addOuterShadow(0, -1, 0, 0xff000000); view.setStroke(4, 0xFFff0000); view.setForegroundDrawable(getResources().getDrawable(R.drawable.fake_luxury_tiled);
最后
以上就是还单身大船最近收集整理的关于外发光、内发光、描边、阴影的TextView的全部内容,更多相关外发光、内发光、描边、阴影内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复