Android 9.0效果圖:?

Android 10.0效果圖:?

1.StrokeTextView的實現(xiàn)
package com.app.animalchess.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.appcompat.widget.AppCompatTextView;
import com.app.animalchess.R;
/**
* @Author : XiaoXred
* @Time : On 2020/10/22 15:59
* @Description : StrokeTextView? 文字內(nèi)容有描邊的TextView
*/
public class StrokeTextViewextends AppCompatTextView {
private TextViewbackGroundText =null;//用于描邊的TextView
? ? public StrokeTextView(Context context) {
this(context, null);
? ? }
public StrokeTextView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
? ? }
public StrokeTextView(Context context, AttributeSet attrs,
? ? ? ? ? ? ? ? ? ? ? ? ? int defStyle) {
super(context, attrs, defStyle);
? ? ? ? backGroundText =new TextView(context, attrs, defStyle);
? ? }
@Override
? ? public void setLayoutParams(ViewGroup.LayoutParams params) {
//同步布局參數(shù)
? ? ? ? backGroundText.setLayoutParams(params);
? ? ? ? super.setLayoutParams(params);
? ? }
@Override
? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
CharSequence tt =backGroundText.getText();
? ? ? ? //兩個TextView上的文字必須一致
? ? ? ? if (tt ==null || !tt.equals(this.getText())) {
backGroundText.setText(getText());
? ? ? ? ? ? this.postInvalidate();
? ? ? ? }
backGroundText.measure(widthMeasureSpec, heightMeasureSpec);
? ? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);
? ? }
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
backGroundText.layout(left, top, right, bottom);
? ? ? ? super.onLayout(changed, left, top, right, bottom);
? ? }
@Override
? ? protected void onDraw(Canvas canvas) {
//其他地方,backGroundText和super的先后順序影響不會很大,但是此處必須要先繪制backGroundText,
? ? ? ? init();
? ? ? ? backGroundText.draw(canvas);
? ? ? ? super.onDraw(canvas);
? ? }
public void init() {
TextPaint tp1 =backGroundText.getPaint();
? ? ? ? //設(shè)置描邊寬度
? ? ? ? tp1.setStrokeWidth(8);
? ? ? ? //背景描邊并填充全部
? ? ? ? tp1.setStyle(Paint.Style.FILL_AND_STROKE);
? ? ? ? //設(shè)置描邊顏色
? ? ? ? backGroundText.setTextColor(getResources().getColor(R.color.color_CC7B02));
? ? ? ? //將背景的文字對齊方式做同步
? ? ? ? backGroundText.setGravity(getGravity());
? ? }
}
2.xml文件
<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"
? ? android:textColor="@color/color_ffffff"
? ? />