Android 9.0效果图:
Android 10.0效果图:
据以上两图显示此方法只用于android9.0可行,第二种方法见Android 中TextView文字描边实现(二)
1.attrs.xml文件
<?xml version
="1.0" encoding
="utf-8"?>
<resources>
<!-- 自定义控件的名称
-->
<declare
-styleable name
="StrokeTextView">
<!-- 自定义的属性名称 和对应的单位
-->
<attr name
="outerColor" format
="color|reference" />
<attr name
="innnerColor" format
="color|reference" />
</resources
>
2.StrokeTextView的实现
package com
.app
.animalchess
.widget
;
import android
.content
.Context
;
import android
.content
.res
.TypedArray
;
import android
.graphics
.Canvas
;
import android
.graphics
.Color
;
import android
.graphics
.Paint
;
import android
.text
.TextPaint
;
import android
.util
.AttributeSet
;
import android
.widget
.TextView
;
import com
.app
.animalchess
.R
;
import java
.lang
.reflect
.Field
;
public class StrokeTextViewextends TextView
{
TextPaintm_TextPaint
;
int mInnerColor
;
int mOuterColor
;
public StrokeTextView(Context context
,int outerColor
,int innnerColor
) {
super(context
);
m_TextPaint
=this.getPaint();
this.mInnerColor
= innnerColor
;
this.mOuterColor
= outerColor
;
}
public StrokeTextView(Context context
, AttributeSet attrs
) {
super(context
, attrs
);
m_TextPaint
=this.getPaint();
TypedArray a
= context
.obtainStyledAttributes(attrs
,R
.styleable
.StrokeTextView
);
this.mInnerColor
= a
.getColor(R
.styleable
.StrokeTextView_innnerColor
,0xffffff);
this.mOuterColor
= a
.getColor(R
.styleable
.StrokeTextView_outerColor
,0xffffff);
}
public StrokeTextView(Context context
, AttributeSet attrs
, int defStyle
,int outerColor
,int innnerColor
) {
super(context
, attrs
, defStyle
);
m_TextPaint
=this.getPaint();
this.mInnerColor
= innnerColor
;
this.mOuterColor
= outerColor
;
}
private boolean m_bDrawSideLine
=true;
@Override
protected void onDraw(Canvas canvas
) {
if (m_bDrawSideLine
) {
setTextColorUseReflection(getResources().getColor(R
.color
.color_CC7B02
));
m_TextPaint
.setStrokeWidth(10);
m_TextPaint
.setStyle(Paint
.Style
.FILL_AND_STROKE
);
m_TextPaint
.setFakeBoldText(true);
m_TextPaint
.setShadowLayer(5, 0,0,0);
super.onDraw(canvas
);
setTextColorUseReflection(Color
.WHITE
);
m_TextPaint
.setStrokeWidth(0);
m_TextPaint
.setFakeBoldText(false);
m_TextPaint
.setShadowLayer(0, 0, 0, 0);
}
super.onDraw(canvas
);
}
private void setTextColorUseReflection(int color
) {
Field textColorField
;
try {
textColorField
= TextView
.class.getDeclaredField("mCurTextColor");
textColorField
.setAccessible(true);
textColorField
.set(this, color
);
textColorField
.setAccessible(false);
}catch (NoSuchFieldException e
) {
e
.printStackTrace();
}catch (IllegalArgumentException e
) {
e
.printStackTrace();
}catch (IllegalAccessException e
) {
e
.printStackTrace();
}
m_TextPaint
.setColor(color
);
}
}
3.布局文件中StrokeTextView的使用
<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"
app
:outerColor
="@color/color_CC7B02"
app
:innnerColor
="@color/color_ffffff" />