//闪光的textview
public class ShineTextView extends TextView {
// LinearGradient线性渲染
private LinearGradient mLinearGradient;
// RadialGradient环形渲染
private RadialGradient mRadialGradient;
// 矩阵
private Matrix mGradientMatrix;
// 画笔
private Paint mPaint;
private int mViewWidth = 0;
private int mViewHeight = 0;
//
private int mTranslateX = 0;
private int mTranslateY = 0;
private int[] colors;
/**
* 构造方法
* @param context
* @param attrs
*/
public ShineTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//获取自定义style,attr
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.textcolors);
//取得color1
String color1 = ta.getString(R.styleable.textcolors_color1);
//取得color2
String color2 = ta.getString(R.styleable.textcolors_color2);
//取得color3
String color3 = ta.getString(R.styleable.textcolors_color3);
//创建渲染数组
colors = new int[] { Color.parseColor(color1), Color.parseColor(color2), Color.parseColor(color3) };
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0 || mViewHeight == 0) {
mViewWidth = getMeasuredWidth();
mViewHeight = getMeasuredHeight();
if (mViewWidth > 0 || mViewHeight > 0) {
mPaint = getPaint();
/**
* 创建环形渲染
* float x: 圆心X坐标
* float y: 圆心Y坐标
* float radius: 圆的半径
* colors: 渲染颜色数组 floate[] positions:
* 相对位置数组,可为null,若为null,可为null,颜色沿渐变线均匀分布
* Shader.TileMode tile:渲染器平铺模式
*/
mRadialGradient = new RadialGradient(50, 50, 40, colors, new float[] { 0, 0.5f, 1 },
Shader.TileMode.CLAMP);
/**
* 创建线性渲染
* float x0: 渐变起始点x坐标
* float y0:渐变起始点y坐标
* float x1:渐变结束点x坐标
* float y1:渐变结束点y坐标
* int[] colors:颜色 的int 数组
* float[] positions: 相对位置的颜色数组,可为null, 若为null,可为null,颜色沿渐变线均匀分布
* Shader.TileMode tile: 渲染器平铺模式*/
mLinearGradient = new LinearGradient(-mViewWidth, 0, 0, 0, colors, new float[] { 0, 0.5f, 1 },Shader.TileMode.CLAMP);
mPaint.setShader(mRadialGradient);
mPaint.setColor(Color.parseColor("#ffffffff"));
/**
* 绘制阴影
* radius:阴影半径
* dx:X轴方向的偏移量
* dy:Y轴方向的偏移量
* color:阴影颜色
*/
mPaint.setShadowLayer(3, 2, 2, 0xFFFF00FF);
mGradientMatrix = new Matrix();
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mGradientMatrix != null) {
mTranslateX += mViewWidth / 10;
mTranslateY += mViewHeight / 10;
if (mTranslateX > 2 * mViewWidth && mTranslateY > 2 * mViewHeight) {
mTranslateX = -mViewWidth;
mTranslateY = -mViewHeight;
}
mGradientMatrix.setTranslate(mTranslateX, mTranslateY);
// mLinearGradient.setLocalMatrix(mGradientMatrix);
mRadialGradient.setLocalMatrix(mGradientMatrix);
postInvalidateDelayed(50);
}
}
}
在values文件下定义的xml属性:
dimens文件
<resources>
<!-- Default screen margins, per the Android Design guidelines. -->
<dimen name="activity_horizontal_margin">16dp</dimen>
<dimen name="activity_vertical_margin">16dp</dimen>
</resources>
textattrs文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="textcolors">
<attr name="color1" format="string" />
<attr name="color2" format="string" />
<attr name="color3" format="string" />
</declare-styleable>
</resources>
最后是在布局中使用:
<com.example.ilikebasketball1.view.ShineTextView
android:id="@+id/login_sign_in_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|bottom"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:text="注册"
android:textSize="18dp"
android:textStyle="bold"
app:color1="#FBF6D6"
app:color2="#D08AD2"
app:color3="#4C1B83" />
</LinearLayout>
别忘记加上自定义标签
xmlns:app="http://schemas.android.com/apk/res/com.example.ilikebasketball1"