8
8
9
9
import java .lang .ref .WeakReference ;
10
10
import java .util .List ;
11
+
11
12
/**
12
13
* ================================================
13
14
* 作 者:JayGoo
@@ -22,12 +23,15 @@ public abstract class RenderView extends SurfaceView implements SurfaceHolder.Ca
22
23
private boolean isStartAnim = false ;
23
24
private final static Object surfaceLock = new Object ();
24
25
private RenderThread renderThread ;
26
+
25
27
/**
26
28
* 绘制背景,防止开始时黑屏
27
29
* 子View可以执行此方法
30
+ *
28
31
* @param canvas
29
32
*/
30
33
protected abstract void doDrawBackground (Canvas canvas );
34
+
31
35
/**
32
36
* 渲染surfaceView的回调方法。
33
37
*
@@ -57,19 +61,20 @@ private static class RenderThread extends Thread {
57
61
private boolean running = false ;
58
62
private boolean destoryed = false ;
59
63
private boolean isPause = false ;
64
+
60
65
public RenderThread (RenderView renderView ) {
61
66
super ("RenderThread" );
62
67
this .renderView = new WeakReference <>(renderView );
63
68
}
64
69
65
- private SurfaceHolder getSurfaceHolder (){
66
- if (getRenderView () != null ) {
70
+ private SurfaceHolder getSurfaceHolder () {
71
+ if (getRenderView () != null ) {
67
72
return getRenderView ().getHolder ();
68
73
}
69
74
return null ;
70
75
}
71
76
72
- private RenderView getRenderView (){
77
+ private RenderView getRenderView () {
73
78
return renderView .get ();
74
79
}
75
80
@@ -80,7 +85,7 @@ public void run() {
80
85
synchronized (surfaceLock ) {
81
86
82
87
//这里并没有真正的结束Thread,防止部分手机连续调用同一Thread出错
83
- while (isPause ){
88
+ while (isPause ) {
84
89
try {
85
90
surfaceLock .wait ();
86
91
} catch (InterruptedException e ) {
@@ -98,7 +103,7 @@ public void run() {
98
103
}
99
104
getSurfaceHolder ().unlockCanvasAndPost (canvas );
100
105
}
101
- }else {
106
+ } else {
102
107
running = false ;
103
108
}
104
109
@@ -123,7 +128,6 @@ public void setRun(boolean isRun) {
123
128
}
124
129
125
130
126
-
127
131
@ Override
128
132
public void surfaceCreated (SurfaceHolder holder ) {
129
133
renderer = onCreateRenderer ();
@@ -138,8 +142,8 @@ public void surfaceCreated(SurfaceHolder holder) {
138
142
* 解锁暂停,继续执行绘制任务
139
143
* 默认当Resume时不自动启动动画
140
144
*/
141
- public void onResume (){
142
- synchronized (surfaceLock ){
145
+ public void onResume () {
146
+ synchronized (surfaceLock ) {
143
147
if (renderThread != null ) {
144
148
renderThread .isPause = false ;
145
149
surfaceLock .notifyAll ();
@@ -149,8 +153,8 @@ public void onResume(){
149
153
150
154
151
155
//假暂停,并没有结束Thread
152
- public void onPause (){
153
- synchronized (surfaceLock ){
156
+ public void onPause () {
157
+ synchronized (surfaceLock ) {
154
158
if (renderThread != null ) {
155
159
renderThread .isPause = true ;
156
160
}
@@ -172,9 +176,9 @@ public void surfaceDestroyed(SurfaceHolder holder) {
172
176
}
173
177
174
178
public void onWindowFocusChanged (boolean hasFocus ) {
175
- if (hasFocus && isStartAnim ){
179
+ if (hasFocus && isStartAnim ) {
176
180
startAnim ();
177
- }else {
181
+ } else {
178
182
startThread ();
179
183
}
180
184
}
@@ -200,12 +204,12 @@ private void render(Canvas canvas, long millisPassed) {
200
204
}
201
205
}
202
206
203
- public void startAnim (){
207
+ public void startAnim () {
204
208
isStartAnim = true ;
205
209
startThread ();
206
210
}
207
211
208
- private void startThread (){
212
+ private void startThread () {
209
213
210
214
if (renderThread != null && !renderThread .running ) {
211
215
renderThread .setRun (true );
@@ -214,30 +218,30 @@ private void startThread(){
214
218
renderThread .start ();
215
219
}
216
220
217
- }catch (RuntimeException e ){
221
+ } catch (Exception e ) {
218
222
e .printStackTrace ();
219
223
}
220
224
221
225
}
222
226
}
223
227
224
- public void stopAnim (){
228
+ public void stopAnim () {
225
229
isStartAnim = false ;
226
230
if (renderThread != null && renderThread .running ) {
227
231
renderThread .setRun (false );
228
232
renderThread .interrupt ();
229
233
}
230
234
}
231
235
232
- public boolean isRunning (){
236
+ public boolean isRunning () {
233
237
if (renderThread != null ) {
234
238
return renderThread .running ;
235
239
}
236
240
return false ;
237
241
}
238
242
239
243
//释放相关资源,防止内存泄漏
240
- public void release (){
244
+ public void release () {
241
245
if (getHolder () != null && getHolder ().getSurface () != null ) {
242
246
getHolder ().getSurface ().release ();
243
247
getHolder ().removeCallback (this );
0 commit comments