drawLine DashPathEffect绘制虚线变成了实线

mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);  
mPaint.setStyle(Style.STROKE);  
mPaint.setColor(Color.parseColor("#cccccc"));  
mPaint.setStrokeWidth(Util.dp2px(context, 5));  
PathEffect effects = new DashPathEffect(new float[] { 5,2}, 1);  

mPaint.setPathEffect(effects);  

canvas.drawLine(0, 0, 0, mHeight, mPaint);

不显示 虚线


要用drawPath


package com.zixing.phil.patheffect;


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.PathDashPathEffect.Style;
import android.graphics.PathEffect;
import android.graphics.SumPathEffect;
import android.util.AttributeSet;
import android.view.View;


public class PathEffectView extends View{


private Paint mPaint;
private Paint starPaint;
private Path mPath;
private Path star;


private float phase = 0;

public PathEffectView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}


public PathEffectView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}

public PathEffectView(Context context) {
super(context);
init();
}


@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
for(int i=0;i<9;i++){
mPath.moveTo(0, 0+(i*50));
mPath.lineTo(50, 30+(i*50));
mPath.lineTo(100,0+(i*50));
mPath.lineTo(150, 30+(i*50));
mPath.lineTo(200, 0+(i*50));
PathEffect pe = null;
switch (i) {
case 0:
pe = new CornerPathEffect(10);
break;
case 1:
pe = new DashPathEffect(new float[]{10f,3f}, phase);
break;
case 2:
pe = new DiscretePathEffect(5f, 10f);
break;
case 3:
pe = new PathDashPathEffect(star, 20, phase, Style.ROTATE);
break;
case 4:
pe = new PathDashPathEffect(star, 20, phase, Style.MORPH);
break;
case 5:
pe = new PathDashPathEffect(star, 20, phase, Style.TRANSLATE);
break;
case 6:
PathEffect cpeOuter = new PathDashPathEffect(star, 20, phase, Style.MORPH);
PathEffect cpeInner = new DiscretePathEffect(5f, 10f);
pe = new ComposePathEffect(cpeOuter, cpeInner);
break;
case 7:
PathEffect cpe1 = new PathDashPathEffect(star, 20, phase, Style.MORPH);
PathEffect cpe2 = new DiscretePathEffect(5f, 10f);
pe = new SumPathEffect(cpe1, cpe2);
break;
default:
break;
}
mPaint.setPathEffect(pe);
canvas.drawPath(mPath, mPaint);
mPath.reset();
}
phase+=2;
invalidate();
}

private void init(){
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(2);
mPaint.setColor(Color.RED);
mPath = new Path();
starPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
starPaint.setStyle(Paint.Style.FILL_AND_STROKE);
starPaint.setColor(Color.YELLOW);
star(10);
}

private void star(float length){
star = new Path();
float dis1 = (float)((length/2)/Math.tan((54f/180)*Math.PI));
float dis2 = (float)(length*Math.sin((72f/180)*Math.PI));
float dis3 = (float)(length*Math.cos((72f/180)*Math.PI));
star.moveTo(length/2, 0);
star.lineTo(length/2-dis3, dis2);
star.lineTo(length, dis1);
star.lineTo(0, dis1);
star.lineTo(length/2+dis3, dis2);
star.lineTo(length/2, 0);
}

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值