Skip to content

Commit 4e80053

Browse files
committed
New widget added
Wave view added
1 parent e2baeae commit 4e80053

File tree

4 files changed

+452
-0
lines changed

4 files changed

+452
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.bvtech.toolslibrary.widget.waveview;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.util.AttributeSet;
7+
import android.view.View;
8+
import android.widget.LinearLayout;
9+
10+
/**
11+
* Created by John on 2014/10/15.
12+
*/
13+
public class Solid extends View {
14+
15+
private Paint aboveWavePaint;
16+
private Paint blowWavePaint;
17+
18+
public Solid(Context context, AttributeSet attrs) {
19+
this(context, attrs, 0);
20+
}
21+
22+
public Solid(Context context, AttributeSet attrs, int defStyleAttr) {
23+
super(context, attrs, defStyleAttr);
24+
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
25+
params.weight = 1;
26+
setLayoutParams(params);
27+
}
28+
29+
public void setAboveWavePaint(Paint aboveWavePaint) {
30+
this.aboveWavePaint = aboveWavePaint;
31+
}
32+
33+
public void setBlowWavePaint(Paint blowWavePaint) {
34+
this.blowWavePaint = blowWavePaint;
35+
}
36+
37+
@Override
38+
protected void onDraw(Canvas canvas) {
39+
super.onDraw(canvas);
40+
canvas.drawRect(getLeft(), 0, getRight(), getBottom(), blowWavePaint);
41+
canvas.drawRect(getLeft(), 0, getRight(), getBottom(), aboveWavePaint);
42+
}
43+
}
Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
package com.bvtech.toolslibrary.widget.waveview;
2+
3+
import android.content.Context;
4+
import android.graphics.Canvas;
5+
import android.graphics.Paint;
6+
import android.graphics.Path;
7+
import android.util.AttributeSet;
8+
import android.view.View;
9+
import android.view.ViewGroup;
10+
11+
import com.bvtech.toolslibrary.R;
12+
13+
// y=Asin(ωx+φ)+k
14+
public class Wave extends View {
15+
private final int WAVE_HEIGHT_LARGE = 16;
16+
private final int WAVE_HEIGHT_MIDDLE = 8;
17+
private final int WAVE_HEIGHT_LITTLE = 5;
18+
19+
private final float WAVE_LENGTH_MULTIPLE_LARGE = 1.5f;
20+
private final float WAVE_LENGTH_MULTIPLE_MIDDLE = 1f;
21+
private final float WAVE_LENGTH_MULTIPLE_LITTLE = 0.5f;
22+
23+
private final float WAVE_HZ_FAST = 0.13f;
24+
private final float WAVE_HZ_NORMAL = 0.09f;
25+
private final float WAVE_HZ_SLOW = 0.05f;
26+
27+
public final int DEFAULT_ABOVE_WAVE_ALPHA = 50;
28+
public final int DEFAULT_BLOW_WAVE_ALPHA = 30;
29+
30+
private final float X_SPACE = 20;
31+
private final double PI2 = 2 * Math.PI;
32+
33+
private Path mAboveWavePath = new Path();
34+
private Path mBlowWavePath = new Path();
35+
36+
private Paint mAboveWavePaint = new Paint();
37+
private Paint mBlowWavePaint = new Paint();
38+
39+
private int mAboveWaveColor;
40+
private int mBlowWaveColor;
41+
42+
private float mWaveMultiple;
43+
private float mWaveLength;
44+
private int mWaveHeight;
45+
private float mMaxRight;
46+
private float mWaveHz;
47+
48+
// wave animation
49+
private float mAboveOffset = 0.0f;
50+
private float mBlowOffset;
51+
52+
private RefreshProgressRunnable mRefreshProgressRunnable;
53+
54+
private int left, right, bottom;
55+
// ω
56+
private double omega;
57+
58+
public Wave(Context context, AttributeSet attrs) {
59+
this(context, attrs, R.attr.tl_waveViewStyle);
60+
}
61+
62+
public Wave(Context context, AttributeSet attrs, int defStyle) {
63+
super(context, attrs, defStyle);
64+
}
65+
66+
@Override
67+
protected void onDraw(Canvas canvas) {
68+
super.onDraw(canvas);
69+
70+
canvas.drawPath(mBlowWavePath, mBlowWavePaint);
71+
canvas.drawPath(mAboveWavePath, mAboveWavePaint);
72+
}
73+
74+
public void setAboveWaveColor(int aboveWaveColor) {
75+
this.mAboveWaveColor = aboveWaveColor;
76+
}
77+
78+
public void setBlowWaveColor(int blowWaveColor) {
79+
this.mBlowWaveColor = blowWaveColor;
80+
}
81+
82+
public Paint getAboveWavePaint() {
83+
return mAboveWavePaint;
84+
}
85+
86+
public Paint getBlowWavePaint() {
87+
return mBlowWavePaint;
88+
}
89+
90+
public void initializeWaveSize(int waveMultiple, int waveHeight, int waveHz) {
91+
mWaveMultiple = getWaveMultiple(waveMultiple);
92+
mWaveHeight = getWaveHeight(waveHeight);
93+
mWaveHz = getWaveHz(waveHz);
94+
mBlowOffset = mWaveHeight * 0.4f;
95+
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
96+
mWaveHeight * 2);
97+
setLayoutParams(params);
98+
}
99+
100+
public void initializePainters() {
101+
mAboveWavePaint.setColor(mAboveWaveColor);
102+
mAboveWavePaint.setAlpha(DEFAULT_ABOVE_WAVE_ALPHA);
103+
mAboveWavePaint.setStyle(Paint.Style.FILL);
104+
mAboveWavePaint.setAntiAlias(true);
105+
106+
mBlowWavePaint.setColor(mBlowWaveColor);
107+
mBlowWavePaint.setAlpha(DEFAULT_BLOW_WAVE_ALPHA);
108+
mBlowWavePaint.setStyle(Paint.Style.FILL);
109+
mBlowWavePaint.setAntiAlias(true);
110+
}
111+
112+
private float getWaveMultiple(int size) {
113+
switch (size) {
114+
case WaveView.LARGE:
115+
return WAVE_LENGTH_MULTIPLE_LARGE;
116+
case WaveView.MIDDLE:
117+
return WAVE_LENGTH_MULTIPLE_MIDDLE;
118+
case WaveView.LITTLE:
119+
return WAVE_LENGTH_MULTIPLE_LITTLE;
120+
}
121+
return 0;
122+
}
123+
124+
private int getWaveHeight(int size) {
125+
switch (size) {
126+
case WaveView.LARGE:
127+
return WAVE_HEIGHT_LARGE;
128+
case WaveView.MIDDLE:
129+
return WAVE_HEIGHT_MIDDLE;
130+
case WaveView.LITTLE:
131+
return WAVE_HEIGHT_LITTLE;
132+
}
133+
return 0;
134+
}
135+
136+
private float getWaveHz(int size) {
137+
switch (size) {
138+
case WaveView.LARGE:
139+
return WAVE_HZ_FAST;
140+
case WaveView.MIDDLE:
141+
return WAVE_HZ_NORMAL;
142+
case WaveView.LITTLE:
143+
return WAVE_HZ_SLOW;
144+
}
145+
return 0;
146+
}
147+
148+
/**
149+
* calculate wave track
150+
*/
151+
private void calculatePath() {
152+
mAboveWavePath.reset();
153+
mBlowWavePath.reset();
154+
155+
getWaveOffset();
156+
157+
float y;
158+
mAboveWavePath.moveTo(left, bottom);
159+
for (float x = 0; x <= mMaxRight; x += X_SPACE) {
160+
y = (float) (mWaveHeight * Math.sin(omega * x + mAboveOffset) + mWaveHeight);
161+
mAboveWavePath.lineTo(x, y);
162+
}
163+
mAboveWavePath.lineTo(right, bottom);
164+
165+
mBlowWavePath.moveTo(left, bottom);
166+
for (float x = 0; x <= mMaxRight; x += X_SPACE) {
167+
y = (float) (mWaveHeight * Math.sin(omega * x + mBlowOffset) + mWaveHeight);
168+
mBlowWavePath.lineTo(x, y);
169+
}
170+
mBlowWavePath.lineTo(right, bottom);
171+
}
172+
173+
@Override
174+
protected void onWindowVisibilityChanged(int visibility) {
175+
super.onWindowVisibilityChanged(visibility);
176+
if (View.GONE == visibility) {
177+
removeCallbacks(mRefreshProgressRunnable);
178+
} else {
179+
removeCallbacks(mRefreshProgressRunnable);
180+
mRefreshProgressRunnable = new RefreshProgressRunnable();
181+
post(mRefreshProgressRunnable);
182+
}
183+
}
184+
185+
@Override
186+
protected void onDetachedFromWindow() {
187+
super.onDetachedFromWindow();
188+
}
189+
190+
@Override
191+
public void onWindowFocusChanged(boolean hasWindowFocus) {
192+
super.onWindowFocusChanged(hasWindowFocus);
193+
if (hasWindowFocus) {
194+
if (mWaveLength == 0) {
195+
startWave();
196+
}
197+
}
198+
}
199+
200+
@Override
201+
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
202+
super.onLayout(changed, left, top, right, bottom);
203+
if (mWaveLength==0){
204+
startWave();
205+
}
206+
}
207+
208+
private void startWave() {
209+
if (getWidth() != 0) {
210+
int width = getWidth();
211+
mWaveLength = width * mWaveMultiple;
212+
left = getLeft();
213+
right = getRight();
214+
bottom = getBottom() + 2;
215+
mMaxRight = right + X_SPACE;
216+
omega = PI2 / mWaveLength;
217+
}
218+
}
219+
220+
private void getWaveOffset() {
221+
if (mBlowOffset > Float.MAX_VALUE - 100) {
222+
mBlowOffset = 0;
223+
} else {
224+
mBlowOffset += mWaveHz;
225+
}
226+
227+
if (mAboveOffset > Float.MAX_VALUE - 100) {
228+
mAboveOffset = 0;
229+
} else {
230+
mAboveOffset += mWaveHz;
231+
}
232+
}
233+
234+
private class RefreshProgressRunnable implements Runnable {
235+
public void run() {
236+
synchronized (Wave.this) {
237+
long start = System.currentTimeMillis();
238+
239+
calculatePath();
240+
241+
invalidate();
242+
243+
long gap = 16 - (System.currentTimeMillis() - start);
244+
postDelayed(this, gap < 0 ? 0 : gap);
245+
}
246+
}
247+
}
248+
249+
}

0 commit comments

Comments
 (0)