PathEffect是绘制path路径增加特殊效果的类,它的子类有ComposePathEffect, CornerPathEffect, DashPathEffect, DiscretePathEffect, PathDashPathEffect, SumPathEffect;
CornerPathEffect(float radius):
它将Path的各个连接线段之间的夹角用一种更平滑的方式连接,类似于圆弧与切线的效果。
一般的,通过CornerPathEffect(float radius)指定一个具体的圆弧半径来实例化一个CornerPathEffect。
DashPathEffect(float[] intervals, float phase):
它可以将path变成虚线链接,intervals为虚线的ON和OFF数组,该数组的length必须大于等于2,phase为绘制时的偏移量。
DiscretePathEffect(float segmentLength, float deviation):
它可以给path路径上添加散开的杂点,segmentLength指定最大的段长,deviation指定偏离量.
PathDashPathEffect(Path shape, float advance, float phase, PathDashPathEffect.Style style):
它的效果类似于DashPathEffect,shape则是指填充图形,advance指每个图形间的间距,phase为绘制时的偏移量,style为该类自由的枚举值,有三种情况:Style.ROTATE、Style.MORPH和
Style.TRANSLATE。其中ROTATE的情况下,线段连接处的图形转换以旋转到与下一段移动方向相一致的角度进行转转,MORPH时图形会以发生拉伸或压缩等变形的情况与下一段相连接,TRANSLATE时,图形会以位置平移的方式与下一段相连接。
ComposePathEffect(PathEffect outerpe, PathEffect innerpe):
这个类的作用就是可将上面挑选出2种结合在一起,会首先将innerpe表现出来,然后再在innerpe的基础上去增加outerpe的效果。
Demo代码
package com.example.xu.patheffectdemo;
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.util.AttributeSet;
import android.view.View;
/**
* Created by 16413 on 2017/3/16.
*/
public class PathEffectView extends View {
private Paint mPaint;
private Path mPath;
private PathEffect[] mPathEffect;
public PathEffectView(Context context) {
this(context,null);
}
public PathEffectView(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public PathEffectView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint();
}
private void initPaint() {
mPathEffect = new PathEffect[5];
mPaint = new Paint();
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.STROKE);
mPath = new Path();
mPath.moveTo(0,0);
for (int i = 0; i < 30; i++) {
mPath.lineTo(i*35,(float)(Math.random()*100));
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 圆滑线 参数:角度
*/
mPathEffect[0] = new CornerPathEffect(30);
/**
* 杂点线 segmentLength指定最大的段长,deviation指定偏离量。
*/
mPathEffect[1] = new DiscretePathEffect(3f,5f);
/**
* 虚线 intervals为虚线的ON和OFF数组,该数组的length必须大于等于2,phase为绘制时的偏移量。
*/
mPathEffect[2] = new DashPathEffect(new float[]{20,5},0);
/**
* 与DashPathEffect 但可以设置点的形状 shape则是指填充图形,advance指每个图形间的间距,phase为绘制时的偏移量,style为该类自由的枚举值,
* 有三种情况:Style.ROTATE、Style.MORPH和Style.TRANSLATE。其中ROTATE的情况下,
* 线段连接处的图形转换以旋转到与下一段移动方向相一致的角度进行转转,MORPH时图形会以发生拉伸或压缩等变形的情况与下一段相连接,
* TRANSLATE时,图形会以位置平移的方式与下一段相连接。
*/
Path path = new Path();
path.addCircle(0,0,10, Path.Direction.CCW);
mPathEffect[3] = new PathDashPathEffect(path,20,0, PathDashPathEffect.Style.TRANSLATE);
/**
* 可以将两种线结合在一起
*/
mPathEffect[4] = new ComposePathEffect(mPathEffect[2],mPathEffect[1]);
for (int i = 0; i < mPathEffect.length; i++) {
mPaint.setPathEffect(mPathEffect[i]);
canvas.drawPath(mPath,mPaint);
canvas.translate(0,200);
}
}
}
demo效果图: