1.纯色圆
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@color/text_color_fc" />
<size
android:width="45dp"
android:height="45dp"></size>
</shape>
2.空心圆
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="28dp"
android:shape="ring"
android:thickness="2dp"
android:useLevel="false">
<solid android:color="@color/text_color_fc" />
</shape>
3.纯色矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/text_color_fc" />
</shape>
4.空心矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 边框 -->
<stroke
android:width="2dp"
android:color="@color/text_color_fc" />
<!-- 形状颜色(如果需要的是空心矩形, 则形状颜色设置为透明) -->
<solid android:color="#00000000" />
</shape>
5.圆角矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/text_color_fc" />
<corners android:radius="10dp" />
</shape>
6.圆角空心矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 边框 -->
<stroke
android:width="2dp"
android:color="@color/text_color_fc" />
<!-- 形状颜色(如果需要的是空心矩形, 则形状颜色设置为透明) -->
<solid android:color="#00000000" />
<corners android:radius="10dp" />
</shape>
7.半角圆角矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/text_color_fc" />
<corners android:radius="50dp" />
</shape>
8.半角空心矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<!-- 边框 -->
<stroke
android:width="2dp"
android:color="@color/text_color_fc" />
<!-- 形状颜色(如果需要的是空心矩形, 则形状颜色设置为透明) -->
<solid android:color="#00000000" />
<corners android:radius="50dp" />
</shape>
9.变色背景矩形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="@color/text_color_fc" />
<corners android:radius="50dp" />
<gradient
android:angle="0"
android:centerColor="@color/text_color_fc"
android:endColor="@color/colorAccent"
android:startColor="@color/color_87d1ab"></gradient>
</shape>
10.文字变色功能
public class ColorTextView extends AppCompatTextView {
private int mViewWidth;
private Paint mPaint;
private LinearGradient mLinearGradient;
private Matrix mMatrix;
private int mTranslate;
public ColorTextView(Context context) {
super(context);
}
public ColorTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public ColorTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0) {
mViewWidth = getMeasuredWidth();
}
if (mViewWidth > 0) {
mPaint = getPaint();
mLinearGradient = new LinearGradient(
0,
0,
mViewWidth,
0,
new int[]{Color.BLUE, Color.BLACK, Color.RED, Color.YELLOW},
null, Shader.TileMode.MIRROR);
mPaint.setShader(mLinearGradient);
mMatrix = new Matrix();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mMatrix != null) {
mTranslate += mViewWidth / 5;
if (mTranslate > 2 * mViewWidth) {
mTranslate = -mViewWidth;
}
mMatrix.setTranslate(mTranslate, 0);
mLinearGradient.setLocalMatrix(mMatrix);
postInvalidateDelayed(100);
}
}