OnClick Listener auf einem ImageView

  • Antworten:0
c da
  • Forum-Beiträge: 3

24.01.2012, 15:05:30 via Website

Hallo Forum,

ich bin neu hier und hoffe meine Frage in der richtigen Kategorie gestellt zu haben.
Ich habe Eine Klasse CameraView, welche View implementiert. Mittels der onDraw Methode zeichne ich alle Objekte auf dem Canvas. Innerhalb der OnDraw Methode rufe ich eine OnDraw Methode einer eigenen Klasse HUD auf. Diese malt ein kleines HUD für ein Spiel. Das Zeichnen an sich funktinoiert auch prima, allerdings funktionieren die ActionListener der ImageViews der Klasse HUD nicht. Ich habe keine Ahnung woran das liegen könnte. Hier mal die Codeschnippsel der entsprechenden Klassen:
1public class CameraView extends View implements SensorEventListener{
2.....
3.....
4
5@Override
6 protected void onDraw(Canvas canvas) {
7
8
9 mCanvas = canvas;
10
11 //update GameTimer and Items
12 hud.updateGameTimer();
13
14 //draw level based on camera coordinates
15 canvas.drawBitmap(background, camX, camY, null);
16
17 //Draw the HUD
18 hud.draw(canvas);
19
20 // and make sure to redraw asap
21 invalidate();
22 }
23
24
25}

1public class Hud{
2 public TextElement gameTimer;
3 CameraView mCameraView;
4 private HashMap<Integer, Item> mItems = new HashMap<Integer, Item>();
5 private Integer mScreenWidth;
6 private Integer mScreenHeight;
7 //private ImageView imgView;
8 private HashMap<Integer, ImageView> imgViews = new HashMap<Integer, ImageView>();
9 private LinearLayout ll;
10 private Integer n;
11
12 public Hud(CameraView cameraView){
13 mCameraView = cameraView;
14
15 mScreenWidth = mCameraView.screenWidth;
16 mScreenHeight = mCameraView.screenHeight;
17
18 //Get an instance of the GameTimer
19 gameTimer = new TextElement("GameTimer", mScreenWidth/2, 25);
20
21 //imgView = new ImageView(mCameraView.getContext());
22
23 //We use a layout to contain the buttons (or any view)
24 ll = new LinearLayout(mCameraView.getContext());
25 ll.setOrientation(LinearLayout.HORIZONTAL);
26 }
27
28 public void updateGameTimer() {
29
30 //update GameTimer
31 long millis = System.currentTimeMillis() - mCameraView.startTime ;
32 int seconds = (int) (millis / 1000);
33 int minutes = seconds / 60;
34 seconds = seconds % 60;
35 gameTimer.setText(String.format("%d: %02d", minutes, seconds));
36
37 }
38
39 public void updateItems() {
40 mItems = Simulation.items;
41
42 ll = new LinearLayout(mCameraView.getContext());
43
44 for (Item item : mItems.values()) {
45
46 Integer id = item.getIdentifier();
47 ImageView imgView = new ImageView(mCameraView.getContext());
48 imgView.setImageBitmap(item.getImage());
49 imgView.setId(id);
50
51 //We set the layout parameters
52 LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
53 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
54
55 //SET THE MARGIN HERE
56 layoutParams.setMargins(30 , mScreenHeight - item.getImage().getHeight() - 20, 0, 0);
57 ImageView.OnClickListener listener = new ImageView.OnClickListener()
58 {
59
60 public void onClick(View v)
61 {
62 Simulation.items.get(v.getId()).useItem();
63 Simulation.items.remove(v.getId());
64 imgViews.remove(v.getId());
65 updateItems();
66
67
68 }
69 };
70
71 imgView.setOnClickListener(listener);
72
73 //Add it to our linear layout
74 ll.addView(imgView, layoutParams);
75
76 //Measure and layout the linear layout before drawing it
77 ll.measure(MeasureSpec.getSize(ll.getMeasuredWidth()), MeasureSpec.getSize(ll.getMeasuredHeight()));
78 ll.layout(0, 0, MeasureSpec.getSize(imgView.getMeasuredWidth()), MeasureSpec.getSize(imgView.getMeasuredHeight()));
79
80 imgViews.put(item.getIdentifier(), imgView);
81
82 }
83
84 }
85
86 public void draw(Canvas canvas) {
87 //GameTimer
88 gameTimer.Draw(canvas);
89
90
91 //Finally draw the linear layout on the canvas
92
93 ll.draw(canvas);
94 if (imgViews.containsKey(1)) {
95
96 //imgViews.get(1).performClick();
97 }
98
99 }
100
101}

Wenn ich auf die angezeigten ItemBilder clicke passiert nichts. Lasse ich jedoch das Programm auf eines der Icons klicken, mittels imgViews.get(1).performClick(); wird zumindest mal die korrekte Aktion ausgeführt. Die Listener scheinen also zu gehen. Aber irgendwie "dringt" der Klick nicht zu dem ImageView durch :-/

Ich hoffe ihr könnte mir helfen!

Vielen Dank schonmal!
MfG, der CvD

Antworten