Ch6 Listings
satya - Friday, June 12, 2009 7:47:50 PM
Listing 6-1. XML Layout File for the Animation Test Harness
<?xml version="1.0" encoding="utf-8"?>
<!?filename: /res/layout/frame_animations_layout.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView android:id="@+id/textViewId1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Debug Scratch Pad"
/>
<Button
android:id="@+id/startFAButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Start Animation"
/>
<ImageView
android:id="@+id/animationImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
satya - Friday, June 12, 2009 7:49:44 PM
Listing 6-2. Activity to Load the ImageView
public class FrameAnimationActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_animations_layout);
}
}
satya - Friday, June 12, 2009 7:51:49 PM
Listing 6-3. XML File Defining the List of Frames to be Animated
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/colored-ball1" android:duration="50" />
<item android:drawable="@drawable/colored-ball2" android:duration="50" />
<item android:drawable="@drawable/colored-ball3" android:duration="50" />
<item android:drawable="@drawable/colored-ball4" android:duration="50" />
<item android:drawable="@drawable/colored-ball5" android:duration="50" />
<item android:drawable="@drawable/colored-ball6" android:duration="50" />
<item android:drawable="@drawable/colored-ball7" android:duration="50" />
<item android:drawable="@drawable/colored-ball8" android:duration="50" />
</animation-list>
satya - Friday, June 12, 2009 8:02:12 PM
Listing 6-4. Complete Code for the Frame-by-Frame Animation Test Harness
public class FrameAnimationActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.frame_animations_layout);
this.setupButton();
}
private void setupButton()
{
Button b = (Button)this.findViewById(R.id.startFAButtonId);
b.setOnClickListener(
new Button.OnClickListener()
{
public void onClick(View v)
{
parentButtonClicked(v);
}
});
}
private void parentButtonClicked(View v)
{
animate();
}
private void animate()
{
ImageView imgView = (ImageView)findViewById(R.id.imageView);
imgView.setVisibility(ImageView.VISIBLE);
imgView.setBackgroundResource(R.drawable.frame_animation);
AnimationDrawable frameAnimation =
(AnimationDrawable) imgView.getBackground();
if (frameAnimation.isRunning())
{
frameAnimation.stop();
}
else
{
frameAnimation.stop();
frameAnimation.start();
}
}
}//eof-class
satya - Friday, June 12, 2009 8:03:57 PM
Listing 6-5. A Scale Animation Defined in an XML File at /res/anim/scale.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration="500"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100" />
</set>
satya - Friday, June 12, 2009 8:05:14 PM
Listing 6-6. XML Layout File Defining the ListView
<?xml version="1.0" encoding="utf-8"?>
<!-- filename: /res/layout/list_layout.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list_view_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
satya - Friday, June 12, 2009 8:13:42 PM
Listing 6-7. Code for the Layout-Animation Activity
public class LayoutAnimationActivity extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.list_layout);
setupListView();
}
private void setupListView()
{
String[] listItems = 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);
}
}
satya - Friday, June 12, 2009 8:15:01 PM
Listing 6-8. Defining Scale Animation in an XML File
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<scale
android:fromXScale="1"
android:toXScale="1"
android:fromYScale="0.1"
android:toYScale="1.0"
android:duration="500"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100" />
</set>
satya - Friday, June 12, 2009 8:18:29 PM
Listing 6-9. Definition for a Layout-Controller XML File
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="30%"
android:animationOrder="reverse"
android:animation="@anim/scale" />
satya - Friday, June 12, 2009 8:20:06 PM
Listing 6-10. The Updated Code for the list_layout.xml File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="@+id/list_view_id"
android:persistentDrawingCache="animation|scrolling"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layoutAnimation="@anim/list_layout_controller" />
/>
</LinearLayout>
satya - Friday, June 12, 2009 8:21:21 PM
Listing 6-11. The alpha.xml File to Test Alpha Animation
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="1000" />
satya - Friday, June 12, 2009 8:23:09 PM
Listing 6-12. Combining Translate and Alpha Animations Through an Animation Set
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator">
<translate android:fromYDelta="-100%" android:toYDelta="0"
android:duration="500" />
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"
android:duration="500" />
</set>
satya - Friday, June 12, 2009 8:24:39 PM
Listing 6-13. Rotate Animation XML File
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:fromDegrees="0.0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="500" />
satya - Friday, June 12, 2009 8:26:47 PM
Listing 6-14. XML Layout File for the View-Animation Activity
<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/list_layout.xml -->
<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>
satya - Friday, June 12, 2009 8:29:20 PM
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[] listItems = 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();
}
});
}
}
satya - Friday, June 12, 2009 8:31:14 PM
Listing 6-16. Code for the ViewAnimation Class
public class ViewAnimation extends Animation
{
public ViewAnimation2(){}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
setDuration(2500);
setFillAfter(true);
setInterpolator(new LinearInterpolator());
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
final Matrix matrix = t.getMatrix();
matrix.setScale(interpolatedTime, interpolatedTime);
}
}
satya - Friday, June 12, 2009 8:36:06 PM
Listing 6-17. Code for the View-Animation Activity, Including 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[] listItems = 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();
}
});
}
private void animateListView()
{
ListView lv = (ListView)this.findViewById(R.id.list_view_id);
lv.startAnimation(new ViewAnimation());
}
}
satya - Friday, June 12, 2009 8:37:35 PM
Listing 6-18. An Implementation of the AnimationListener Interface
public class ViewAnimationListener
implements Animation.AnimationListener
{
private ViewAnimationListener(){}
public void onAnimationStart(Animation animation)
{
Log.d("Animation Example", "onAnimationStart");
}
public void onAnimationEnd(Animation animation)
{
Log.d("Animation Example", "onAnimationEnd");
}
public void onAnimationRepeat(Animation animation)
{
Log.d("Animation Example", "onAnimationRepeat");
}
}