public class WaveView extends View {
private int screenWidth;
private float xoffset = 0;
private Path path;
private Paint paint;
public WaveView(Context context) {
this(context,null);
}
public WaveView(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public WaveView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
screenWidth = wm.getDefaultDisplay().getWidth();
paint = new Paint();
paint.setColor(Color.RED);
paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setStyle(Paint.Style.STROKE);
path = new Path();
ValueAnimator animator = ValueAnimator.ofFloat(0f,screenWidth);
animator.setDuration(1000);
animator.setRepeatCount(-1);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
xoffset = (float) animation.getAnimatedValue();
invalidate();
}
});
animator.start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
path.reset();
path.moveTo(0+xoffset, 500);
path.quadTo(screenWidth / 4+xoffset, 300, screenWidth / 2+xoffset, 500);
path.moveTo(screenWidth / 2+xoffset, 500);
path.quadTo(screenWidth / 4 * 3+xoffset, 700, screenWidth+xoffset, 500);
path.moveTo(0 + xoffset - screenWidth, 500);
path.quadTo(screenWidth / 4 + xoffset - screenWidth, 300, screenWidth / 2 + xoffset - screenWidth, 500);
path.moveTo(screenWidth / 2 + xoffset - screenWidth, 500);
path.quadTo(screenWidth / 4 * 3 + xoffset - screenWidth, 700, screenWidth + xoffset - screenWidth, 500);
canvas.drawPath(path, paint);
}
}
<com.bw.fengye.bolangxian.WaveView
android:layout_width="match_parent"
android:layout_height="match_parent" />```