0

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.

Madona wambua
  • 1,408
  • 1
  • 21
  • 37

1 Answers1

0

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.

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127