## 技术共享之贝塞尔曲线

``````package besia.test.liang.com.besiacom.liang.test.besia;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
* Created by 梁 on 2017/12/14.
*/

public class Wave extends View {

private Paint paint;
private Path path;
private int waveLength = 300;  //波长的宽度
private int dx;
private int dy;

public Wave(Context context, AttributeSet attrs) {
super(context, attrs);
paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL_AND_STROKE);

path = new Path();

}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.reset();  //重置 path
int originY = getHeight();
if(dy<originY + 150){
dy += 10;  //高度每秒 + 10
}
int halfWaveLength = waveLength/2;   //半个波长
path.moveTo(-waveLength+dx, originY-dy);
//屏幕的宽度里面放多少个波长
for (int i = -waveLength; i < getWidth() + waveLength; i += waveLength) {
//相对绘制二阶贝塞尔曲线(相对于自己的起始点--也即是上一个曲线的终点  的距离dx1)

}
//颜色填充
//画一个封闭的空间
path.lineTo(getWidth(), getHeight());
path.lineTo(0, getHeight());
path.close();

canvas.drawPath(path, paint);  //开始绘制
}

public void startAnimation(){
ValueAnimator animator = ValueAnimator.ofInt(0,waveLength);
animator.setDuration(1000);  //设置时长
animator.setInterpolator(new LinearInterpolator());  //设置线性加速器
//无限循环
animator.setRepeatCount(ValueAnimator.INFINITE);

@Override
public void onAnimationUpdate(ValueAnimator animation) {
dx = (int) animation.getAnimatedValue();
postInvalidate();  //重绘
}
});
animator.start();
}
}
``````

``````<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="besia.test.liang.com.besiacom.liang.test.besia.MainActivity">

<besia.test.liang.com.besiacom.liang.test.besia.Wave
android:id="@+id/wave"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</android.support.constraint.ConstraintLayout>``````

``````package besia.test.liang.com.besiacom.liang.test.besia;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

/**
* Created by 梁 on 2017/12/14.
*/

public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Wave wave = (Wave)findViewById(R.id.wave);
wave.startAnimation();
}
}
``````