OnClick Listener auf einem ImageView

C

cda

Neues Mitglied
0
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 Draw 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:

Code:
[COLOR=darkred]public[/COLOR] [COLOR=darkred]class[/COLOR] CameraView [COLOR=darkred]extends[/COLOR] View [COLOR=darkred]implements[/COLOR] SensorEventListener{ ..... .....  @Override     [COLOR=darkred]protected[/COLOR] [COLOR=darkred]void[/COLOR] onDraw(Canvas canvas) {                   mCanvas = canvas;              [COLOR=green]//update GameTimer and Items[/COLOR]         hud.updateGameTimer();          [COLOR=green]//draw level based on camera coordinates[/COLOR]         canvas.drawBitmap(background, camX, camY, [COLOR=darkred]null[/COLOR]);          [COLOR=green]//Draw the HUD[/COLOR]         hud.draw(canvas);            [COLOR=green]// and make sure to redraw asap[/COLOR]         invalidate();     } }
Code:
[COLOR=darkred]public[/COLOR] [COLOR=darkred]class[/COLOR] Hud{     [COLOR=darkred]public[/COLOR] TextElement gameTimer;     CameraView mCameraView;     [COLOR=darkred]private[/COLOR] HashMap<[COLOR=darkviolet]Integer[/COLOR], Item> mItems = [COLOR=darkred]new[/COLOR] HashMap<[COLOR=darkviolet]Integer[/COLOR], Item>();     [COLOR=darkred]private[/COLOR] [COLOR=darkviolet]Integer[/COLOR] mScreenWidth;     [COLOR=darkred]private[/COLOR] [COLOR=darkviolet]Integer[/COLOR] mScreenHeight;     [COLOR=green]//private ImageView imgView;[/COLOR]     [COLOR=darkred]private[/COLOR] HashMap<[COLOR=darkviolet]Integer[/COLOR], ImageView> imgViews = [COLOR=darkred]new[/COLOR] HashMap<[COLOR=darkviolet]Integer[/COLOR], ImageView>();     [COLOR=darkred]private[/COLOR] LinearLayout ll;     [COLOR=darkred]private[/COLOR] [COLOR=darkviolet]Integer[/COLOR] n;      [COLOR=darkred]public[/COLOR] Hud(CameraView cameraView){         mCameraView = cameraView;          mScreenWidth = mCameraView.screenWidth;         mScreenHeight = mCameraView.screenHeight;          [COLOR=green]//Get an instance of the GameTimer[/COLOR]         gameTimer = [COLOR=darkred]new[/COLOR] TextElement([COLOR=red]"GameTimer"[/COLOR], mScreenWidth/2, [COLOR=darkorange]25[/COLOR]);          [COLOR=green]//imgView = new ImageView(mCameraView.getContext());[/COLOR]          [COLOR=green]//We use a layout to contain the buttons (or any view)[/COLOR]         ll = [COLOR=darkred]new[/COLOR] LinearLayout(mCameraView.getContext());         ll.setOrientation(LinearLayout.HORIZONTAL);     }      [COLOR=darkred]public[/COLOR] [COLOR=darkred]void[/COLOR] updateGameTimer() {          [COLOR=green]//update GameTimer[/COLOR]         [COLOR=darkred]long[/COLOR] millis = [COLOR=darkviolet]System[/COLOR].currentTimeMillis() - mCameraView.startTime  ;         [COLOR=darkred]int[/COLOR] seconds = ([COLOR=darkred]int[/COLOR]) (millis / [COLOR=darkorange]1000[/COLOR]);         [COLOR=darkred]int[/COLOR] minutes = seconds / [COLOR=darkorange]60[/COLOR];         seconds     = seconds % [COLOR=darkorange]60[/COLOR];         gameTimer.setText([COLOR=darkviolet]String[/COLOR].format([COLOR=red]"%d: %02d"[/COLOR], minutes, seconds));      }      [COLOR=darkred]public[/COLOR] [COLOR=darkred]void[/COLOR] updateItems() {         mItems = Simulation.items;          ll = [COLOR=darkred]new[/COLOR] LinearLayout(mCameraView.getContext());                  [COLOR=darkred]for[/COLOR] (Item item : mItems.values()) {                          [COLOR=darkviolet]Integer[/COLOR] id = item.getIdentifier();             ImageView imgView = [COLOR=darkred]new[/COLOR] ImageView(mCameraView.getContext());             imgView.setImageBitmap(item.getImage());             imgView.setId(id);              [COLOR=green]//We set the layout parameters[/COLOR]             LinearLayout.LayoutParams layoutParams = [COLOR=darkred]new[/COLOR] LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);              [COLOR=green]//SET THE MARGIN HERE[/COLOR]             layoutParams.setMargins([COLOR=darkorange]30[/COLOR] , mScreenHeight - item.getImage().getHeight() - [COLOR=darkorange]20[/COLOR], [COLOR=darkorange]0[/COLOR], [COLOR=darkorange]0[/COLOR]);             ImageView.OnClickListener listener = [COLOR=darkred]new[/COLOR] ImageView.OnClickListener()              {                  [COLOR=darkred]public[/COLOR] [COLOR=darkred]void[/COLOR] onClick(View v)                 {                     Simulation.items.get(v.getId()).useItem();                     Simulation.items.remove(v.getId());                     imgViews.remove(v.getId());                     updateItems();                                       }             };                                  imgView.setOnClickListener(listener);                          [COLOR=green]//Add it to our linear layout[/COLOR]             ll.addView(imgView, layoutParams);              [COLOR=green]//Measure and layout the linear layout before drawing it[/COLOR]             ll.measure(MeasureSpec.getSize(ll.getMeasuredWidth()), MeasureSpec.getSize(ll.getMeasuredHeight()));             ll.layout([COLOR=darkorange]0[/COLOR], [COLOR=darkorange]0[/COLOR], MeasureSpec.getSize(imgView.getMeasuredWidth()), MeasureSpec.getSize(imgView.getMeasuredHeight()));              imgViews.put(item.getIdentifier(), imgView);                      }      }      [COLOR=darkred]public[/COLOR] [COLOR=darkred]void[/COLOR] draw(Canvas canvas) {         [COLOR=green]//GameTimer[/COLOR]         gameTimer.Draw(canvas);                           [COLOR=green]//Finally draw the linear layout on the canvas  [/COLOR]          ll.draw(canvas);         [COLOR=darkred]if[/COLOR] (imgViews.containsKey([COLOR=darkorange]1[/COLOR])) {                          [COLOR=green]//imgViews.get(1).performClick();[/COLOR]         }              }  }
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

Edit: Wie kann ich denn den Code hier im Forum gescheit darstellen?
 
Nun ist der Sourcecode als TXT im Anhang
 

Anhänge

  • CameraView.txt
    430 Bytes · Aufrufe: 117
  • HUD.txt
    2,8 KB · Aufrufe: 98

Ähnliche Themen

M
  • MikelKatzengreis
Antworten
5
Aufrufe
136
swa00
swa00
Zurück
Oben Unten