Info

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent"

android:layout_height="fill_parent" >

<Button android:id="@+id/btn_animate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Start Animation"

<ListView android:id="@+id/list_view_id"

android:persistentDrawingCache="animation|scrolling"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

</LinearLayout>

Notice that the file location and the file name are embedded at the top of the XML file for your reference. This layout has two parts: the first is the button named btn_animate to animate a view, and the second is the ListView, which is named list_view_id.

Now that you have the layout for the activity, you can create the activity to show the view and set up the Start Animation button (see Listing 6-15).

Listing 6-15. Code for the View-Animation Activity, Before Animation public class ViewAnimationActivity extends Activity {

^Override public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); setContentView(R.layout.list_layout); setupListView(); this.setupButton();

private void setupListView() {

String[] listltems = new String[] { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6",

ArrayAdapter listItemAdapter = new ArrayAdapter(this

,android.R.layout.simple_list_item_1 ,listItems);

ListView lv = (ListView)this.findViewById(R.id.list_view_id); lv.setAdapter(listItemAdapter);

private void setupButton() {

Button b = (Button)this.findViewById(R.id.btn_animate); b.setOnClickListener(

new Button.OnClickListener(){

public void onClick(View v) {

//animateListView();

The code for the view-animation activity in Listing 6-15 closely resembles the code for the layout-animation activity in Listing 6-7. We have similarly loaded the view and set up the ListView to contain six text items. We've set up the button in such a way that it would call animateListView() when clicked. But for now, comment out that part until you get this basic example running.

You can invoke this activity as soon as you register it in the AndroidManifest.xml file:

<activity android:name=".ViewAnimationActivity"

android:label="View Animation Test Activity">

Once this registration is in place, you can invoke this view-animation activity from any menu item in your application by executing the following code:

Intent intent = new Intent(this, ViewAnimationActivity.class); startActivity(intent);

When you run this program, you will see the UI as laid out in Figure 6-5.

0 0

Post a comment