绘图基础之Path类的应用
来源:互联网 发布:arduino摄像头编程 编辑:程序博客网 时间:2024/06/06 09:09
绘图基础之Path类的应用
Path类可以预先在View上将N个点连成一条”路径”,然后调用Canvas的drawPath(path,paint)即可沿着路径绘制图形
直接看代码
Activity代码
package com.shake.pathtest;import android.content.Context;import android.content.pm.ActivityInfo;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.ComposePathEffect;import android.graphics.CornerPathEffect;import android.graphics.DashPathEffect;import android.graphics.DiscretePathEffect;import android.graphics.Paint;import android.graphics.Path;import android.graphics.PathDashPathEffect;import android.graphics.PathEffect;import android.graphics.SumPathEffect;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); setContentView(new MyView(this)); } class MyView extends View { float phase; //PathEffect这个类本身并没有做什么特殊的处理,只是继承了Object,通常我们使用的是它的几个子类 PathEffect[] effects =new PathEffect[7]; int [] colors; private Paint paint; Path path; public MyView(Context context) { super(context); paint = new Paint(); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(4); //创建并且初始化Path path =new Path(); path.moveTo(0,0); for (int i=0;i<=40;i++){ //生成40个点,随机生成Y坐标,并连成一条path path.lineTo(i*20,(float) Math.random()*60); //初始化七个颜色 colors=new int[]{Color.BLACK,Color.BLUE,Color.GRAY,Color.GREEN,Color.MAGENTA,Color.RED,Color.YELLOW}; } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //将背景填充成白色 canvas.drawColor(Color.WHITE); // -----------下面开始初始化7种路径效果---------- // 不使用路径效果 effects[0]=null; //使用CornerPathEffect路径效果:这个类的作用就是将Path的各个连接线段之间的夹角用一种更平滑的方式连接,类似于圆弧与切线的效果。 //一般的,通过CornerPathEffect(float radius)指定一个具体的圆弧半径来实例化一个CornerPathEffect。 effects[1]=new CornerPathEffect(10); // 初始化DiscretePathEffect:这个类的作用是打散Path的线段,使得在原来路径的基础上发生打散效果。 //一般的,通过构造DiscretePathEffect(float segmentLength,float deviation)来构造一个实例,segmentLength指定最大的段长,deviation指定偏离量。 effects[2]=new DiscretePathEffect(3.0f,5.0f); // 初始化DashPathEffect:这个类的作用就是将Path的线段虚线化。 //构造函数为DashPathEffect(float[] intervals, float phase),其中intervals为虚线的ON和OFF数组 //该数组的length必须大于等于2,phase为绘制时的偏移量。 effects[3]=new DashPathEffect(new float[]{20,10,5,10},phase); // 初始化PathDashPathEffect:这个类的作用是使用Path图形来填充当前的路径 Path path =new Path(); path.addRect(0,0,8,8,Path.Direction.CCW); //其构造函数为PathDashPathEffect (Path shape, float advance, float phase,PathDashPathEffect.Stylestyle)。 //shape则是指填充图形,advance指每个图形间的间距,phase为绘制时的偏移量,style为该类自由的枚举值,有三种情况:Style.ROTATE、Style.MORPH和 //Style.TRANSLATE。其中ROTATE的情况下,线段连接处的图形转换以旋转到与下一段移动方向相一致的角度进行旋转,MORPH时图形 //会以发生拉伸或压缩等变形的情况与下一段相连接,TRANSLATE时,图形会以位置平移的方式与下一段相连接。 effects[4]=new PathDashPathEffect(path,12,phase,PathDashPathEffect.Style.ROTATE); //初始化ComposePathEffect //组合效果,这个类需要两个PathEffect参数来构造一个实例,ComposePathEffect (PathEffect outerpe,PathEffect innerpe),表现时, //会首先将innerpe表现出来,然后再在innerpe的基础上去增加outerpe的效果。 effects[5]=new ComposePathEffect(effects[2],effects[4]); //叠加效果,这个类也需要两个PathEffect作为参数SumPathEffect(PathEffect first,PathEffect second),但与ComposePathEffect不同 //的是,在表现时,会分别对两个参数的效果各自独立进行表现,然后将两个效果简单的重叠在一起显示出来。 effects[6]=new SumPathEffect(effects[4],effects[3]); // 将画布移动到(8、8)处开始绘制 canvas.translate(8,8); // 依次使用7种不同路径效果、7种不同的颜色来绘制路径 for (int i=0;i<effects.length;i++){ paint.setPathEffect(effects[i]); paint.setColor(colors[i]); canvas.drawPath(path, paint); //平移,将画布的坐标原点向左右方向移动x,向上下方向移动y canvas.translate(0,60); } // 改变phase值,形成动画效果 //phase参数的值不停发生改变,那么所绘制的图形也会随着偏移量而不断的发生变动 phase+=1; // 重绘, 再一次执行onDraw 程序 invalidate(); } }}
效果,显示不出来,可能是因为小米的系统
0 0
- 绘图基础之Path类的应用
- 自定义View-绘图基础之Path
- QCustomPlot之绘图的基础
- QCustomPlot之绘图的基础
- Android基础之Path类的使用
- Android基础之Path类的使用
- Android基础之Path类的使用
- Android绘图基础Path、PathMeasure
- Android绘图基础:Canvas、Paint、Path的简单使用
- 利用Path类绘图
- view绘图Path类
- android绘图之Path总结
- android绘图之Path总结
- Android学习笔记之基础绘图类
- MATLAB之绘图基础
- java 基础之Path,classPath的设置
- android 绘图之Path与Paint详解
- android 绘图之Path与Paint详解
- jQuery11(过滤器的3个练习)
- 面试笔试杂项积累-leetcode 136-140
- jQuery scrollFire插件
- BaseAdapter用法(一)
- <Java设计模式>—工厂方法模式
- 绘图基础之Path类的应用
- 常和异常处理(windows平台)
- jQuery12(prev练习,相对元素)
- 面试笔试杂项积累-leetcode 141-145
- python的正则表达式 re
- fibonacci数列 取模
- N-Queens II 经典问题:8皇后问题 题解
- 具备白名单、黑名单的Filter基类扩展
- [LeetCode]164. Maximum Gap