博客
关于我
Android开发之PathMeasure的基本用法
阅读量:788 次
发布时间:2019-03-24

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

PathMeasure测量类

PathMeasure是Android图形库中用于测量路径长度的强大工具,支持闭合和非闭合路径的测量。它能够帮助开发者快速获取路径数据,节省手动计算的时间。这一功能在绘图、游戏开发等场景中都有广泛应用。

forceClosed参数的作用

PathMeasure(path, false);默认情况下,measure.isClosed()返回的值与path的闭合状态有关。如果想要强制闭合路径,无论Path本身是否闭合,可以通过forceClosed属性来处理。例如,forceClosed=true时,measure.getLength()将包括路径的闭合部分的测量结果。这一功能特别有用,当路径并非闭合时,仍能完整测量所有边线。

我们来看看实际案例

代码示例:

import android.content.Context;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.util.Log;
import android.view.View;

public class CustomView extends View {

private Paint paint;
private int mViewHeight;
private int mViewWidth;

public CustomView(Context context) {      super(context);      paint = new Paint();      paint.setColor(Color.RED);      paint.setStyle(Paint.Style.STROKE);      paint.setStrokeWidth(10);  }  @Override  protected void onSizeChanged(int w, int h, int oldw, int oldh) {      super.onSizeChanged(w, h, oldw, oldh);      mViewHeight = h;      mViewWidth = w;  }  @Override  protected void onDraw(Canvas canvas) {      super.onDraw(canvas);      canvas.translate(mViewWidth / 2, mViewHeight / 2);      Path path = new Path();      path.lineTo(0, 200);      path.lineTo(200, 200);      path.lineTo(200, 0);      PathMeasure measure1 = new PathMeasure(path, false);      PathMeasure measure2 = new PathMeasure(path, true);      Log.i("---length1---", measure1.getLength() + "");      Log.i("---length2---", measure2.getLength() + "");      canvas.drawPath(path, paint);  }

}

运行效果图:

接下来看看PathMeasure的其他方法

nextContour属性

代码示例:

Path path = new Path();

path.addRect(-200, -200, 200, 200, Path.Direction.CW);
path.addRect(-100, -100, 100, 100, Path.Direction.CW);

PathMeasure measure = new PathMeasure(path, false);

float length1 = measure.getLength();
boolean nextContour = measure.nextContour();
float length2 = measure.getLength();

运行效果图:

截取路径片段

代码示例:

Path path = new Path();

path.addRect(-200, -200, 200, 200, Path.Direction.CW);

PathMeasure measure = new PathMeasure(path, false);

float length = measure.getLength();

Path path1 = new Path();

measure.getSegment(200, 600, path1, false);
measure.getSegment(200, 600, path1, true);

运行效果图:

getPosTan方法

代码示例:

Path path = new Path();

path.addCircle(0, 0, 300, Path.Direction.CW);

PathMeasure measure = new PathMeasure(path, false);

float[] pos = new float[2];
float[] tan = new float[2];

measure.getPosTan(measure.getLength() / 4, pos, tan);

运行效果图:

通过PathMeasure可以轻松获取路径的直角斜率和位置信息,这在绘图、物理仿真等场景下都有实用价值。

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

你可能感兴趣的文章
MySQL8找不到my.ini配置文件以及报sql_mode=only_full_group_by解决方案
查看>>
mysql8的安装与卸载
查看>>
MySQL8,体验不一样的安装方式!
查看>>
MySQL: Host '127.0.0.1' is not allowed to connect to this MySQL server
查看>>
Mysql: 对换(替换)两条记录的同一个字段值
查看>>
mysql:Can‘t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock‘解决方法
查看>>
MYSQL:基础——3N范式的表结构设计
查看>>
MYSQL:基础——触发器
查看>>
Mysql:连接报错“closing inbound before receiving peer‘s close_notify”
查看>>
mysqlbinlog报错unknown variable ‘default-character-set=utf8mb4‘
查看>>
mysqldump 参数--lock-tables浅析
查看>>
mysqldump 导出中文乱码
查看>>
mysqldump 导出数据库中每张表的前n条
查看>>
mysqldump: Got error: 1044: Access denied for user ‘xx’@’xx’ to database ‘xx’ when using LOCK TABLES
查看>>
Mysqldump参数大全(参数来源于mysql5.5.19源码)
查看>>
mysqldump备份时忽略某些表
查看>>
mysqldump实现数据备份及灾难恢复
查看>>
mysqldump数据库备份无法进行操作只能查询 --single-transaction
查看>>
mysqldump的一些用法
查看>>
mysqli
查看>>