Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

Chapter 32  Property Animation


When you display this oval in a square view, you will get a circle. People will nod their heads in
approval, and then think about the real sun up in the sky.


Next, build the entire scene out in a layout file. This layout will be used in SunsetFragment, which you
will build in a moment, so name it fragment_sunset.xml.


Listing 32.3  Setting up the layout (res/layout/fragment_sunset.xml)


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/sky"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.61"
android:background="@color/blue_sky">
<ImageView
android:id="@+id/sun"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
android:src="@drawable/sun" />



<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.39"
android:background="@color/sea" />



Check out the preview. You should see a daytime scene of the sun in a blue sky over a dark blue sea.
You may find yourself thinking about a trip you once took to the beach or aboard a boat.


Time to finally get this thing up and running on a device. Create a fragment called SunsetFragment
and add a newInstance() method. In onCreateView(...), inflate the fragment_sunset layout file and
return the resulting view.


Listing 32.4  Creating SunsetFragment (SunsetFragment.java)


public class SunsetFragment extends Fragment {


public static SunsetFragment newInstance() {
return new SunsetFragment();
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_sunset, container, false);


return view;
}
}

Free download pdf