ImageView in fixed position
In mine RelativeLayout
, I have a large ocean view background image ( 1870px
x 756px
in drawable-xxhdpi
) that I am the hub for all devices:
<ImageView
android:src="@drawable/bg_image"
android:scaleType = "centerCrop"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</ImageView>
In addition to this, I want to place another ship image ( 500px
x 300px
also at drawable-xxhdpi
), centered horizontally, but must be 230px
away from the top of the screen to be on the horizon.
<ImageView
android:src="@drawable/another_image"
android:layout_centerHorizontal="true"
android:layout_marginTop="230px"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ImageView>
I am not getting correct results and android is complaining about using px
as unit for top margin.
Results: Device 1 (configured on the horizon)
Smaller device 2 (not tuned on horizon):
Any suggestions?
source to share
Make sure the horizon in the background image is centered vertically. Then you can use this simple trick RelativeLayout
to position your ship image above the vertical center of the view
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:src="@drawable/bg_image"
android:scaleType = "centerCrop"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ImageView>
<View
android:id="@+id/center"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<ImageView
android:src="@drawable/another_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="@+id/center">
</ImageView>
</RelativeLayout>
source to share