先上效果图:
自定义波浪View:
通过正余弦自定义View 实现水波浪效果:
double ω = 2 * Math.PI / getWidth();
mAbovePath.moveTo(getLeft(), getBottom());
mBelowWavePath.moveTo(getLeft(), getBottom());
for (float x = 0; x <= getWidth(); x += 20) {
y = (float) (rangeY * Math.cos(ω * x + φ) + 8);
y2 = (float) (rangeY * Math.sin(ω * x + φ));
mAbovePath.lineTo(x, y);
mBelowWavePath.lineTo(x, y2);
//回调 把y坐标的值传出去(在activity里面接收让图片随波浪一起摇摆)
if (mWaveAnimationListener != null)
mWaveAnimationListener.OnWaveAnimation(y);
}
mAbovePath.lineTo(getRight(), getBottom());
mBelowWavePath.lineTo(getRight(), getBottom());
canvas.drawPath(mAbovePath, mAboveWavePaint);
canvas.drawPath(mBelowWavePath, mBelowWavePaint);
在Activity中监听自定义view的波动幅度,使图片随着上下浮动;
demo下载地址
http://download.csdn.net/download/shanshan_1117/10255755