Skip to content

Commit edaa825

Browse files
author
JayGoo
committed
fix: #18
1 parent f8056d9 commit edaa825

File tree

2 files changed

+30
-26
lines changed

2 files changed

+30
-26
lines changed

library/src/main/java/jaygoo/widget/wlv/RenderView.java

+22-18
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.lang.ref.WeakReference;
1010
import java.util.List;
11+
1112
/**
1213
* ================================================
1314
* 作 者:JayGoo
@@ -22,12 +23,15 @@ public abstract class RenderView extends SurfaceView implements SurfaceHolder.Ca
2223
private boolean isStartAnim = false;
2324
private final static Object surfaceLock = new Object();
2425
private RenderThread renderThread;
26+
2527
/**
2628
* 绘制背景,防止开始时黑屏
2729
* 子View可以执行此方法
30+
*
2831
* @param canvas
2932
*/
3033
protected abstract void doDrawBackground(Canvas canvas);
34+
3135
/**
3236
* 渲染surfaceView的回调方法。
3337
*
@@ -57,19 +61,20 @@ private static class RenderThread extends Thread {
5761
private boolean running = false;
5862
private boolean destoryed = false;
5963
private boolean isPause = false;
64+
6065
public RenderThread(RenderView renderView) {
6166
super("RenderThread");
6267
this.renderView = new WeakReference<>(renderView);
6368
}
6469

65-
private SurfaceHolder getSurfaceHolder(){
66-
if (getRenderView() != null ){
70+
private SurfaceHolder getSurfaceHolder() {
71+
if (getRenderView() != null) {
6772
return getRenderView().getHolder();
6873
}
6974
return null;
7075
}
7176

72-
private RenderView getRenderView(){
77+
private RenderView getRenderView() {
7378
return renderView.get();
7479
}
7580

@@ -80,7 +85,7 @@ public void run() {
8085
synchronized (surfaceLock) {
8186

8287
//这里并没有真正的结束Thread,防止部分手机连续调用同一Thread出错
83-
while (isPause){
88+
while (isPause) {
8489
try {
8590
surfaceLock.wait();
8691
} catch (InterruptedException e) {
@@ -98,7 +103,7 @@ public void run() {
98103
}
99104
getSurfaceHolder().unlockCanvasAndPost(canvas);
100105
}
101-
}else {
106+
} else {
102107
running = false;
103108
}
104109

@@ -123,7 +128,6 @@ public void setRun(boolean isRun) {
123128
}
124129

125130

126-
127131
@Override
128132
public void surfaceCreated(SurfaceHolder holder) {
129133
renderer = onCreateRenderer();
@@ -138,8 +142,8 @@ public void surfaceCreated(SurfaceHolder holder) {
138142
* 解锁暂停,继续执行绘制任务
139143
* 默认当Resume时不自动启动动画
140144
*/
141-
public void onResume(){
142-
synchronized (surfaceLock){
145+
public void onResume() {
146+
synchronized (surfaceLock) {
143147
if (renderThread != null) {
144148
renderThread.isPause = false;
145149
surfaceLock.notifyAll();
@@ -149,8 +153,8 @@ public void onResume(){
149153

150154

151155
//假暂停,并没有结束Thread
152-
public void onPause(){
153-
synchronized (surfaceLock){
156+
public void onPause() {
157+
synchronized (surfaceLock) {
154158
if (renderThread != null) {
155159
renderThread.isPause = true;
156160
}
@@ -172,9 +176,9 @@ public void surfaceDestroyed(SurfaceHolder holder) {
172176
}
173177

174178
public void onWindowFocusChanged(boolean hasFocus) {
175-
if (hasFocus && isStartAnim){
179+
if (hasFocus && isStartAnim) {
176180
startAnim();
177-
}else {
181+
} else {
178182
startThread();
179183
}
180184
}
@@ -200,12 +204,12 @@ private void render(Canvas canvas, long millisPassed) {
200204
}
201205
}
202206

203-
public void startAnim(){
207+
public void startAnim() {
204208
isStartAnim = true;
205209
startThread();
206210
}
207211

208-
private void startThread(){
212+
private void startThread() {
209213

210214
if (renderThread != null && !renderThread.running) {
211215
renderThread.setRun(true);
@@ -214,30 +218,30 @@ private void startThread(){
214218
renderThread.start();
215219
}
216220

217-
}catch (RuntimeException e){
221+
} catch (Exception e) {
218222
e.printStackTrace();
219223
}
220224

221225
}
222226
}
223227

224-
public void stopAnim(){
228+
public void stopAnim() {
225229
isStartAnim = false;
226230
if (renderThread != null && renderThread.running) {
227231
renderThread.setRun(false);
228232
renderThread.interrupt();
229233
}
230234
}
231235

232-
public boolean isRunning(){
236+
public boolean isRunning() {
233237
if (renderThread != null) {
234238
return renderThread.running;
235239
}
236240
return false;
237241
}
238242

239243
//释放相关资源,防止内存泄漏
240-
public void release(){
244+
public void release() {
241245
if (getHolder() != null && getHolder().getSurface() != null) {
242246
getHolder().getSurface().release();
243247
getHolder().removeCallback(this);

library/src/main/java/jaygoo/widget/wlv/WaveLineView.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,16 @@ protected void onRender(Canvas canvas, long millisPassed) {
167167
resetPaths();
168168
softerChangeVolume();
169169

170-
//双重判断确保必要参数正常
171-
if (isParametersNull()) {
172-
initDraw(canvas);
173-
}
174-
if (isParametersNull()) {
175-
return;
176-
}
177-
178170
//波形函数的值
179171
float curY;
180172
for (int i = 0; i <= samplingSize; i++) {
173+
//双重判断确保必要参数正常
174+
if (isParametersNull()) {
175+
initDraw(canvas);
176+
if (isParametersNull()) {
177+
return;
178+
}
179+
}
181180
float x = samplingX[i];
182181
curY = (float) (amplitude * calcValue(mapX[i], offset));
183182
for (int n = 0; n < paths.size(); n++) {
@@ -415,6 +414,7 @@ public void setVolume(int volume) {
415414

416415
public void setBackGroundColor(int backGroundColor) {
417416
this.backGroundColor = backGroundColor;
417+
this.isTransparentMode = (backGroundColor == Color.TRANSPARENT);
418418
}
419419

420420
public void setLineColor(int lineColor) {

0 commit comments

Comments
 (0)