Skip to content

Commit

Permalink
修复View初始黑屏的Bug
Browse files Browse the repository at this point in the history
  • Loading branch information
JinJieGu committed Jul 31, 2017
1 parent db2c796 commit 072268b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void onClick(View v) {
@Override
protected void onResume() {
super.onResume();
waveLineView.onResume(true);
waveLineView.onResume();
}

@Override
Expand Down
10 changes: 9 additions & 1 deletion app/src/main/java/jaygoo/wavelineview/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,19 @@ public void onClick(View v) {
});
}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus){
// waveLineView.justDrawBackground();
}
}

@Override
protected void onResume() {
super.onResume();
waveLineView.onResume(true);
waveLineView.onResume();

}

@Override
Expand Down
69 changes: 36 additions & 33 deletions library/src/main/java/jaygoo/widget/wlv/RenderView.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,22 @@
*/
public abstract class RenderView extends SurfaceView implements SurfaceHolder.Callback {

private boolean isAutoStartAnim = false;
//是否正在绘制动画
private boolean isStartAnim = false;
private final static Object surfaceLock = new Object();
private RenderThread renderThread;
/**
* 绘制背景,防止开始时黑屏
* 子View可以执行此方法
* @param canvas
*/
protected abstract void doDrawBackground(Canvas canvas);
/**
* 渲染surfaceView的回调方法。
*
* @param canvas 画布
*/
protected abstract void onRender(Canvas canvas, long millisPassed);

public RenderView(Context context) {
this(context, null);
Expand Down Expand Up @@ -77,19 +92,22 @@ public void run() {
if (getSurfaceHolder() != null && getRenderView() != null) {
Canvas canvas = getSurfaceHolder().lockCanvas();
if (canvas != null) {
getRenderView().render(canvas, System.currentTimeMillis() - startAt); //这里做真正绘制的事情
getRenderView().doDrawBackground(canvas);
if (getRenderView().isStartAnim) {
getRenderView().render(canvas, System.currentTimeMillis() - startAt); //这里做真正绘制的事情
}
getSurfaceHolder().unlockCanvasAndPost(canvas);
}
}else {
running = false;
}
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
try {
Thread.sleep(SLEEP_TIME);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

}
Expand All @@ -103,8 +121,7 @@ public void setRun(boolean isRun) {

}

private final static Object surfaceLock = new Object();
private RenderThread renderThread;


@Override
public void surfaceCreated(SurfaceHolder holder) {
Expand All @@ -130,20 +147,6 @@ public void onResume(){
}


/**
* 解锁暂停,继续执行绘制任务
* @param isAutoStartAnim 是否当Resume时自动启动动画
*/
public void onResume(boolean isAutoStartAnim){
synchronized (surfaceLock){
this.isAutoStartAnim = isAutoStartAnim;
if (renderThread != null) {
renderThread.isPause = false;
surfaceLock.notifyAll();
}
}
}

//假暂停,并没有结束Thread
public void onPause(){
synchronized (surfaceLock){
Expand All @@ -168,12 +171,14 @@ public void surfaceDestroyed(SurfaceHolder holder) {
}

public void onWindowFocusChanged(boolean hasFocus) {
if (hasFocus && isAutoStartAnim){
if (hasFocus && isStartAnim){
startAnim();
}else {
startThread();
}
}
/*绘图*/

/*绘图*/
public interface IRenderer {
void onRender(Canvas canvas, long millisPassed);
}
Expand All @@ -194,15 +199,12 @@ private void render(Canvas canvas, long millisPassed) {
}
}

/**
* 渲染surfaceView的回调方法。
*
* @param canvas 画布
*/
protected void onRender(Canvas canvas, long millisPassed) {
public void startAnim(){
isStartAnim = true;
startThread();
}

public void startAnim(){
private void startThread(){

if (renderThread != null && !renderThread.running) {
renderThread.setRun(true);
Expand All @@ -219,6 +221,7 @@ public void startAnim(){
}

public void stopAnim(){
isStartAnim = false;
if (renderThread != null && renderThread.running) {
renderThread.setRun(false);
renderThread.interrupt();
Expand Down
11 changes: 7 additions & 4 deletions library/src/main/java/jaygoo/widget/wlv/WaveLineView.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,17 +124,20 @@ private void initAttr(AttributeSet attrs) {
checkSensibilityValue();
}

@Override
protected void doDrawBackground(Canvas canvas) {
//绘制背景
canvas.drawColor(backGroundColor);
}

@Override
protected void onRender(Canvas canvas, long millisPassed) {
super.onRender(canvas, millisPassed);
float offset = millisPassed / offsetSpeed;

if (null == samplingX){
initDraw(canvas);
}

//绘制背景
canvas.drawColor(backGroundColor);
if (lineAnim(canvas)) {
resetPaths();
softerChangeVolume();
Expand Down Expand Up @@ -276,7 +279,7 @@ public void startAnim() {
@Override
public void stopAnim() {
super.stopAnim();
// clearDraw();
clearDraw();
}

//清空画布所有内容
Expand Down

0 comments on commit 072268b

Please sign in to comment.