public class gradual extends View {

private int animatedValue;
private int colorEnd;
private int colorStart;

public gradual(Context context) {
    super(context);
    init();
}

public gradual(Context context, @Nullable AttributeSet attrs) {
    super(context, attrs);
    System.out.println(111);
    init();
}

public void init() {

        ValueAnimator animator=ValueAnimator.ofInt(0,255);
        animator.setDuration(20000);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animatedValue = (int) animation.getAnimatedValue();
                colorStart = Color.rgb(255-animatedValue,animatedValue,255-animatedValue);
                colorEnd = Color.rgb(animatedValue,animatedValue,255-animatedValue);
                invalidate();
            }
        });
        animator.start();


}
@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    //获取View的宽高
    int width = getWidth();
    int height = getHeight();

    Paint paint = new Paint();
    LinearGradient backGradient = new LinearGradient(width, 0, 0, 0, new int[]{colorStart,colorEnd}, new float[]{0,1f}, Shader.TileMode.CLAMP);
    paint.setShader(backGradient);

    canvas.drawRect(0, 0, width, height, paint);
}

}

你可能感兴趣的:(他)