Path类详解

前言:自定义控件学习中。。。。。。。。。

Android 提供的Path是一个非常有用的类,它可以预先在View上将N个点连成一条路径,然后调用Canvas的drawPath(path,paint)即可沿着路径绘制图形。实际上Android还为路径绘制提供了PathEffect来定义绘制效果,PathEffect包含了如下子类(每一种子类代表一种绘制效果):

1)ComposePathEddect

2) CornerPathEffect

3) DashPathEffect

4) DiscretePathEffect

5) PathDashPathEffect

6) SumPathEffect

绘制效果语言描述效果很差,先上图,然后源码走起



package com.example.androidtext1;

import android.R.color;
import android.content.Context;
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.util.AttributeSet;
import android.view.View;

public class PathText extends View{

	float phase;
	PathEffect[] effects = new PathEffect[7];
	int[] colors;
	private Paint paint;
	Path path;
	public PathText(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 =1;i<=15;i++){
			//生成15个点,随机生成他们的y坐标,并将他们连城一套path
			path.lineTo(i*20, (float)Math.random()*60);
		}
		//初始化7个颜色
		colors = new int[]{Color.BLACK,Color.BLUE,Color.GREEN,Color.DKGRAY,Color.RED,Color.CYAN,Color.MAGENTA};
		
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		//将背景填充成白色
		canvas.drawColor(Color.WHITE);
		//------下面开始初始化7种路劲效果-----------
		//不使用路劲效果
		effects[0] = null;
		//使用CornerPathEffect路劲效果
		effects[1] = new CornerPathEffect(10);
		//初始化DiscretePathEffect
		effects[2] = new DiscretePathEffect(3.0f, 5.0f);
		//初始化DashPathEffect
		effects[3] = new DashPathEffect(new float[]{20, 10,5,10},phase);
		//初始化PathDashPathEffect
		Path p = new Path();
		p.addRect(0, 0, 8, 8, Path.Direction.CCW);
		effects[4] = new PathDashPathEffect(p, 12, phase, PathDashPathEffect.Style.ROTATE);
		//初始化PthDashPathEffect
		effects[5] = new ComposePathEffect(effects[2],effects[4]);
		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);
			canvas.translate(0, 60);
		}
		
		//改变phase,形成动画效果
		phase +=1;
		invalidate();
		
		
		
		
	}

}

下面的代码是绘制文本不是简单的水平排列,而是沿着指定的路径绘制的


package com.example.androidtext1;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;

public class PathTextSecond extends View {

	final String DRAW_STR = "你的剑就是我的剑";
	Path[] paths = new Path[3];
	Paint paint;

	public PathTextSecond(Context context) {
		super(context);
		paths[0] = new Path();
		paths[0].moveTo(0, 0);
		for (int i = 1; i <= 7; i++) {
			// 生成7个点,随机生成他们的y坐标,并将他们连成一条Path
			paths[0].lineTo(i * 30, (float) Math.random() * 30);
		}
		paths[1] = new Path();
		RectF rectF = new RectF(0, 0, 200, 120);
		paths[1].addOval(rectF, Path.Direction.CCW);
		paths[2] = new Path();
		paths[2].addArc(rectF, 60, 180);
		// 初始化画笔
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.CYAN);
		paint.setStrokeWidth(1);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
		canvas.drawColor(Color.WHITE);
		canvas.translate(40, 40);
		// 设置从右边开始绘制(右对齐)
		paint.setTextAlign(Paint.Align.RIGHT);
		paint.setTextSize(20);
		// 沿着路径绘制一段文本
		paint.setStyle(Paint.Style.FILL);
		canvas.drawTextOnPath(DRAW_STR, paths[0], -8, 20, paint);
		// 对 canvas进行坐标变换,画布下移120
		canvas.translate(0, 60);
		// 绘制路径
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawPath(paths[1], paint);
		// 沿着路劲绘制一段文本
		paint.setStyle(Paint.Style.FILL);
		canvas.drawTextOnPath(DRAW_STR, paths[1], -20, 20, paint);
		// 对 canvas进行坐标变换,画布下移120
		canvas.translate(0, 120);
		// 绘制路径
		paint.setStyle(Paint.Style.STROKE);
		canvas.drawPath(paths[2], paint);
		// 沿着路劲绘制一段文本
		paint.setStyle(Paint.Style.FILL);
		canvas.drawTextOnPath(DRAW_STR, paths[2], -10, 20, paint);
	}

}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值