I am working on a simple project and i wanted to know what or how is the best way to make your background image appear moving or move , i have this clouds that i want to move infinitely ? any help will be appreciated.
-
Are the clouds ImageViews? or part of the background image? – SuperFrog Oct 29 '15 at 13:59
-
@UdiIdan part of the imageview – Madona wambua Oct 29 '15 at 17:01
-
if its a part of `ImageView` then create a custom `Drawable` class and animate your clouds there – pskink Oct 29 '15 at 17:13
-
@pskink any help? i am stuck if you can offer any links i will appreciate. – Madona wambua Oct 29 '15 at 17:16
-
see this for example as a start: http://www.vogella.com/tutorials/AndroidDrawables/article.html#drawables_custom1 – pskink Oct 29 '15 at 17:19
1 Answers
A <FrameLayout ...>
(in the layout xml) draws its children one on top of another.
So
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
< the background view />
< the informative view />
</FrameLayout>
should do the trick. But please mind that a moving background can waste such resources as CPU load and battery.
UPDATE (moving: as any other view)
To make a view move, you can either use an animation or change the view position programmatically. I did the latter in response to a touch, you probably will choose the former. There is a lot about animations on SO. I would try to take the background image 2 times wider than the screen, set the left coordinate to a negative value, and animate changing this value to 0 (the image would move right, at least so it was in iOS).
Note 1. To position a view inside an either LinearLayout or RelativeLayout you can use auxiliary transparent views.
For example, the layout below splits the screen in 5 areas:
11111111111
222 333
44444444444
and places an image in the corner to the right of "2" and above "4":
<RelativeLayout
android:id="@+id/sight"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<View
android:id="@+id/sightq1"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_alignParentTop="true"
/>
<View
android:id="@+id/sightq4"
android:layout_width="fill_parent"
android:layout_height="80px"
android:layout_alignParentBottom="true"
/>
<View
android:id="@+id/sightq2"
android:layout_width="80px"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_above="@id/sightq4"
android:layout_below="@id/sightq1"
/>
<View
android:id="@+id/sightq3"
android:layout_width="80px"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_above="@id/sightq4"
android:layout_below="@id/sightq1"
/>
<!-- example: -->
<ImageView
android:id="@+id/..."
android:layout_toRightOf="@id/sightq2"
android:layout_alignTop="@id/sightq4"
android:src="@drawable/..."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Having said that, I have to note that probably you will not need auxiliary views, just positioning will do.
Note 2.
To get the actual view width/height of a view, you must use a ViewTreeObserver.OnGlobalLayoutListener
and call v.getMeasuredWidth() and v.getMeasuredHeight() from onGlobalLayout(). These width and height are known only after layout happens.
v.getViewTreeObserver().addOnGlobalLayoutListener(mLayoutListener);
to register a listener.

- 1
- 1

- 16,368
- 4
- 94
- 127
-
-
-
You want to move the informative view or the background one? The difference is that you likely can crop the background, but must keep information fully visible. Depending on this, you choose one or another method. See updates. – 18446744073709551615 Oct 30 '15 at 09:09
-