88
99import java .lang .ref .WeakReference ;
1010import 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 );
0 commit comments