网上开源项目:https://github.com/john990/WaveView
主要代码:
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.example.waveview.R;
/**
* Created by John on 2014/10/15.
*/
public class WaveView extends LinearLayout {
protected static final int LARGE = 1;
protected static final int MIDDLE = 2;
protected static final int LITTLE = 3;
private int mAboveWaveColor;
private int mBlowWaveColor;
private int mProgress;
private int mWaveHeight;
private int mWaveMultiple;
private int mWaveHz;
private int mWaveToTop;
private Wave mWave;
private Solid mSolid;
private final int DEFAULT_ABOVE_WAVE_COLOR = Color.WHITE;
private final int DEFAULT_BLOW_WAVE_COLOR = Color.WHITE;
private final int DEFAULT_PROGRESS = 80;
public WaveView(Context context, AttributeSet attrs) {
super(context, attrs);
setOrientation(VERTICAL);
//load styled attributes.
final TypedArray attributes = context.getTheme().obtainStyledAttributes(attrs, R.styleable.WaveView, R.attr.waveViewStyle, 0);
mAboveWaveColor = attributes.getColor(R.styleable.WaveView_above_wave_color, DEFAULT_ABOVE_WAVE_COLOR);
mBlowWaveColor = attributes.getColor(R.styleable.WaveView_blow_wave_color, DEFAULT_BLOW_WAVE_COLOR);
mProgress = attributes.getInt(R.styleable.WaveView_progress, DEFAULT_PROGRESS);
mWaveHeight = attributes.getInt(R.styleable.WaveView_wave_height, MIDDLE);
mWaveMultiple = attributes.getInt(R.styleable.WaveView_wave_length, LARGE);
mWaveHz = attributes.getInt(R.styleable.WaveView_wave_hz, MIDDLE);
attributes.recycle();
mWave = new Wave(context, null);
mWave.initializeWaveSize(mWaveMultiple, mWaveHeight, mWaveHz);
mWave.setAboveWaveColor(mAboveWaveColor);
mWave.setBlowWaveColor(mBlowWaveColor);
mWave.initializePainters();
mSolid = new Solid(context, null);
mSolid.setAboveWavePaint(mWave.getAboveWavePaint());
mSolid.setBlowWavePaint(mWave.getBlowWavePaint());
addView(mWave);
addView(mSolid);
setProgress(mProgress);
}
public void setProgress(int progress) {
this.mProgress = progress > 100 ? 100 : progress;
computeWaveToTop();
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
su