美文网首页
paint高级应用渲染

paint高级应用渲染

作者: 揾食艰难 | 来源:发表于2019-10-09 22:21 被阅读0次

    paint常用API

    // 初始化
    mPaint = new Paint();
    // 设置颜色
    mPaint.setColor(Color.RED);
    // 设置 Paint对象颜色,范围为0~255
    mPaint.setARGB(255, 255, 255, 0); 
    // 设置alpha不透明度,范围为0~255
    mPaint.setAlpha(200); 
    // 抗锯齿
    mPaint.setAntiAlias(true); 
    // 描边效果
    mPaint.setStyle(Paint.Style.FILL); 
    // 描边宽度
    mPaint.setStrokeWidth(4);
    // 圆角效果
    mPaint.setStrokeCap(Paint.Cap.ROUND); 
    // 拐角风格
    mPaint.setStrokeJoin(Paint.Join.MITER);
    // 设置环形渲染器
    mPaint.setShader(new SweepGradient(200, 200, Color.BLUE, Color.RED)); 
    // 设置图层混合模式
    mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DARKEN));
    // 设置颜色过滤器
    mPaint.setColorFilter(new LightingColorFilter(0x00ffff, 0x000000));
    // 设置双线性过滤
    mPaint.setFilterBitmap(true); 
    // 设置画笔遮罩滤镜 ,传入度数和样式
    mPaint.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL));
    // 设置文本缩放倍数
    mPaint.setTextScaleX(2);
    // 设置字体大小
    mPaint.setTextSize(38);
    // 对其方式
    mPaint.setTextAlign(Paint.Align.LEFT);
    // 设置下划线
    mPaint.setUnderlineText(true);
    // 测量文本大小,将文本大小信息存放在rect中
    String str = "Android高级工程师";
    Rect rect = new Rect();
    mPaint.getTextBounds(str, 0, str.length(), rect); 
    // 获取文本的宽
    mPaint.measureText(str); 
    // 获取字体度量对象
    mPaint.getFontMetrics(); 
    

    获取字体度量对象mPaint.getFontMetrics()方法,返回一个FontMetrics类对象

        public static class FontMetrics {
            /**
             * The maximum distance above the baseline for the tallest glyph in
             * the font at a given text size.
             */
            public float   top;
            /**
             * The recommended distance above the baseline for singled spaced text.
             */
            public float   ascent;
            /**
             * The recommended distance below the baseline for singled spaced text.
             */
            public float   descent;
            /**
             * The maximum distance below the baseline for the lowest glyph in
             * the font at a given text size.
             */
            public float   bottom;
            /**
             * The recommended additional space to add between lines of text.
             */
            public float   leading;
        }
    
    1.2.1-Paint_FontMetrics.png
    ==mPaint.setShader==(设置渲染器)介绍使用:

    一般使用shader的子类:

    LinearGradient 线性渲染
    RadialGradient 环形渲染
    SweepGradient 扫描渲染,
    BitmapShader 位图渲染,
    ComposeShader 组合渲染,
    

    LinearGradient线性渲染:

           /**
            * 1.线性渲染,LinearGradient(float x0, float y0, float x1, float y1, @NonNull @ColorInt int colors[], @Nullable float positions[], @NonNull TileMode tile)
            * (x0,y0):渐变起始点坐标
            * (x1,y1):渐变结束点坐标
            * color0:渐变开始点颜色,16进制的颜色表示,必须要带有透明度
            * color1:渐变结束颜色
            * colors:渐变数组
            * positions:位置数组,position的取值范围[0,1],作用是指定某个位置的颜色值,如果传null,渐变就线性变化。
            * tile:用于指定控件区域大于指定的渐变区域时,空白区域的颜色填充方法
            */
           mShader = new LinearGradient(0, 0, 500, 500, new int[]{Color.RED, Color.BLUE, Color.GREEN}, new float[]{0.f,0.7f,1}, Shader.TileMode.REPEAT);
           mPaint.setShader(mShader);
           canvas.drawCircle(250, 250, 250, mPaint);
    

    RadialGradient 环形渲染:

            /**
            * 环形渲染,RadialGradient(float centerX, float centerY, float radius, @ColorInt int colors[], @Nullable float stops[], TileMode tileMode)
            * centerX ,centerY:shader的中心坐标,开始渐变的坐标
            * radius:渐变的半径
            * centerColor,edgeColor:中心点渐变颜色,边界的渐变颜色
            * colors:渐变颜色数组
            * stoops:渐变位置数组,类似扫描渐变的positions数组,取值[0,1],中心点为0,半径到达位置为1.0f
            * tileMode:shader未覆盖以外的填充模式。
            */
           mShader = new RadialGradient(250, 250, 250, new int[]{Color.GREEN, Color.YELLOW, Color.RED}, null, Shader.TileMode.CLAMP);
           mPaint.setShader(mShader);
           canvas.drawCircle(250, 250, 250, mPaint);
    

    SweepGradient 扫描渲染

            /**
            * 环形渲染,RadialGradient(float centerX, float centerY, float radius, @ColorInt int colors[], @Nullable float stops[], TileMode tileMode)
            * centerX ,centerY:shader的中心坐标,开始渐变的坐标
            * radius:渐变的半径
            * centerColor,edgeColor:中心点渐变颜色,边界的渐变颜色
            * colors:渐变颜色数组
            * stoops:渐变位置数组,类似扫描渐变的positions数组,取值[0,1],中心点为0,半径到达位置为1.0f
            * tileMode:shader未覆盖以外的填充模式。
            */
           mShader = new RadialGradient(250, 250, 250, new int[]{Color.GREEN, Color.YELLOW, Color.RED}, null, Shader.TileMode.CLAMP);
           mPaint.setShader(mShader);
           canvas.drawCircle(250, 250, 250, mPaint);
    

    BitmapShader 位图渲染

           /**
            * 位图渲染,BitmapShader(@NonNull Bitmap bitmap, @NonNull TileMode tileX, @NonNull TileMode tileY)
            * Bitmap:构造shader使用的bitmap
            * tileX:X轴方向的TileMode
            * tileY:Y轴方向的TileMode
                  REPEAT, 绘制区域超过渲染区域的部分,重复排版
                  CLAMP, 绘制区域超过渲染区域的部分,会以最后一个像素拉伸排版
                  MIRROR, 绘制区域超过渲染区域的部分,镜像翻转排版
            */
           mShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.MIRROR);
           mPaint.setShader(mShader);
           canvas.drawRect(0,0,500, 500, mPaint);
    

    ComposeShader 组合渲染

            /**
             * 组合渲染,
             * ComposeShader(@NonNull Shader shaderA, @NonNull Shader shaderB, Xfermode mode)
             * ComposeShader(@NonNull Shader shaderA, @NonNull Shader shaderB, PorterDuff.Mode mode)
             * shaderA,shaderB:要混合的两种shader
             * Xfermode mode: 组合两种shader颜色的模式
             * PorterDuff.Mode mode: 组合两种shader颜色的模式
             */
            BitmapShader bitmapShader = new BitmapShader(mBitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
            LinearGradient linearGradient = new LinearGradient(0, 0, 1000, 1600, new int[]{Color.RED, Color.GREEN, Color.BLUE}, null, Shader.TileMode.CLAMP);
            mShader = new ComposeShader(bitmapShader, linearGradient, PorterDuff.Mode.MULTIPLY);
            mPaint.setShader(mShader);
            canvas.drawCircle(250, 250, 250, mPaint);
    

    相关文章

      网友评论

          本文标题:paint高级应用渲染

          本文链接:https://www.haomeiwen.com/subject/wlynpctx.html