Paint中ComposePathEffect的简单使用

在前面,我们认识了PathEffect的两个子类,现在我们来认识一下第三个子类ComposePathEffect,它就很有意思了,它是把两个子类的效果连在一起。记住是两个,不能多了。它是方法是

public ComposePathEffect(PathEffect outerpe, PathEffect innerpe) {
        native_instance = nativeCreate(outerpe.native_instance,
                                       innerpe.native_instance);
    }

它是先表现出innerpe的效果,再表现中outerpe的效果,下面把前两章的两个子类结合一下,看一下代码

public class PathComposePathEffectView extends View {

    private Paint mPaint;

    private Path mPath;
    private PathEffect comPathEffect;
    private PathEffect cornerPathEffect;
    private PathEffect dashPathEffect;

    private PathMeasure mPathMeasure;
    private float mLength;
    private float animValue;

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

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

    public PathComposePathEffectView(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,100);
        mPath.lineTo(100,150);
        mPath.lineTo(200,50);
        mPath.lineTo(300,200);
        mPath.lineTo(400,150);

        mPathMeasure = new PathMeasure(mPath,false);
        mLength = mPathMeasure.getLength();

        //设置拆线处为圆角
        cornerPathEffect = new CornerPathEffect(30);

        //还记得这个动画吧,让线从无到有的动画
        ValueAnimator animator = ValueAnimator.ofFloat(1,0);
        animator.setDuration(2000);
        animator.setRepeatCount(0);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                animValue = (float)animation.getAnimatedValue();

                dashPathEffect = new DashPathEffect(new float[]{mLength,mLength},mLength * animValue);
                
                //将两个Effect组合起来
                comPathEffect = new ComposePathEffect(cornerPathEffect,dashPathEffect);
                mPaint.setPathEffect(comPathEffect);
                invalidate();
            }
        });
        animator.start();

    }

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

        canvas.drawPath(mPath,mPaint);
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值