Android实现简单水波纹效果
作者:wanxiaofan 时间:2021-11-09 12:40:45
本文为大家分享了Android实现水波纹效果展示的具体代码,供大家参考,具体内容如下
一、效果
二、实现原理
自定义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<originY+150){
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();
}
}
最后把这个当成一个控件使用就可以。
来源:https://blog.csdn.net/wanxiaofan/article/details/77451384
标签:Android,水波纹
0
投稿
猜你喜欢
Spring的@Validation和javax包下的@Valid区别以及自定义校验注解
2021-06-20 04:06:35
Android框架组件Lifecycle的使用详解
2022-08-01 08:48:59
java应用占用内存过高排查的解决方案
2023-09-21 12:47:30
Android Jetpack组件中LifeCycle作用详细介绍
2022-05-14 04:56:15
Java单例模式实现的几种方式
2021-06-09 18:38:47
Spring Security之默认的过滤器链及自定义Filter操作
2023-11-24 02:48:35
IDEA集成MyBatis Generator插件的使用
2023-08-12 00:28:47
Android使用PowerImageView实现播放强大的ImageView动画效果
2022-06-16 19:55:54
Java内存模型之happens-before概念详解
2023-11-23 03:11:50
Android RecyclerView基本使用详解
2023-07-24 21:13:30
对WPF中的TreeView实现右键选定
2022-01-21 00:25:55
Java 判断数组是否相等的方法示例
2023-09-03 10:21:13
Springboot整合微信支付(订单过期取消及商户主动查单)
2023-05-15 23:40:50
MyBatis根据条件批量修改字段的方式
2023-12-16 03:24:01
idea创建javaweb原生项目的实现示例
2023-06-16 05:40:35
Spring自动配置之condition条件判断上篇
2022-07-01 17:34:51
详解Spring框架注解扫描开启之配置细节
2022-11-01 18:04:14
Android Jetpack架构组件Lifecycle详解
2023-03-04 22:21:01
Android Studio ADB网络调试汇总
2023-07-13 22:10:07
java日期操作工具类(获取指定日期、日期转换、相隔天数)
2023-11-28 06:42:53