0

I code a game like battleship. When a player has put a ship on a playfield by dropping an imageView (a ship) I would like to be able to mark this ship being hit with putting a imageView indicating that later on.

This is my layout so far (setup to configure the ships):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:weightSum="1"
                android:background="@drawable/background_blue"
                android:layout_gravity="top|center_horizontal" >
        <ImageView
                android:id="@+id/imageSetzeSchiffe"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal|top"
                android:src="@drawable/image_schiffe_setzen"
                android:contentDescription="@string/startscreen"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true">
        </ImageView>
        <ImageView
                android:id="@+id/imagePlayfield"
                android:layout_width="300dp"
                android:layout_height="280dp"
                android:src="@drawable/image_playfield"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="left|bottom"
                android:layout_below="@+id/imageSetzeSchiffe"
                android:layout_alignParentLeft="true"/>
        <ImageButton
                android:id="@+id/buttonDrehen"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_drehen"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignBottom="@+id/imagePlayfield"
                android:layout_toRightOf="@+id/imagePlayfield"
                android:layout_marginLeft="24dp"/>

        <ImageButton
                android:id="@+id/buttonPlay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/button_play"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignBottom="@+id/buttonTurn"
                android:layout_toRightOf="@+id/imageShipCarrier"/>
        <ImageView
                android:id="@+id/imageShipCarrier"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_carrier"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_centerVertical="true"
                android:layout_toRightOf="@+id/buttonDrehen"/>
        <ImageView
                android:id="@+id/imageShipCruiser"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_cruiser"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignTop="@+id/imagePlayfield"
                android:layout_toLeftOf="@+id/imageShipGunboat"
                android:layout_marginRight="31dp"
                android:layout_marginTop="19dp"/>
        <ImageView
                android:id="@+id/imageShipGunboat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_gunboat"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_alignTop="@+id/imageShipCruiser" android:layout_alignRight="@+id/buttonSpielen"
                android:layout_alignEnd="@+id/buttonSpielen"/>
        <ImageView
                android:id="@+id/imageShipSubmarine"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_submarine"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_above="@+id/buttonSpielen" android:layout_alignRight="@+id/buttonSpielen"
                android:layout_alignEnd="@+id/buttonSpielen"/>
        <ImageView
                android:id="@+id/imageShipSpeedboat"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/image_ship_speedboat"
                android:contentDescription="@string/startscreen"
                android:layout_gravity="center"
                android:layout_below="@+id/imageShipCruiser" android:layout_alignLeft="@+id/imageShipCruiser"
                android:layout_alignStart="@+id/imageShipCruiser"/>
</RelativeLayout>

For drag and drop, I use an touchListener (for the ships being clicked) and dragListener for the drop area (here: imagePlayfield).

How can I load another imageView (not within the xml/layout) later on and put it over a ship being set?

Pille
  • 1,123
  • 4
  • 16
  • 38
  • http://stackoverflow.com/questions/2739971/overlay-two-images-in-android-to-set-an-imageview – The Hungry Androider Jul 21 '15 at 18:27
  • The imageView has been positionated by an user and I try to figure out where it has been dropped. So does your stated solution/link reflect does the imageView where another imageView should be put over has already been instantiated? – Pille Jul 21 '15 at 18:45
  • @TheHungryAndroider Thank you! How can I set those drawable multiple times over the same imageView? As a ship can take several hits before it sinks there is a need that the imageView should take several images as layers. How can I set the coordinates of the layers? – Pille Jul 21 '15 at 19:06
  • You can keep creating LayerDrawables on top of each other – The Hungry Androider Jul 21 '15 at 19:08
  • How can I change the size of the image being put above the other image or influence the exact position? As I combined two images the second was streched over the other loosing its original size. – Pille Jul 21 '15 at 21:13

1 Answers1

1

Use Frame layout with the images stacked on each other, move the elements up or down in the stack, so that it becomes visible or hide alternatively.

Y2K
  • 394
  • 3
  • 15
  • Is it possible to stack an ImageView (image of hit) on another ImageView (ship) when the ship gets dragged? – Pille Jul 21 '15 at 21:07
  • You might trigger the above mentioned layout change onDragged. I haven't tried it , but conceptually it may work. – Y2K Jul 22 '15 at 06:02
  • I did trigger the layout change after oprimising the coordinates and before setting the imageview. I have not been precisely in what i meant in my last comment, sorry. The layout change should occur if a special event occurs (when the ship got a hit by comparision of coordinates). – Pille Jul 22 '15 at 06:39
  • Yes yes on your special event . Firstly try Replace all your Image View with Frame layout , Then Put your Two Images ie: Ship and Destruction Image in 2 Image views. Frame layout would stack them automatically. – Y2K Jul 22 '15 at 06:52
  • Why do I have use FrameLayout? I tried your suggestion and the hit image is being strechted over the ships image fully. It need be that there are several hit images on the ship as it will be destroyed after all hit (that is: 2 for a small one and five of the biggest ship). – Pille Jul 22 '15 at 07:16
  • If It doesn't get Stretched, Will it work ? you can show/hide Image views , imageScale ;FitXY ,centercrop , Change the ordering : bringtofront(), Set a ForegroundDrawable to Frame layout , etc. If this doesn't work Then You should try other solutions – Y2K Jul 22 '15 at 07:46