Skip to content

Commit

Permalink
fix: #18
Browse files Browse the repository at this point in the history
  • Loading branch information
JayGoo committed Sep 1, 2019
1 parent f8056d9 commit edaa825
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 26 deletions.
40 changes: 22 additions & 18 deletions library/src/main/java/jaygoo/widget/wlv/RenderView.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.lang.ref.WeakReference;
import java.util.List;

/**
* ================================================
* 作 者:JayGoo
Expand All @@ -22,12 +23,15 @@ public abstract class RenderView extends SurfaceView implements SurfaceHolder.Ca
private boolean isStartAnim = false;
private final static Object surfaceLock = new Object();
private RenderThread renderThread;

/**
* 绘制背景,防止开始时黑屏
* 子View可以执行此方法
*
* @param canvas
*/
protected abstract void doDrawBackground(Canvas canvas);

/**
* 渲染surfaceView的回调方法。
*
Expand Down Expand Up @@ -57,19 +61,20 @@ private static class RenderThread extends Thread {
private boolean running = false;
private boolean destoryed = false;
private boolean isPause = false;

public RenderThread(RenderView renderView) {
super("RenderThread");
this.renderView = new WeakReference<>(renderView);
}

private SurfaceHolder getSurfaceHolder(){
if (getRenderView() != null ){
private SurfaceHolder getSurfaceHolder() {
if (getRenderView() != null) {
return getRenderView().getHolder();
}
return null;
}

private RenderView getRenderView(){
private RenderView getRenderView() {
return renderView.get();
}

Expand All @@ -80,7 +85,7 @@ public void run() {
synchronized (surfaceLock) {

//这里并没有真正的结束Thread,防止部分手机连续调用同一Thread出错
while (isPause){
while (isPause) {
try {
surfaceLock.wait();
} catch (InterruptedException e) {
Expand All @@ -98,7 +103,7 @@ public void run() {
}
getSurfaceHolder().unlockCanvasAndPost(canvas);
}
}else {
} else {
running = false;
}

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



@Override
public void surfaceCreated(SurfaceHolder holder) {
renderer = onCreateRenderer();
Expand All @@ -138,8 +142,8 @@ public void surfaceCreated(SurfaceHolder holder) {
* 解锁暂停,继续执行绘制任务
* 默认当Resume时不自动启动动画
*/
public void onResume(){
synchronized (surfaceLock){
public void onResume() {
synchronized (surfaceLock) {
if (renderThread != null) {
renderThread.isPause = false;
surfaceLock.notifyAll();
Expand All @@ -149,8 +153,8 @@ public void onResume(){


//假暂停,并没有结束Thread
public void onPause(){
synchronized (surfaceLock){
public void onPause() {
synchronized (surfaceLock) {
if (renderThread != null) {
renderThread.isPause = true;
}
Expand All @@ -172,9 +176,9 @@ public void surfaceDestroyed(SurfaceHolder holder) {
}

public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus && isStartAnim){
if (hasFocus && isStartAnim) {
startAnim();
}else {
} else {
startThread();
}
}
Expand All @@ -200,12 +204,12 @@ private void render(Canvas canvas, long millisPassed) {
}
}

public void startAnim(){
public void startAnim() {
isStartAnim = true;
startThread();
}

private void startThread(){
private void startThread() {

if (renderThread != null && !renderThread.running) {
renderThread.setRun(true);
Expand All @@ -214,30 +218,30 @@ private void startThread(){
renderThread.start();
}

}catch (RuntimeException e){
} catch (Exception e) {
e.printStackTrace();
}

}
}

public void stopAnim(){
public void stopAnim() {
isStartAnim = false;
if (renderThread != null && renderThread.running) {
renderThread.setRun(false);
renderThread.interrupt();
}
}

public boolean isRunning(){
public boolean isRunning() {
if (renderThread != null) {
return renderThread.running;
}
return false;
}

//释放相关资源,防止内存泄漏
public void release(){
public void release() {
if (getHolder() != null && getHolder().getSurface() != null) {
getHolder().getSurface().release();
getHolder().removeCallback(this);
Expand Down
16 changes: 8 additions & 8 deletions library/src/main/java/jaygoo/widget/wlv/WaveLineView.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,17 +167,16 @@ protected void onRender(Canvas canvas, long millisPassed) {
resetPaths();
softerChangeVolume();

//双重判断确保必要参数正常
if (isParametersNull()) {
initDraw(canvas);
}
if (isParametersNull()) {
return;
}

//波形函数的值
float curY;
for (int i = 0; i <= samplingSize; i++) {
//双重判断确保必要参数正常
if (isParametersNull()) {
initDraw(canvas);
if (isParametersNull()) {
return;
}
}
float x = samplingX[i];
curY = (float) (amplitude * calcValue(mapX[i], offset));
for (int n = 0; n < paths.size(); n++) {
Expand Down Expand Up @@ -415,6 +414,7 @@ public void setVolume(int volume) {

public void setBackGroundColor(int backGroundColor) {
this.backGroundColor = backGroundColor;
this.isTransparentMode = (backGroundColor == Color.TRANSPARENT);
}

public void setLineColor(int lineColor) {
Expand Down

0 comments on commit edaa825

Please sign in to comment.