impjor
- Forum-Beiträge: 1.793
10.05.2013, 19:03:53 via Website
10.05.2013 19:03:53 via Website
Hallo!
Ich stehe gerade vor einem Problem, welches mit einfach nicht einleuchten will! Ich erzeuge meine eigene SurfaceView und möchte in der MainActivity einfach einen Text (mit der Anzahl der FPS) anzeigen. Außerdem zeichne ich da ein paar Bitmaps. Die Bitmaps werden anstandslos gezeichnet, vom Text ist aber nichts zu sehen
Die CanvasView-Klasse
Wäre total froh, wenn jemand Licht ins Dunkel bringen würde...
Gruß
Ich stehe gerade vor einem Problem, welches mit einfach nicht einleuchten will! Ich erzeuge meine eigene SurfaceView und möchte in der MainActivity einfach einen Text (mit der Anzahl der FPS) anzeigen. Außerdem zeichne ich da ein paar Bitmaps. Die Bitmaps werden anstandslos gezeichnet, vom Text ist aber nichts zu sehen
1Paint p = new Paint();
2
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 mCanvasView = new CanvasView(this);
6 mCanvasView.setOnDrawNewFrameListener(this);
7 setContentView(mCanvasView);
8 }
9
10 public void onDraw(Canvas c) {
11//Ermitteln von fps usw.
12...
13 // Zeichne Hintergrund
14 c.drawRGB(75, 242, 150);
15
16 //Zeichnen von ein paar Bitmaps, klappt anstandslos!
17
18
19 // Zeichen FPS
20 p.setColor(Color.BLACK);
21 p.setTextSize(20);
22 p.setStyle(Style.FILL);
23 c.drawText("FPS: " + fps, 0, 0, p);
24 }
2
3 public void onCreate(Bundle savedInstanceState) {
4 super.onCreate(savedInstanceState);
5 mCanvasView = new CanvasView(this);
6 mCanvasView.setOnDrawNewFrameListener(this);
7 setContentView(mCanvasView);
8 }
9
10 public void onDraw(Canvas c) {
11//Ermitteln von fps usw.
12...
13 // Zeichne Hintergrund
14 c.drawRGB(75, 242, 150);
15
16 //Zeichnen von ein paar Bitmaps, klappt anstandslos!
17
18
19 // Zeichen FPS
20 p.setColor(Color.BLACK);
21 p.setTextSize(20);
22 p.setStyle(Style.FILL);
23 c.drawText("FPS: " + fps, 0, 0, p);
24 }
Die CanvasView-Klasse
1public class CanvasView extends SurfaceView implements Callback {
2 private static final long THREAD_SLEEP = 10;
3 private SurfaceHolder holder = null;
4 private boolean mThreadRunning = false;
5 private onDrawNewFrameListener odl = null;
6
7
8 public CanvasView(Context context) {
9 super(context);
10 init();
11 }
12
13 public CanvasView(Context context, AttributeSet attrs) {
14 super(context, attrs);
15 init();
16 }
17
18 public CanvasView(Context context, AttributeSet attrs, int defStyle) {
19 super(context, attrs, defStyle);
20 init();
21 }
22
23 private void init() {
24 holder = getHolder();
25 holder.addCallback(this);
26 }
27
28 @Override
29 public void surfaceChanged(SurfaceHolder holder, int format, int width,
30 int height) {
31
32 }
33
34 @Override
35 public void surfaceCreated(SurfaceHolder holder) {
36 mThreadRunning = true;
37 new Thread(this).start();
38 }
39
40 @Override
41 public void surfaceDestroyed(SurfaceHolder holder) {
42 mThreadRunning = false;
43 }
44
45 @Override
46 public void run() {
47 // Runns while we want to draw in our Surface
48 Canvas can;
49 while (mThreadRunning) {
50 if (odl != null) {
51 can = holder.lockCanvas();
52 odl.onDraw(can);
53 holder.unlockCanvasAndPost(can);
54 }
55 try {
56 Thread.sleep(THREAD_SLEEP);
57 } catch (InterruptedException e) {
58 e.printStackTrace();
59 Log.e("CanvasView", "InterruptedException while sleep in run!");
60 }
61 }
62 }
63
64 public void setOnDrawNewFrameListener(onDrawNewFrameListener odl) {
65 this.odl = odl;
66 }
67
68 public interface onDrawNewFrameListener {
69 public void onDraw(Canvas c);
70 }
71
72}
2 private static final long THREAD_SLEEP = 10;
3 private SurfaceHolder holder = null;
4 private boolean mThreadRunning = false;
5 private onDrawNewFrameListener odl = null;
6
7
8 public CanvasView(Context context) {
9 super(context);
10 init();
11 }
12
13 public CanvasView(Context context, AttributeSet attrs) {
14 super(context, attrs);
15 init();
16 }
17
18 public CanvasView(Context context, AttributeSet attrs, int defStyle) {
19 super(context, attrs, defStyle);
20 init();
21 }
22
23 private void init() {
24 holder = getHolder();
25 holder.addCallback(this);
26 }
27
28 @Override
29 public void surfaceChanged(SurfaceHolder holder, int format, int width,
30 int height) {
31
32 }
33
34 @Override
35 public void surfaceCreated(SurfaceHolder holder) {
36 mThreadRunning = true;
37 new Thread(this).start();
38 }
39
40 @Override
41 public void surfaceDestroyed(SurfaceHolder holder) {
42 mThreadRunning = false;
43 }
44
45 @Override
46 public void run() {
47 // Runns while we want to draw in our Surface
48 Canvas can;
49 while (mThreadRunning) {
50 if (odl != null) {
51 can = holder.lockCanvas();
52 odl.onDraw(can);
53 holder.unlockCanvasAndPost(can);
54 }
55 try {
56 Thread.sleep(THREAD_SLEEP);
57 } catch (InterruptedException e) {
58 e.printStackTrace();
59 Log.e("CanvasView", "InterruptedException while sleep in run!");
60 }
61 }
62 }
63
64 public void setOnDrawNewFrameListener(onDrawNewFrameListener odl) {
65 this.odl = odl;
66 }
67
68 public interface onDrawNewFrameListener {
69 public void onDraw(Canvas c);
70 }
71
72}
Wäre total froh, wenn jemand Licht ins Dunkel bringen würde...
Gruß
Liebe Grüße impjor.
Für ein gutes Miteinander: Unsere Regeln
Apps für jeden Einsatzzweck
Stellt eure App vor!
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.