android水波纹教程,Android实现简单水波纹效果

本文为大家分享了Android实现水波纹效果展示的具体代码,供大家参考,具体内容如下

一、效果

6b9772d4907f428f64c1bc9a14689d99.gif

二、实现原理

自定义view,使用Path和贝塞尔曲线绘制,然后不断刷新,并且改变X、Y的值

主要知识点rQuadTo的使用

三、实现

WaveView.java

public class WaveView extends View {

private Paint mPaint;

private final Path mPath;

//波长

private int wavelength = 500;

private int originY=800;

private int dx,dy;

public WaveView(Context context, @Nullable AttributeSet attrs) {

super(context, attrs);

mPaint = new Paint();

mPath = new Path();

mPaint.setColor(Color.GREEN);

mPaint.setStrokeWidth(5);

mPaint.setStyle(Paint.Style.FILL_AND_STROKE);

// startanimation();

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

//重置path

mPath.reset();

// 改变y的起始坐标

if(dy

dy+=10;

}

int halfWaveLength = wavelength / 2;

mPath.moveTo(-wavelength + dx, originY-dy);

//屏幕多宽,画多少

for (int i = -wavelength; i <= getWidth() + wavelength; i += wavelength) {

/**

* 相对绘制二阶贝塞尔曲线(相对于自己的起始点--也即是上一个曲线的终点 )

* float dx1 相对于上一个曲线的终点 的距离

* float dy1

* float dx2

* float dy2

*/

mPath.rQuadTo(halfWaveLength / 2, -150, halfWaveLength, 0);

mPath.rQuadTo(halfWaveLength / 2, 150, halfWaveLength, 0);

}

//颜色填充

//画一个封闭的空间

mPath.lineTo(getWidth(), getHeight());

mPath.lineTo(0, getHeight());

mPath.close();

canvas.drawPath(mPath, mPaint);

// //设置起始点坐标

// path.moveTo(100,400);

// //二阶贝塞尔曲线1

// path.quadTo(250,200,400,400);

// //二阶贝塞尔曲线2

// path.quadTo(550,600,700,400);

// //关闭路径(将起点和终点闭合)

// path.close();

// path.moveTo(100,700);

// path.cubicTo(50,500,550,500,700,700);

}

public void startanimation() {

ValueAnimator animator = ValueAnimator.ofInt(0, wavelength);

animator.setDuration(1000);

animator.setInterpolator(new LinearInterpolator());

//无限循环

animator.setRepeatCount(ValueAnimator.INFINITE);

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

dx = (int) animation.getAnimatedValue();

postInvalidate();

}

});

animator.start();

}

}

最后把这个当成一个控件使用就可以。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持找一找教程网。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值