Android 魔術字型 (背景陰影,外陰影效果)
阿新 • • 發佈:2018-12-10
先隨便貼兩張效果圖
圖 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