Android开发之PathMeasure的基本用法
来源:互联网 发布:网络计算机培训 编辑:程序博客网 时间:2024/05/01 08:29
前言:PathMeasure是Path的测量类,ok!我们来看下有那些用法。
-------------分割线----------
关于PathMeasure(path,false);中的第二个参数,true都会自动测量path包括闭合部分的长度 ,false则反之!
我们来看下具体案例。
代码:
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;import static android.R.attr.x;import static android.R.attr.y;import static android.content.ContentValues.TAG;/** * Created by Fly on 2017/6/14. */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() + ""); //forceClosed:不管path绘制的是否关闭,forceClosed=true都会自动测量path包括闭合部分的长度 canvas.drawPath(path, paint); }}运行效果图:
打印信息:
-------------分割线-------------
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();//获取下一个路径,有可能没有多个路径了,返回false float length2 = measure.getLength(); Log.i("---nextContour---", nextContour + ""); Log.i("---length1---", length1 + ""); Log.i("---length2---", length2 + ""); canvas.drawPath(path, paint);运行效果图:
日志打印:
----------------分割线---------------
截取片段:
Path path = new Path(); path.addRect(-200, -200, 200, 200, Path.Direction.CW); PathMeasure measure = new PathMeasure(path, false); float length = measure.getLength(); Log.i("---length---", length + ""); canvas.drawPath(path, paint); Path path1 = new Path();// path1.lineTo(-300, -300);// path1.lineTo(-200, 0);// path1.lineTo(-200, 0);// path1.lineTo(0, 0); //startWithMoveTo:false,代表该起始点是否位上一个的结束点(是否保持连续性)。// measure.getSegment(200, 600, path1, false); measure.getSegment(200, 600, path1, true); paint.setColor(Color.GREEN); canvas.drawPath(path1, paint);效果图:
----------分割线------------
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];//tan=y/x measure.getPosTan(measure.getLength() / 4, pos, tan); Log.i("---------->", "position:x-" + pos[0] + ", y-" + pos[1]); Log.i("---------->", "tan:x-" + tan[0] + ", y-" + tan[1]); canvas.drawPath(path, paint);看下效果:
打印日志:
------------完----------
阅读全文
0 0
- Android开发之PathMeasure的基本用法
- android中PathMeasure的简单用法
- PathMeasure的基本使用
- Android Path 之 PathMeasure
- android开发之pinyin4j的基本用法
- PathMeasure用法
- Android PathMeasure的使用详解
- Android 中PathMeasure的使用
- 安卓PathMeasure的简单用法
- android开发之ListView基本用法
- Path之PathMeasure的介绍(一)
- iOS开发之NSPredicate的基本用法
- android开发-广播的基本用法
- Android开发--SurfaceView的基本用法
- Android开发初识Volley的基本用法
- Android开发笔记:Button的基本用法
- Android Path和PathMeasure类的使用之获取圆弧上的坐标值
- Android之SharedPreferences的基本用法
- ZOJ--1078:Palindrom Numbers
- Mysql主从复制原理解析和技术难点解决
- nginx 学习笔记(三)基本数据结构
- 考研英语
- [置顶] Android APK反编译就这么简单 详解(附图)
- Android开发之PathMeasure的基本用法
- 0020_Valid Parentheses
- Thymeleaf系列五 迭代,if,switch语法
- Android7.0权限适配
- python 格式化输出
- 使用脚本进行同行股票基本面对比效果
- python 相对导入
- dubbo 部分 配置的关系-dubbo github 官方案例
- Linux相关挂载操作