52 lines
1.5 KiB
Java
52 lines
1.5 KiB
Java
package com.xscm.moduleutil.widget;
|
|
|
|
import android.content.Context;
|
|
import android.graphics.LinearGradient;
|
|
import android.graphics.Shader;
|
|
import android.util.AttributeSet;
|
|
import androidx.appcompat.widget.AppCompatTextView;
|
|
|
|
/**
|
|
* com.xscm.moduleutil.widget
|
|
* qx 爵位中的文字渐变颜色
|
|
* 2025/11/8
|
|
*/
|
|
public class GradientTextView extends AppCompatTextView {
|
|
|
|
public GradientTextView(Context context) {
|
|
super(context);
|
|
}
|
|
|
|
public GradientTextView(Context context, AttributeSet attrs) {
|
|
super(context, attrs);
|
|
}
|
|
|
|
public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
|
|
super(context, attrs, defStyleAttr);
|
|
}
|
|
|
|
@Override
|
|
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
|
|
super.onLayout(changed, left, top, right, bottom);
|
|
if (changed) {
|
|
createGradient();
|
|
}
|
|
}
|
|
|
|
private void createGradient() {
|
|
// 获取TextView的宽度
|
|
int width = getWidth();
|
|
if (width > 0) {
|
|
// 创建从左到右的线性渐变 #A292FF -> #FFFFFF -> #A292FF
|
|
LinearGradient gradient = new LinearGradient(
|
|
0, 0, width, 0,
|
|
new int[]{0xFFA292FF, 0xFFFFFFFF, 0xFFA292FF},
|
|
new float[]{0f, 0.5f, 1f},
|
|
Shader.TileMode.CLAMP
|
|
);
|
|
getPaint().setShader(gradient);
|
|
invalidate();
|
|
}
|
|
}
|
|
}
|