Android实现波浪效果 - WaveView
jopen
9年前
实现
WaveView的属性
Wate Level(水位)波浪静止时水面距离底部的高度Amplitude(振幅)波浪垂直振动时偏离水面的最大距离Wave Length(波长)一个完整的波浪的水平长度Wave Shift(偏移)波浪相对于初始位置的水平偏移
实现思路
设想我们有一个画好波形的图片,那么我们只需要用这张图片填充(X轴方向重复,Y轴方向延伸)整个View,然后水平移动图片,就可以得到波浪效果了。
所以要做的事很简单:绘制一个波形图,填充到View里,移动波形图。
1. 绘制初始波形
private void createShader() { ... Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); // Draw default waves into the bitmap // y=Asin(ωx+φ)+h float waveX1 = 0; final float wave2Shift = mDefaultWaveLength / 4; final float endX = getWidth(); final float endY = getHeight(); ... while (waveX1 < endX) { double wx = waveX1 * mDefaultAngularFrequency; int startY = (int) (mDefaultWaterLevel + mDefaultAmplitude * Math.sin(wx)); // draw bottom wave with the alpha 40 canvas.drawLine(waveX1, startY, waveX1, endY, wavePaint1); // draw top wave with the alpha 60 float waveX2 = (waveX1 + wave2Shift) % endX; canvas.drawLine(waveX2, startY, waveX2, endY, wavePaint2); waveX1++; } // use the bitamp to create the shader mWaveShader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.CLAMP); mViewPaint.setShader(mWaveShader); }