矩形底部是圆弧且渐变效果的自定义背景

项目需求:项目中需要一个矩形底部是圆弧,并且有渐变效果的背景。

背景效果:
矩形底部是圆弧且渐变效果的自定义背景_第1张图片
图片.png

自定义背景:
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);
    }
}

你可能感兴趣的:(矩形底部是圆弧且渐变效果的自定义背景)