使用贝赛尔曲线绘制波浪线

来源:互联网 发布:美的集团金融中心知乎 编辑:程序博客网 时间:2024/05/17 22:48
package com.lenovohit.administrator.tyut.views;import android.animation.ValueAnimator;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Path;import android.util.AttributeSet;import android.view.View;import android.view.animation.LinearInterpolator;import com.lenovohit.administrator.tyut.R;/** * Created by Administrator on 2017/3/6. */public class WaveView extends View {    private Paint mPaint;    private Path mPath;    private int mItemWaveLength = 1000;    private int dx;    public WaveView(Context context, AttributeSet attrs) {        super(context, attrs);        mPath = new Path();        mPaint = new Paint();        mPaint.setColor(getResources().getColor(R.color.dahai));        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        mPath.reset();        int originY = 300;        int halfWaveLen = mItemWaveLength/2;        mPath.moveTo(-mItemWaveLength+dx,originY);        for (int i = -mItemWaveLength;i<=getWidth()+mItemWaveLength;i+=mItemWaveLength){            mPath.rQuadTo(halfWaveLen/2,-100,halfWaveLen,0);            mPath.rQuadTo(halfWaveLen/2,100,halfWaveLen,0);        }        mPath.lineTo(getWidth(),getHeight());        mPath.lineTo(0,getHeight());        mPath.close();        canvas.drawPath(mPath,mPaint);    }    public void startAnim(){        ValueAnimator animator = ValueAnimator.ofInt(0,mItemWaveLength);        animator.setDuration(2000);        animator.setRepeatCount(ValueAnimator.INFINITE);        animator.setInterpolator(new LinearInterpolator());        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                dx = (int)animation.getAnimatedValue();                postInvalidate();            }        });        animator.start();    }}
0 0
原创粉丝点击