Android 中TextView文字描邊實現(xiàn)(二)

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"

? ? />

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容