Button in SurfaceView integrieren

AkechiKogoro

Mitglied
Hallo an alle im Java-Forum,

ich habe folgendes Problem:
Ich habe eine Klasse "GameView" geschrieben, die die Klasse SurfaceView erweitert. Bei der Erzeugung eines GameView-Objektes mit "private GameView gameView = new GameView(this);" wird ja mit "this" der gesamte Context der App und damit die ganze Oberfläche in die GameView einbezogen ( so viel ich weiß ).
Im Konstruktor dieser Klasse wird mit "SurfaceHolder myHolder = getHolder();" auf Basis der GameView ( erweiterte SurfaceView) ein SurfaceHolder erzeugt, mit dem ich die Spielfigur laufen lassen kann. So weit funktioniert das auch. Jedoch läuft die Figur nur los wenn man irgendwo auf den Bildschirm tippt.
Jetzt möchte ich aber ein ein Button ( als Anfang für ein Steuerkreuz ) einbeziehen und will damit erreichen dass die Spielfigur nur losläuft, wenn man diesen Button betätigt.
Was noch zu erwähnen ist: In der onCreate()-Methode steht bei mir "setContentView(gameView)"

Wie kann ich über die SurfaceView einen Button legen? Oder gibt es eine andere Möglichkeit dies zu realisieren?

Ich hoffe dass mir diesbezüglich jemand weiterhelfen kann.

Viele Grüße
euer Akechi Kogoro
 

mihe7

Top Contributor
Aus der Doku
https://developer.android.com/reference/android/view/SurfaceView hat gesagt.:
The surface is Z ordered so that it is behind the window holding its SurfaceView; the SurfaceView punches a hole in its window to allow its surface to be displayed. The view hierarchy will take care of correctly compositing with the Surface any siblings of the SurfaceView that would normally appear on top of it. This can be used to place overlays such as buttons on top of the Surface, though note however that it can have an impact on performance since a full alpha-blended composite will be performed each time the Surface changes.
Sprich: ein Layout erstellen, das neben der SurfaceView ein weiteres Layout mit den Buttons enthält. Beispiel hierzu unter https://stackoverflow.com/a/5779267
 

AkechiKogoro

Mitglied
Ich habe es nach dem Schema versucht wie du es vorgeschlagen hast @mihe7, jedoch leider ohne Erfolg. Hier ein Auszug aus dem was ich gemacht habe:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<ImageButton
android:id="@+id/iBtnNachRechts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@android:drawable/ic_media_play" />

<com.example.runningman2.MainActivity.GameView
android:id="@+id/gameView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/iBtnNachRechts"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>


Und im Activity selbst:

...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); -> Zeile 24
gameView = findViewById(R.id.gameView);
}

...

Daraufhin ist folgender Fehler aufgetreten:

E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.runningman2/com.example.runningman2.MainActivity}: android.view.InflateException: Binary XML file line #20: Error inflating class com.example.runningman2.MainActivity.GameView
...
at com.example.runningman2.MainActivity.onCreate(MainActivity.java:24)

( Ich habe keine Ahnung warum er die "activity_main", in der sich jetzt die GameView "gameView" befundet nicht mehr findet )
 

AkechiKogoro

Mitglied
"activity_main" ist das Layout und "gameView" ist ein GameView-Objekt ( eine Ableitung aus der Klasse SurfaceView ). Ich habe versucht die GameView "gameView" innerhalb ( mit XML ) des Layouts "activity_main" und dort im ConstraintLayout zu integrieren. Das hat leider aber nicht funktioniert.

Mir ist in der DesignAnsicht noch folgende Warnmeldung aufgefallen:
The following classes could not be found:
- com.example.runningman2.MainActivity.GameView (Fix Build Path, Edit XML, Create Class)


( In der XML-Ansicht wird nichts beanstandet, weswegen ich die Warnmeldung erst später entdeckt hatte )
Wenn es daran liegen sollte: Wie kann ich das korrigieren? ( Die Klasse existiert ja immerhin in der Activity ).
 
Zuletzt bearbeitet:

mihe7

Top Contributor
Das hier com.example.runningman2.MainActivity.GameView sieht aus, als hättest Du die Klasse GameView innerhalb der Klasse MainActivity definiert. Das funktioniert so nicht. Du musst die Klasse GameView in einer eigenen Datei definieren und den vollqualifizierten Namen der Klasse angeben. Alternative dazu findest Du in dem Link oben.
 

AkechiKogoro

Mitglied
Danke für den Tipp. Die Klasse wird jetzt erkannt.
Jedoch hat die App schon wieder 2 neue Probleme aus denen ich nicht schlau werde:
Render Problem
Custom view GameView is not using the 2- or 3-argument View constructors; XML attributes will not work
Tip: Try to refresh the layout.

Render Problem
Color spaces are not supported
Tip: Try to refresh the layout


Der refresh hat aber leider nichts geändert.
 

mihe7

Top Contributor
Das steht alles in dem Link, z. B. "There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file."

Gib Deiner Klasse alle Konstruktoren, die auch SurfaceView hat und rufe super(...) auf.
 

AkechiKogoro

Mitglied
Mein bisher schon verwendeter Konstruktor sieht folgendermaßen aus:
public GameView(Context context ) {
super(context);
ourHolder = getHolder();
bitmapRunningMan = BitmapFactory.decodeResource(getResources(), R.drawable.running_man);
bitmapRunningMan = Bitmap.createScaledBitmap(bitmapRunningMan, frameWidth * frameCount, frameHight, false);
}


Die anderen Konstrunktoren benötigen ebenfalls alle einen Context:
SurfaceView(Context context)
SurfaceView(Context context, AttributeSet attrs)
SurfaceView(Context context, AttributeSet attrs, int defStyleAttr)
SurfaceView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)

Wie kann ich nun ( ggf. damit ) realisieren dass sich die GameView nicht die ganze Oberfläche schnappt, damit ich darunter noch den Button packen kann?

Auch meckert der Compiler noch an folgender Zeile rum:
setContentView(R.layout.activity_main);
Das macht er aber erst seit ich die GameView wie folgt in die XML-Datei "activity_main" geschrieben habe ( Deswegen denke ich ist der Konstruktor nicht das Problem, denn der wird erst in der Zeile danach aufgerufen ).

<com.example.runningman2.GameView
android:id="@+id/gameView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="@+id/iBtnNachRechts"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />


Die gleichen Warnungen aus in meinem Beitrag von heute 15:58 Uhr sind leider noch immer darin sichtbar.
 

mihe7

Top Contributor
Die gleichen Warnungen aus in meinem Beitrag von heute 15:58 Uhr sind leider noch immer darin sichtbar.

Natürlich. In der Warnung steht doch ganz klar das Problem:
Custom view GameView is not using the 2- or 3-argument View constructors

Du brauchst also Konstruktoren wie

Java:
public GameView(Context context, AttributeSet attrs) {
    super(context, attrs);
    // ...
}
 

AkechiKogoro

Mitglied
Danke @mihe7,
dank deiner Hilfe hat es jetzt endlich geklappt. Ich hätte nicht gedacht dass es daran liegt dass ich nicht alle Konstruktoren aus SurfaceView umgesetzt hatte. Da habe ich wieder etwas gelernt.

Vielen Dank nochmal! :)
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
T Android Studio: Einen Button in einer For Schleife verwenden Android & Cross-Platform Mobile Apps 2
W App deinstallieren per Button & Andere App Öffnen per Button Android & Cross-Platform Mobile Apps 24
W Apk automatisch installieren per Button Android & Cross-Platform Mobile Apps 55
W aus Fragmente in andere Klasse wechseln mit Button Android & Cross-Platform Mobile Apps 3
T App updaten via Button Android & Cross-Platform Mobile Apps 10
AGW Android Teilen Button ändern Android & Cross-Platform Mobile Apps 14
J Android App - Browser öffnen und Text eingeben/Button click auslösen Android & Cross-Platform Mobile Apps 10
L Button zum Quadrad zwingen Android & Cross-Platform Mobile Apps 2
S Android Absoluter Neuling: EditText zur Laufzeit verändern bzw. über Button Android & Cross-Platform Mobile Apps 2
L Dialog anzeigen wenn auf Button gedrückt wird. Android & Cross-Platform Mobile Apps 4
S Android ListFragment & ArrayAdapter - Button-Werte werden vergessen Android & Cross-Platform Mobile Apps 0
B Android Abfragen wie lang ein Button gedrückt wurde Android & Cross-Platform Mobile Apps 2
J Android button mithilfe einer Methode automatisch erstellen Android & Cross-Platform Mobile Apps 6
A AlertDialog Enable Button Android & Cross-Platform Mobile Apps 1
J Button array ID Problem Android & Cross-Platform Mobile Apps 2
D Android Activity wechseln per Button Android & Cross-Platform Mobile Apps 3
B Android ringProgressDialog nach Erfolg Button einfärben Android & Cross-Platform Mobile Apps 2
A Android Menü Button oben links Android & Cross-Platform Mobile Apps 1
B Mit Button GeräteEinstellung des Handys öffnen Android & Cross-Platform Mobile Apps 3
M Android Android "Up-Button" extra definieren? Android & Cross-Platform Mobile Apps 1
K Problem mit arraylist und button Android & Cross-Platform Mobile Apps 16
T Button aktivieren per Qullcode... Android & Cross-Platform Mobile Apps 3
T Button geht net... Android & Cross-Platform Mobile Apps 2
D Man sieht nicht ob Button gedrückt wurde! Android & Cross-Platform Mobile Apps 10
K Grafik Tablerow, Button erstreckt sich in der gesamten Breite trotz Beschrenkung durch (max)width Android & Cross-Platform Mobile Apps 2
G Button ein Wert zuweisen Android & Cross-Platform Mobile Apps 5
M 20 Image Button für jede Auflösung positionieren Android & Cross-Platform Mobile Apps 3
B Android Button erstellen nach Vorlage Android & Cross-Platform Mobile Apps 4
L Android Button mit Pfeil nach rechts Android & Cross-Platform Mobile Apps 1
M Einzel Verarbeitung welcher Button angeklickt wurde? Android & Cross-Platform Mobile Apps 6
G Back-Button Methode überschreiben Android & Cross-Platform Mobile Apps 2
P Android Option Button Android & Cross-Platform Mobile Apps 4
G Check Button ist unchecked trotz setChecked(true) Android & Cross-Platform Mobile Apps 6
G Android Button mit Bild Android & Cross-Platform Mobile Apps 4
W Android App Programmierung - Button ganz transparent machen Android & Cross-Platform Mobile Apps 3
L Custom Dialog Button event Android & Cross-Platform Mobile Apps 2
J Button rechtsbündig Android & Cross-Platform Mobile Apps 5
A Android Button Array? Android & Cross-Platform Mobile Apps 6
P Android Button mit bild versehen Android & Cross-Platform Mobile Apps 4
J "Button" auf List erstellen Android & Cross-Platform Mobile Apps 4
U Fire button und Command.BACK, 1 kommen sich in die quere Android & Cross-Platform Mobile Apps 2
D Android Acceleometer in SurfaceVIew Android & Cross-Platform Mobile Apps 14
H Android SurfaceView Verständnis Android & Cross-Platform Mobile Apps 2
D Barcode Scanner ZXing integrieren Android & Cross-Platform Mobile Apps 15
M Eclipse: cdc.jar in eine Midlet Suite integrieren Android & Cross-Platform Mobile Apps 18

Ähnliche Java Themen

Neue Themen


Oben