用path画一个抽象的树叶

源码地址:https://github.com/X-FAN/LeafView 只是个简单的demo,大家可以参考下

public class PathTestView extends View {
    private int mWidth;
    private int mHeight;
    private int mDuration = 5000;
    private int mState = 0;//当前状态;
    private float mFraction;


    private Paint mPaint;
    private PathMeasure mPathMeasure;
    private PathMeasure mPathMeasureLeft;
    private PathMeasure mPathMeasureRight;
    private ValueAnimator mAnimator;


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

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

    public PathTestView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initPaint();
        initPathAndMeasure();
        initAnimator();
    }

    private void initPaint() {
        mPaint = new Paint();
        mPaint.setStrokeWidth(10);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(Color.WHITE);
    }

    private void initPathAndMeasure() {
        Path mPathLine = new Path();
        Path mPathLeft = new Path();
        Path mPathRight = new Path();
        mPathLine.lineTo(0, 400);
        mPathMeasure = new PathMeasure(mPathLine, false);
        mPathLeft.quadTo(-200, 200, 0, 400);//画贝赛尔曲线
        mPathMeasureLeft = new PathMeasure(mPathLeft, false);
        mPathRight.quadTo(200, 200, 0, 400);
        mPathMeasureRight = new PathMeasure(mPathRight, false);
    }

    private void initAnimator() {
        mAnimator = ValueAnimator.ofFloat(0f, 1.0f);
        mAnimator.setDuration(mDuration);
        mAnimator.setInterpolator(new DecelerateInterpolator());
        mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                mFraction = (float) animation.getAnimatedValue();
                invalidate();
            }
        });
        mAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (mState < 3) {//更改状态
                    mState++;
                } else {
                    mState = 0;
                }
                mAnimator = mAnimator.clone();//本来想直接复用mAnimator,但是执行到onAnimationEnd,mAnimator貌似没有立即结束,直接start会有问题,
                mAnimator.start();           //问题待研究,若有知道具体原因的望告知

            }
        });
        mAnimator.start();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth = w;
        mHeight = h;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.translate(mWidth / 2, mHeight / 2);// 将画布坐标原点移动到中心位置
        canvas.scale(1, -1);//翻转y轴
        canvas.save();
        drawLine(canvas);
        drawLeaf(canvas);
        canvas.restore();
    }

    /**
     * 画叶子的边界
     *
     * @param canvas
     */
    private void drawLeaf(Canvas canvas) {
        Path dst = new Path();
        Path dstRight = new Path();
        switch (mState) {
            case 0:
                mPathMeasureLeft.getSegment(0, mPathMeasureLeft.getLength() * mFraction, dst, true);//取出要绘制部分的path
                mPathMeasureRight.getSegment(0, mPathMeasureRight.getLength() * mFraction, dstRight, true);
                break;
            case 1:
                mPathMeasureLeft.getSegment(mPathMeasureLeft.getLength() * mFraction, mPathMeasureLeft.getLength(), dst, true);//取出要绘制部分的path
                mPathMeasureRight.getSegment(mPathMeasureRight.getLength() * mFraction, mPathMeasureRight.getLength(), dstRight, true);
                break;
            case 2:
                mPathMeasureLeft.getSegment(mPathMeasureLeft.getLength() * (1 - mFraction), mPathMeasureLeft.getLength(), dst, true);//取出要绘制部分的path
                mPathMeasureRight.getSegment(mPathMeasureRight.getLength() * (1 - mFraction), mPathMeasureRight.getLength(), dstRight, true);
                break;
            case 3:
                mPathMeasureLeft.getSegment(0, mPathMeasureLeft.getLength() * (1 - mFraction), dst, true);//取出要绘制部分的path
                mPathMeasureRight.getSegment(0, mPathMeasureRight.getLength() * (1 - mFraction), dstRight, true);
                break;
            default:
                break;
        }
        canvas.drawPath(dst, mPaint);
        canvas.drawPath(dstRight, mPaint);
    }

    /**
     * 画主干
     */
    private void drawLine(Canvas canvas) {
        Path dst = new Path();
        switch (mState) {
            case 0:
                mPathMeasure.getSegment(0, mPathMeasure.getLength() * mFraction, dst, true);
                break;
            case 1:
                mPathMeasure.getSegment(mPathMeasure.getLength() * mFraction, mPathMeasure.getLength(), dst, true);
                break;
            case 2:
                mPathMeasure.getSegment(mPathMeasure.getLength() * (1 - mFraction), mPathMeasure.getLength(), dst, true);
                break;
            case 3:
                mPathMeasure.getSegment(0, mPathMeasure.getLength() * (1 - mFraction), dst, true);
                break;
            default:
                break;
        }
        canvas.drawPath(dst, mPaint);
    }


}

效果图

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏叁金大数据

WPF Canvas 画区域

有时候需要实现类似于QQ截图那样的选择区域功能,这里的区域可以是一条线,圆,矩形等等

28830
来自专栏跟着阿笨一起玩NET

asp.net中的比较完美的验证码

本文转载:http://blog.csdn.net/zjk20108023/article/details/7836174

1.1K10
来自专栏Spring相关

使用canvas绘制渐变色矩形和使用按键控制人物移动

13830
来自专栏潇涧技术专栏

Android Heroes Reading Notes 3

《Android群英传》读书笔记 (3) 第六章 Android绘图机制与处理技巧 + 第七章 Android动画机制与使用技巧

8520
来自专栏CodingBlock

Android查缺补漏(View篇)--自定义View利器Canvas和Paint详解

上篇文章介绍了自定义View的创建流程,从宏观上给出了一个自定义View的创建步骤,本篇是上一篇文章的延续,介绍了自定义View中两个必不可少的工具Canvas...

447120
来自专栏進无尽的文章

绘图-圆环进度条实现详解

27920
来自专栏Android知识点总结

Android关于Color你所知道的和不知道的一切

18240
来自专栏向治洪

React Native仿美团下拉菜单

在很多产品中都会涉及到下拉菜单选择功能,用的最好的当属美团了,其效果如下: ? 要实现上面的效果,在原生中比较好做,直接使用PopWindow组件即可。如果使...

92450
来自专栏Android开发与分享

【Android】RecyclerView:打造悬浮效果

1K100
来自专栏互联网杂技

30+有用的CSS代码片段

在一篇文章中收集所有的CSS代码片段几乎是不可能的事情,但是我们这里列出了一些相对于其他的更有用的代码片段,不要被这些代码的长度所吓到,因为它们都很容易实现,并...

41260

扫码关注云+社区

领取腾讯云代金券