- Forum-Beiträge: 206
04.09.2013, 14:00:56 via App
04.09.2013 14:00:56 via App
Ich habe einen Custom-Surface View für Spiele zusammengesetzt. Wenn ich in einer Activity eine andere aufrufe, und die alte beende, hängt sich die Anwendung relativ häufig auf, vor allem, wenn man die Anwendung mit der Hardware-Zurück-Taste schließt und neu öffnet. Woran kann das liegen?
Mein Code des PanelViews:
2
3import android.content.*;
4import android.graphics.*;
5import android.util.*;
6import android.view.*;
7import android.widget.*;
8
9public abstract class PanelView extends SurfaceView implements SurfaceHolder.Callback
10{
11
12 PanelThread _thread;
13 public Paint paint;
14 public Float panelX, panelY;
15 public Canvas canvas;
16
17 public PanelView(Context context) {
18 super(context);
19 getHolder().addCallback(this);
20
21 postInvalidate();
22 }
23
24 public PanelView(Context c, AttributeSet attr) {
25 super(c, attr);
26 getHolder().addCallback(this);
27
28 postInvalidate();
29 }
30
31 @Override
32 public void onDraw(Canvas canvas) {
33 try {
34 if(paint == null) {
35 paint = new Paint();
36
37 panelX = Float.valueOf(canvas.getWidth());
38 panelY = Float.valueOf(canvas.getHeight());
39
40 }
41 if(this.canvas == null) {
42 this.canvas = canvas;
43 }
44
45 onPostInvalidate(canvas);
46
47 if(getHolder().getSurface().isValid()) {
48 postInvalidate();
49 }
50 } catch(Exception e) {
51 new Thread() {
52 public void run() {
53 try {
54 sleep(100);
55 } catch(Exception e) {
56 e.printStackTrace();
57 } finally {
58 postInvalidate();
59 }
60 }
61 }.start();
62 }
63 }
64
65 public abstract void onPostInvalidate(Canvas canvas);
66
67 @Override
68 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
69 getHolder().getSurface().isValid();
70 }
71
72 class PanelThread extends Thread {
73 private SurfaceHolder _surfaceHolder;
74 private PanelView _panel;
75 private boolean _run = false;
76
77 public PanelThread(SurfaceHolder surfaceHolder, PanelView panel) {
78 _surfaceHolder = surfaceHolder;
79 _panel = panel;
80 }
81
82 public void setRunning(boolean run) {
83 //Allow us to stop the thread
84 _run = run;
85 }
86
87 @Override
88 public void run() {
89 Canvas c;
90 while(_run) {
91 c = null;
92 //When setRunning(false) occurs, _run is
93 //set to false and loop ends, stopping thread
94 try {
95 c = _surfaceHolder.lockCanvas(null);
96 synchronized(_surfaceHolder) {
97 //Insert methods to modify positions of items in onDraw()
98 postInvalidate();
99 }
100 } finally {
101 if (c != null) {
102 _surfaceHolder.unlockCanvasAndPost(c);
103 }
104 }
105 }
106 }
107 }
108
109 @Override
110 public void surfaceCreated(SurfaceHolder holder) {
111 setWillNotDraw(false);
112 //Allows us to use invalidate() to call onDraw()
113
114 _thread = new PanelThread(getHolder(), this);
115 //Start the thread that
116 _thread.setRunning(true);
117 //will make calls to
118 _thread.start();
119 //onDraw()
120
121 getHolder().getSurface().isValid();
122 }
123
124 @Override
125 public void surfaceDestroyed(SurfaceHolder holder) {
126 try {
127 _thread.setRunning(false);
128 //Tells thread to stop
129 _thread.join();
130 //Removes thread from mem.
131 } catch (InterruptedException e) {
132
133 }
134 }
135
136}
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.