自定View练习第二节

2019-12-13 12:58:38 浏览数 (1)

1.渐变色

代码语言:javascript复制
Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.GREEN);//颜色
        mPaint.setStyle(Paint.Style.STROKE);//类型
        mPaint.setStrokeWidth(25);//线条宽度
        canvas.drawCircle(200,200,100,mPaint);
        Shader shader1 = new LinearGradient(100,100,200,200,Color.BLUE,Color.GREEN,Shader.TileMode.REPEAT);
        mPaint.setShader(shader1);
        canvas.drawCircle(350,200,100,mPaint);
        Shader shader2 = new LinearGradient(100,100,200,200,Color.BLUE,Color.GREEN,Shader.TileMode.MIRROR);
        mPaint.setShader(shader2);
        canvas.drawCircle(500,200,100,mPaint);
        Shader shader3 = new LinearGradient(500,100,650,300,Color.RED,Color.GREEN,Shader.TileMode.CLAMP);
        mPaint.setShader(shader3);
        canvas.drawCircle(650,200,100,mPaint);
        Shader shader4 = new RadialGradient(200,500,100,Color.RED,Color.GREEN,Shader.TileMode.MIRROR);
        mPaint.setShader(shader4);
        mPaint.setStyle(Paint.Style.FILL);//类型
        canvas.drawCircle(200,500,100,mPaint);
    }

2.截图

代码语言:javascript复制
    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setStyle(Paint.Style.FILL);//类型
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bbdalao);
        Shader shader = new BitmapShader(bitmap,Shader.TileMode.MIRROR,Shader.TileMode.MIRROR);
        mPaint.setShader(shader);
        canvas.drawCircle(200,500,350,mPaint);
        canvas.drawCircle(850,500,350,mPaint);
    }

3.模糊

代码语言:javascript复制
    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mPaint.setColor(Color.RED);
        setLayerType(View.LAYER_TYPE_SOFTWARE, mPaint);//关闭硬件加速
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.bbdalao2);
        mPaint.setMaskFilter(new BlurMaskFilter(250, BlurMaskFilter.Blur.NORMAL));
        canvas.drawBitmap(bitmap,50,50,mPaint);

        mPaint.setMaskFilter(new BlurMaskFilter(250, BlurMaskFilter.Blur.INNER));
        canvas.drawBitmap(bitmap,50,850,mPaint);
    }

0 人点赞