package android.car.ui
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.BlurMaskFilter
import android.graphics.BlurMaskFilter.Blur
import android.graphics.Canvas
import android.text.TextPaint
import android.util.AttributeSet
import android.view.View
import android.widget.TextView
import java.lang.reflect.Field
public class ShadowTextView extends TextView {
static final String TAG = "ShadowTextView"
ColorStateList mShadowColorList
BlurMaskFilter mMaskFilter
boolean mEnableFilter = false
int mShadowColor = 0
float mShadowSize = 0.0f
Blur mBlurType
Field mCurTextColorField
public ShadowTextView(Context context) {
super(context)
init()
}
public ShadowTextView(Context context, AttributeSet attrs) {
super(context, attrs)
init()
}
public ShadowTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle)
init()
}
private void init() {
mCurTextColorField = getField("mCurTextColor")
setLayerType(View.LAYER_TYPE_SOFTWARE, null)
if (mCurTextColorField != null) {
mCurTextColorField.setAccessible(true)
}
}
public void setEnableShadow(boolean enable) {
mEnableFilter = enable
}
public void setShadow(int color, float size, Blur type) {
mShadowColor = color
mShadowSize = size
mBlurType = type
mMaskFilter = new BlurMaskFilter(mShadowSize, mBlurType)
}
private static Field getField(String name) {
try {
Field f = TextView.class.getDeclaredField(name)
return f
} catch (NoSuchFieldException e) {
e.printStackTrace()
}
return null
}
void setCurrColor(int color) {
if (mCurTextColorField != null) {
try {
mCurTextColorField.setInt(this, color)
} catch (IllegalArgumentException e) {
e.printStackTrace()
} catch (IllegalAccessException e) {
e.printStackTrace()
}
}
}
@Override
protected void onDraw(Canvas canvas) {
if (mEnableFilter && mMaskFilter != null) {
int oldColor = getCurrentTextColor()
setCurrColor(mShadowColor)
TextPaint tp = getPaint()
tp.setMaskFilter(mMaskFilter)
super.onDraw(canvas)
tp.setMaskFilter(null)
setCurrColor(oldColor)
}
super.onDraw(canvas)
}
}