Animating All Views in a Layout

In addition to applying animations to individual View controls, you can also apply them to each child View control within a Layout (such as TableLayout and each TableRow), using LayoutAnimationController.

To animate View controls in this fashion, you must load the animation, create LayoutAnimationController, configure it as necessary, and then call the layout's setLayoutAnimation() method. For example, the following code loads the cus-tom_anim animation, creates a LayoutAnimationController, and then applies it to each TableRow in the TableLayout control:

Animation spinin = AnimationUtils.loacMnimation(this, R.anim.custom_anim); LayoutAnimationController controller =

new LayoutAnimationController(spinin); TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); for (int i = 0; i < table.getChildCount(); i++) { TableRow row = (TableRow) table.getChildAt(i); row.setLayoutAnimation(controller);

There is no need to call any startAnimation() method in this case because LayoutAnimationController handles that for you. Using this method, the animation is applied to each child view, but each starts at a different time. (The default is 50% of the duration of the animation—which, in this case, would be 1 second.) This gives you the nice effect of each ImageView spinning into existence in a cascading fashion.

Stopping LayoutAnimationController animations is no different from stopping individual animations: You simply use the clearAnimation() method. The additional lines to do this in the existing onPause() method are shown here:

TableLayout table = (TableLayout) findViewById(R.id.TableLayout01); for (int i = 0; i < table.getChildCount(); i++) { TableRow row = (TableRow) table.getChildAt(i); row.clearAnimation();

0 0

Post a comment