實作漸層 TextView
21 Feb 2019設置顏色漸層的 TextView
import android.content.Context;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
public class GradientTextView extends android.support.v7.widget.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);
//Setting the gradient if layout is changed
if (changed) {
getPaint().setShader(new LinearGradient(getWidth()/2, 0, getWidth()/2, getHeight(),
ContextCompat.getColor(getContext(), R.color.textColor),
ContextCompat.getColor(getContext(), R.color.textColorEnd),
Shader.TileMode.CLAMP));
}
}
}
LinearGradient參數
LinearGradient(漸層 起點X值, 漸層起點Y值, 漸層終點X值, 漸層終點Y值, 起始顏色, 結束顏色, shader type)