Android 9.0效果图:
Android 10.0效果图:
1.StrokeTextView的实现
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
;
public class StrokeTextViewextends AppCompatTextView
{
private TextViewbackGroundText
=null
;
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
) {
backGroundText
.setLayoutParams(params
);
super.setLayoutParams(params
);
}
@Override
protected void onMeasure(int widthMeasureSpec
, int heightMeasureSpec
) {
CharSequence tt
=backGroundText
.getText();
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
) {
init();
backGroundText
.draw(canvas
);
super.onDraw(canvas
);
}
public void init() {
TextPaint tp1
=backGroundText
.getPaint();
tp1
.setStrokeWidth(8);
tp1
.setStyle(Paint
.Style
.FILL_AND_STROKE
);
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"
/>
转载请注明原文地址: https://lol.8miu.com/read-38112.html