先随便贴两张效果图
图 1
图 2
可以看到文字变炫了,图2是比较正常的剧情配图,图1是夸张了些是为了更好的让人看出来字体附加的效果是如何实现的,这两图都是对文字增加了外阴影,只不过图1的纵坐标偏移过大了,想实现这个效果只需要一个类即可,代码如下:
public class ShineTextView extends TextView {
private ArrayList<Shadow> outerShadows;
public ShineTextView(Context context) {
super(context);
init(null);
}
public ShineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}
public ShineTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(attrs);
}
public void init(AttributeSet attrs) {
outerShadows = new ArrayList<Shadow>();
if (attrs != null) {
TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.ShineTextView);
if (a.hasValue(R.styleable.ShineTextView_bgShadowColor)) {
float bgRadius = a.getFloat(R.styleable.ShineTextView_bgShadowRadius, 0);
float bgDx = a.getFloat(R.styleable.ShineTextView_bgShadowDx, 0);
float bgDy = a.getFloat(R.styleable.ShineTextView_bgShadowDy, 0);
int bgColor = a.getColor(R.styleable.ShineTextView_bgShadowColor, 0xff000000);
this.addOuterShadow(bgRadius, bgDx, bgDy, bgColor);
}
if (a.hasValue(R.styleable.ShineTextView_outerShadowColor)) {
float radius = a.getFloat(R.styleable.ShineTextView_outerShadowRadius, 0);
float dx = a.getFloat(R.styleable.ShineTextView_outerShadowDx, 0);
float dy = a.getFloat(R.styleable.ShineTextView_outerShadowDy, 0);
int color = a.getColor(R.styleable.ShineTextView_outerShadowColor, 0xff2b88f6);
this.addOuterShadow(radius, dx, dy, color);
}
a.recycle();
}
}
public void updateShadow() {
}
public void addOuterShadow(float r, float dx, float dy, int color) {
outerShadows.add(new ShineTextView.Shadow(r, dx, dy, color));
}
public void clearOuterShadows() {
outerShadows.clear();
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
this.setCompoundDrawables(null, null, null, null);
for (ShineTextView.Shadow shadow : outerShadows) {
this.setShadowLayer(shadow.r, shadow.dx, shadow.dy, shadow.color);
super.onDraw(canvas);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (widthMode == MeasureSpec.EXACTLY) {
} else if (widthMode == MeasureSpec.AT_MOST) {
} else if (widthMode == MeasureSpec.UNSPECIFIED) {
}
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
}
public static class Shadow {
float r;
float dx;
float dy;
int color;
public Shadow(float r, float dx, float dy, int color) {
this.r = r;
this.dx = dx;
this.dy = dy;
this.color = color;
}
}
}
<com.xiaoluobei.policecar.view.view.ShineTextView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="电源正在检测是否开启..."
android:textColor="@color/white"
android:textSize="32sp"
app:outerShadowColor="@color/blue"
app:outerShadowDy="1"
app:outerShadowRadius="25" />
outerShadowColor:外阴影的颜色 outerShadowDy: 外阴影的纵坐标偏移 outerShadowDx: 外阴影的横坐标偏移 outerShadowRadius:外阴影的半径(辐射区大小),范围是0-25
bgShadowColor:背景阴影的颜色 bgShadowDy: 背景阴影的纵坐标偏移 bgShadowDx: 背景阴影的横坐标偏移 bgShadowRadius:背景阴影的半径(辐射区大小),范围是0-25