Ch6 Listings


<?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>

public class FrameAnimationActivity extends Activity
{
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.frame_animations_layout);
   }
}

<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>

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

<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>

<?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>

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);
   }
}

<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>

<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
   android:delay="30%"
   android:animationOrder="reverse"
   android:animation="@anim/scale" />

<?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>

<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" />

<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>

<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" />

<?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>

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();
            }
         });
   }
}

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);
   }
}

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());
   }
}

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");
   }
}