I would like to briefly mention that these small modules were not original by me, but were also found by searching for information at that time. Because it took a long time, I had forgotten the original link, so I will not list the cited links here. However, I have modified and improved these codes and added some comments, hoping that they will be helpful to everyone.
The function of text stroke is quite practical. If text is displayed under a single background, text strokes can also serve as decorative functions. If the text is displayed in a complex background, especially in different picture backgrounds, the text color is easily similar to the picture background, which leads to the text being difficult to see clearly and the user experience is poor. If the text is stroked with different colors, the text outline part will have one color and another color inside the text, because generally, the picture will either be similar to the text outline or the color inside the text. In this way, no matter how complex the background of the picture is, the text will be displayed as a whole.
The method I use here is to rewriteTextView
Way.
The following is the relevant code, which is relatively simple and easy to understand.
The inherited TextView text stroke class is as follows:
public class StrokeTextView extends TextView { private TextView outlineTextView = null; public StrokeTextView(Context context) { super(context); outlineTextView = new TextView(context); init(); } public StrokeTextView(Context context, AttributeSet attrs) { super(context, attrs); outlineTextView = new TextView(context, attrs); init(); } public StrokeTextView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); outlineTextView = new TextView(context, attrs, defStyle); init(); } public void init() { TextPaint paint = (); (3);// Stroke width (); (("#45c01a");// Stroke color (getGravity()); } @Override public void setLayoutParams ( params) { (params); (params); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { (widthMeasureSpec, heightMeasureSpec); // Set outline text CharSequence outlineText = (); if (outlineText == null || !(())) { (getText()); postInvalidate(); } (widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout (boolean changed, int left, int top, int right, int bottom) { (changed, left, top, right, bottom); (left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { (canvas); (canvas); } }
The layout file is as follows:
< android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:textSize="25sp" android:textColor="@color/dark_gray" android:text="@string/hello_world" />
The call code is as follows:
private StrokeTextView test_stroketextview = null; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); test_stroketextview = (StrokeTextView)findViewById(.test_stroketextview); test_stroketextview.setText("Hello world!"); }
If you want to change the text stroke width or stroke color, you need to modify the aboveStrokeTextView
Of course, this class can also be designed more flexible, so that the stroke width or stroke color can be dynamically modified.
The above is an implementation example of the Chinese text stroke function of Android. I hope this article will be helpful to everyone in learning Android development. Please support me more.