This repository was archived by the owner on Mar 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Expand file tree
/
Copy pathWaveView.java
More file actions
140 lines (118 loc) · 4.58 KB
/
WaveView.java
File metadata and controls
140 lines (118 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.john.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;
/**
* 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 boolean mIsFilledTillBrim;
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);
mIsFilledTillBrim = attributes.getBoolean(R.styleable.WaveView_fill_to_brim,false);
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) {
super.onWindowFocusChanged(hasWindowFocus);
if (hasWindowFocus) {
computeWaveToTop();
}
}
private void computeWaveToTop() {
boolean isViewFilled = mProgress==100 && mIsFilledTillBrim;
mWaveToTop = (int)((isViewFilled)? (-mWave.getMaxHeight()) : (getHeight() * (1f - mProgress / 100f)));
ViewGroup.LayoutParams params = mWave.getLayoutParams();
if (params != null) {
((LayoutParams) params).topMargin = mWaveToTop;
}
mWave.setLayoutParams(params);
}
@Override
public Parcelable onSaveInstanceState() {
// Force our ancestor class to save its state
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.progress = mProgress;
return ss;
}
@Override
public void onRestoreInstanceState(Parcelable state) {
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
setProgress(ss.progress);
}
private static class SavedState extends BaseSavedState {
int progress;
/**
* Constructor called from {@link android.widget.ProgressBar#onSaveInstanceState()}
*/
SavedState(Parcelable superState) {
super(superState);
}
/**
* Constructor called from {@link #CREATOR}
*/
private SavedState(Parcel in) {
super(in);
progress = in.readInt();
}
@Override
public void writeToParcel(Parcel out, int flags) {
super.writeToParcel(out, flags);
out.writeInt(progress);
}
public static final Creator<SavedState> CREATOR = new Creator<SavedState>() {
public SavedState createFromParcel(Parcel in) {
return new SavedState(in);
}
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
}