Paint - 简介

  1. 概念:画笔,保存了绘制几何图形、文本和位图样式颜色信息
  2. 常用API:主要与文本、颜色和效果相关等。

自定义简单的View:

public class GradientView extends View {

    private Paint mPaint; // 画笔
    private Shader mShader; // 渲染器
    private Bitmap mBitmap; // bitmap图片

    public GradientView(Context context) {
        this(context, null);
    }

    public GradientView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

    }
}

Paint的一些方法调用,和使用说明

private void init() {
    mPaint = new Paint(); // 初始化
    mPaint.setColor(Color.RED); // 设置颜色
    mPaint.setARGB(255, 255, 255, 0); // 设置Paint对象颜色
    mPaint.setAlpha(200); // 设置不透明度, 0~255
    mPaint.setAntiAlias(true); // 抗锯齿
    // 设置双线性过滤(滤波,也可起到抗锯齿的效果)
    // Bitmap图片有些颜色过渡,如果过渡过快,可以使用setFilterBitmap
    // 来使它平滑颜色过渡,显示的效果就比较柔和。
    mPaint.setFilterBitmap(true);
    mPaint.setStyle(Paint.Style.STROKE); // 描边效果
    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.setMaskFilter(new BlurMaskFilter(10, BlurMaskFilter.Blur.NORMAL)); 
    mPaint.setTextScaleX(2); // 设置文本缩放倍数
    mPaint.setTextSize(38); // 设置字体大小
    mPaint.setTextAlign(Paint.Align.LEFT); // 设置文本对齐方式
    mPaint.setUnderlineText(true); // 设置下划线
}

Style、Cap、Join、Align

Style

Cap

Join

Align

FontMetrics

FontMetrics

FontMetrics细则

Paint - 文本相关

String str = "Android Paint测试";
Rect rect = new Rect();
// 测量文本大小,将文本大小信息存放在rect中。
mPaint.getTextBounds(str, 0, str.length(), rect);
mPaint.measureText(str); // 获取文本的宽
mPaint.getFontMetrics(); // 获取字体度量对象。
发布了19 篇原创文章 · 获赞 0 · 访问量 869

猜你喜欢

转载自blog.csdn.net/qq_32036981/article/details/103748031