项目需求:项目中需要一个矩形底部是圆弧,并且有渐变效果的背景。
自定义背景:
public class ShaderView extends View {
private Paint mPaint;
private Context mContext;
public ShaderView(Context context) {
super(context, null);
}
public ShaderView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint();
mContext = context;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
RadialGradient radialGradient = new RadialGradient(0, 500, 1000,
mContext.getResources().getColor(R.color.personalCenterHeadStart),
mContext.getResources().getColor(R.color.personalCenterHeadEnd), Shader.TileMode.CLAMP);
mPaint.setShader(radialGradient);
int width = getWidth();
int height = getHeight() - 50;
int heightAdd = 50;
canvas.drawRect(0, 0, width, height, mPaint);
RectF rectF = new RectF(0, height - heightAdd, width, height + heightAdd);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawArc(rectF, 0, 180, true, mPaint);
}
}