博客
关于我
Android 绘图之Paint
阅读量:625 次
发布时间:2019-03-13

本文共 2876 字,大约阅读时间需要 9 分钟。

Paint在Android中的应用分别介绍

Paint库是Android开发中常用的图形绘制工具,其提供了丰富的功能和属性设置。以下将从不同功能模块详细介绍Paint的使用方法。

一、 Paint的基本使用

1. 获取Paint对象

创建一个Paint对象的基本方式是调用其默认构造方法:

val paint = Paint(Paint.ANTI_ALIAS_FLAG)

2. Paint的主要属性设置

Paint类提供了多种方法来设置属性,主要包括以下几种常用方法:

a. 颜色设置

  • 使用setColor(int color)设置颜色(资源文件内)。
  • 使用setARGB(int a, int r, int g, int b)设置带透明度的颜色。
  • 使用setAlpha(int a)设置透明度。

b. 抗锯齿处理

paint.setAntiAlias(true)

c. 抗抖动处理

paint.setDither(true)

二、文本绘制相关设置

文本绘制时常用的设置方法包括:

a. 字体大小

paint.setTextSize(20f)

b. 字体样式

paint.setTypeface(getTypeFace())

c. 文本对齐方式

paint.setTextAlign(Paint.Align.CENTER)

三、图形绘制相关设置

图形绘制时常用的设置方法包括:

a. 描边宽度

paint.setStrokeWidth(5f)

b. 绘制样式

paint.setStyle(Paint.Style.STROKE)

c. 线帽类型

paint.set.setStrokeCap(Paint.Cap.ROUND)

d. 线段连接方式

paint.setStrokeJoin(Paint.Join.ROUND)

四、Shader渲染效果

Shader增强了绘制效果,常用的方式是通过设置Paint.shader属性。以下是几种常见的Shader类型:

a. LinearGradient线性渐变

val shader = LinearGradient(    0f, 0f,    300f, 0f,    Color.RED, Color.GREEN,    Shader.TileMode.CLAMP)

b. RadialGradient放射渐变

val shader = RadialGradient(    180f, 150f,    150f,    Color.RED, Color.GREEN,    Shader.TileMode.CLAMP)

c. SweepGradient扫描渐变

val shader = SweepGradient(    150f, 150f,    Color.RED, Color.GREEN)

d. BitmapShader位图渲染

val bitmap = BitmapFactory.decodeResource(getResources(), R.drawable bitmap)val shader = BitmapShader(    bitmap,    Shader.TileMode.MIRROR,    Shader.TileMode.MIRROR)

e. ComposeShader组合渲染

val shader = ComposeShader(    bitmapShader,    linearGradient,    PorterDuff.Mode.MULTIPLY)

五、颜色过滤效果

颜色过滤器可以用来调整图像颜色,常用的包括:

a. LightingColorFilter灯光过滤

LightingColorFilter(    ColorConvert enum_RGBToHSV values)

b. ColorMatrixColorFilter颜色矩阵过滤

ColorMatrixColorFilter用于自定义颜色矩阵处理:

val matrix = ColorMatrix()matrix.setSaturation(-100f)val filter = ColorMatrixColorFilter(matrix)

六、PathEffect路径效果

PathEffect专门用于在绘制路径时添加效果,常用的包括:

a. DashPathEffect虚线效果

val effect = DashPathEffect(float[] segments, float phase)

b. CornerPathEffect圆角效果

val effect = CornerPathEffect(32f)

c. DiscretePathEffect切割线段效果

val effect = DiscretePathEffect(10f, 5f)

七、综合应用示例

以下是一个综合使用多种效果的实例:

@Overridefun onDraw(canvas: Canvas) {    val mPaint = Paint()    // 设置颜色和样式    mPaint.color = Color.GREEN    mPaint.style = Paint.Style.STROKE    mPaint.strokeWidth = 8f    // 使用PathEffect    val mPath = Path()    mPath.moveTo(100f, 100f)    mPath.lineTo(300f, 300f)    mPath.lineTo(100f, 500f)    mPath.lineTo(300f, 700f)    // 创建一条偏移后的Path效果    val mPathSrc = Path()    mPathSrc.moveTo(100f + 100f, 100f)    mPathSrc.lineTo(300f + 100f, 300f)    mPathSrc.lineTo(100f + 100f, 500f)    mPathSrc.lineTo(300f + 100f, 700f)    // 调用PathEffect设置    mPaint.pathEffect = DashPathEffect(floatArrayOf(15f, 5f), 0f)    canvas.drawPath(mPath, mPaint)    // 重置PathEffect    mPaint.pathEffect = null    canvas.drawPath(mPathSrc, mPaint)    mPath.reset()    mPathSrc.reset()}

以上就是对Paint在Android开发中的各项功能的详细说明,通过合理设置Paint的属性和效果,可以实现丰富多样的图形绘制效果。

转载地址:http://heoaz.baihongyu.com/

你可能感兴趣的文章
Postgresql CopyManager 流式批量数据入库
查看>>
PostgreSQL cube 插件 - 多维空间对象
查看>>
PostgreSQL Daily Maintenance - cluster table
查看>>
PostgreSQL on Linux 最佳部署手册
查看>>
PostgreSQL Oracle 兼容性之 - pipelined
查看>>
PostgreSQL Point-In-Time Recovery (Incremental Backup)
查看>>
postgresql Streaming Replication监控与注意事项
查看>>
postgresql 不需要付费_使用数据传输在PostgreSQL执行 外部连接运算符
查看>>
postgresql 主从配置_生产环境postgresql主从环境配置
查看>>
postgresql 函数&存储过程 ; 递归查询
查看>>
PostgreSQL 分组聚合查询中 filter 子句替换 case when
查看>>
PostgreSQL 同步流复制锁瓶颈分析
查看>>
PostgreSQL 备份与还原命令 pg_dump
查看>>
Postgresql 外部表插件postgres_fdw的安装和使用
查看>>
PostgreSQL 如何从崩溃状态恢复(上)
查看>>
PostgreSQL 存储过程基本语法
查看>>
PostgreSQL 实现批量更新、删除、插入
查看>>
PostgreSQL 导入 .gz 备份文件
查看>>
PostgreSQL 批量插入&更新数据时报错(ERROR: ON CONFLICT DO UPDATE command cannot affect row a second time)
查看>>
PostgreSQL 新增数据返回自增ID
查看>>