auf Canvas zeichnen

  • Antworten:1
max theis
  • Forum-Beiträge: 31

08.09.2010, 11:03:19 via Website

Guten Morgen,

ich hab mithilfe eines Tutorials http://www.droidnova.com/playing-with-graphics-in-android-part-iii,176.html

ein Programm erstellt in dem ich ein Icon verschieben kann. Jetzt möchte ich statt mit dem Icon, auf der Canvas malen bzw. zeichnen.

Panel.java // Meine Klasse die fürs zeichnen zuständig ist.

1package com.test.graphics;
2
3import android.content.Context;
4import android.graphics.Bitmap;
5import android.graphics.BitmapFactory;
6import android.graphics.Canvas;
7import android.graphics.Color;
8import android.graphics.Paint;
9import android.graphics.Paint.Style;
10import android.view.MotionEvent;
11import android.view.SurfaceHolder;
12import android.view.SurfaceView;
13import android.view.View;
14
15public class Panel extends SurfaceView
16 implements SurfaceHolder.Callback{
17
18 private DrawThread _thread;
19 private int _x = 20;
20 private int _y = 20;
21
22 Paint paint = new Paint();
23
24
25 public Panel(Context context) {
26 super(context);
27 getHolder().addCallback(this);
28 _thread = new DrawThread(getHolder(),this);
29 setFocusable(true);
30
31
32 }
33
34
35
36 public boolean onTouchEvent (MotionEvent event){
37 _x = (int) event.getX();
38 _y = (int) event.getY();
39 return true;
40
41 }
42
43 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
44
45 }
46
47 public void surfaceCreated (SurfaceHolder holder){
48 _thread.setRunning(true);
49 _thread.start();
50
51 }
52
53 public void surfaceDestroyed(SurfaceHolder holder){
54 boolean retry = true;
55 _thread.setRunning(false);
56
57 while(retry){
58 try{
59 _thread.join();
60 retry = false;
61 }catch(InterruptedException e){
62 }
63
64
65 }
66
67 }
68
69
70 @Override
71 public void onDraw(Canvas canvas) {
72
73 Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
74 canvas.drawColor(Color.BLACK);
75 canvas.drawBitmap(_scratch, _x-(_scratch.getWidth()/2), _y-(_scratch.getHeight()/2), null);
76
77 }
78
79}


Die onDraw muss geändert werden, in ihr muss ich den Hintergrund und die Art und Weise wie ich auf der Canvas zeichnen möchte definieren. Nur wie?
Kann mir jemand mal ein bisschen unter die Arme greifen = )

Vielen Dank.

Gruß
max

Antworten
max theis
  • Forum-Beiträge: 31

08.09.2010, 11:26:49 via Website

So, hier ist mal ein Versuch mein Problem zu lösen, vielleicht kann mir jemand sagen, ob ich auf dem richtigen Weg bin ?!

1package com.test.graphics;
2
3import android.content.Context;
4import android.graphics.Bitmap;
5import android.graphics.BitmapFactory;
6import android.graphics.Canvas;
7import android.graphics.Color;
8import android.graphics.Paint;
9import android.graphics.Paint.Style;
10import android.view.MotionEvent;
11import android.view.SurfaceHolder;
12import android.view.SurfaceView;
13import android.view.View;
14
15public class Panel extends SurfaceView
16 implements SurfaceHolder.Callback{
17
18 private DrawThread _thread;
19 private int _x = 20;
20 private int _y = 20;
21
22 Paint paint = new Paint();
23 Bitmap bitmap;
24 Canvas bitmapCanvas;
25 boolean isInitialized;
26
27
28 public Panel(Context context) {
29 super(context);
30 getHolder().addCallback(this);
31 _thread = new DrawThread(getHolder(),this);
32 setFocusable(true);
33
34
35 }
36
37
38 private void init()
39 {
40 bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.RGB_565);
41
42 bitmapCanvas = new Canvas();
43 bitmapCanvas.setBitmap(bitmap);
44 bitmapCanvas.drawColor(Color.BLACK);
45
46 isInitialized = true;
47
48 }
49
50
51
52
53
54 public boolean onTouchEvent (MotionEvent event){
55 // _x = (int) event.getX();
56 // _y = (int) event.getY();
57
58
59 bitmapCanvas.drawPoint(event.getX(), event.getY(), paint);
60
61 invalidate();
62
63
64
65 return true;
66
67 }
68
69 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height){
70
71 }
72
73 public void surfaceCreated (SurfaceHolder holder){
74 _thread.setRunning(true);
75 _thread.start();
76
77 }
78
79 public void surfaceDestroyed(SurfaceHolder holder){
80 boolean retry = true;
81 _thread.setRunning(false);
82
83 while(retry){
84 try{
85 _thread.join();
86 retry = false;
87 }catch(InterruptedException e){
88 }
89
90
91 }
92
93 }
94
95
96 @Override
97 public void onDraw(Canvas canvas) {
98
99 if (!isInitialized)
100 init();
101
102 canvas.drawBitmap(bitmap, 0, 0, paint);
103
104 /* Bitmap _scratch = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
105 canvas.drawColor(Color.BLACK);
106 canvas.drawBitmap(_scratch, _x-(_scratch.getWidth()/2), _y-(_scratch.getHeight()/2), null); */
107
108 }
109
110}

Antworten