Paint中DiscretePathEffect的简单使用

DiscretePathEffect这个子类,说实话,我是真心没整明白,在什么地方能用到,它给人的感觉是一种毛刺的效果,下方面是它的方法

    public DiscretePathEffect(float segmentLength, float deviation) {
        native_instance = nativeCreate(segmentLength, deviation);
    }

官方的解释:
DiscretePathEffect切断线段
segmentLength是指定切断的长度
deviation为切断之后线段的偏移量,随机的,小于等于deviation。

代码如下:

public class PathDiscreteView extends View {

    private Paint mPaint;
    private Path mPath;

    private PathEffect mPathEffect;

    public PathDiscreteView(Context context) {
        this(context,null);
    }

    public PathDiscreteView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public PathDiscreteView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeWidth(5);
        mPaint.setColor(Color.RED);

        mPath = new Path();
        mPath.moveTo(0,450);
        mPath.lineTo(200,450);
        mPath.lineTo(400,250);
        mPath.lineTo(600,550);

        mPathEffect = new DiscretePathEffect(3,30);
        mPaint.setPathEffect(mPathEffect);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawPath(mPath,mPaint);
    }
}

效果图:
效果图

发布了305 篇原创文章 · 获赞 261 · 访问量 184万+

猜你喜欢

转载自blog.csdn.net/chenguang79/article/details/102560190