In this example, we use TranslateAnimation to show down the Animation effect of a list item (Cascade) of a ListView. For details about the basic Animation, see Android ApiDemos example resolution (3 ): app-> Activity-> Animation.
In this example, the code is used to add LayoutAnimation to the ListView.
[Java]
AnimationSet set = new AnimationSet (true );
Animation animation = new AlphaAnimation (0.0f, 1.0f );
Animation. setDuration (50 );
Set. addAnimation (animation );
Animation = new TranslateAnimation (
Animation. RELATIVE_TO_SELF, 0.0f,
Animation. RELATIVE_TO_SELF, 0.0f,
Animation. RELATIVE_TO_SELF,-1.0f,
Animation. RELATIVE_TO_SELF, 0.0f
);
Animation. setDuration (2000 );
Set. addAnimation (animation );
LayoutAnimationController controller
= New LayoutAnimationController (set, 0.5f );
ListView listView = getListView ();
ListView. setLayoutAnimation (controller );
AnimationSet set = new AnimationSet (true );
Animation animation = new AlphaAnimation (0.0f, 1.0f );
Animation. setDuration (50 );
Set. addAnimation (animation );
Animation = new TranslateAnimation (
Animation. RELATIVE_TO_SELF, 0.0f,
Animation. RELATIVE_TO_SELF, 0.0f,
Animation. RELATIVE_TO_SELF,-1.0f,
Animation. RELATIVE_TO_SELF, 0.0f
);
Animation. setDuration (2000 );
Set. addAnimation (animation );
LayoutAnimationController controller
= New LayoutAnimationController (set, 0.5f );
ListView listView = getListView ();
ListView. setLayoutAnimation (controller );
In this example, the Cascade effect is implemented through TranslateAnimation. The constructor used in this example is as follows:
Public TranslateAnimation (int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)
FromXType: Specifies the fromXValue mode. In this example, RELATIVE_TO_SELF is used to indicate the width or height of the View. RELATIVE_TO_PARENT is the width or height of the parent View. ABSOLUTE indicates that fromXValue uses ABSOLUTE values.
FromXValue: The coefficient relative to the current View or parent View when fromXType is RELATIVE_TO_PARENT or RELATIVE_TO_SELF. In this example, expand downward and there is no animation in the X direction, so it is set to 0.
ToXType: Specifies the toXValue mode. In this example, RELATIVE_TO_SELF is used to indicate the width or height of the View. RELATIVE_TO_PARENT is the width or height of the parent View. ABSOLUTE indicates that fromXValue uses ABSOLUTE values.
ToXValue: The coefficient relative to the current View or parent View when fromXType is RELATIVE_TO_PARENT or RELATIVE_TO_SELF. In this example, expand downward and there is no animation in the X direction, so it is set to 0.
FromYType: Specifies the fromYValue mode. In this example, RELATIVE_TO_SELF is used to indicate the width or height of the View. RELATIVE_TO_PARENT is the width or height of the parent View. ABSOLUTE indicates that fromYValue uses ABSOLUTE values.
FromYValue: The coefficient relative to the current View or parent View when fromYType is RELATIVE_TO_PARENT or RELATIVE_TO_SELF.
ToYType: indicates the toYValue mode. In this example, RELATIVE_TO_SELF is used to indicate the width or height of the View. RELATIVE_TO_PARENT is the width or height of the parent View. ABSOLUTE indicates that fromXValue uses ABSOLUTE values.
ToYValue: The coefficient relative to the current View or parent View when fromYType is RELATIVE_TO_PARENT or RELATIVE_TO_SELF. In this example, expand down. A value of 1 indicates moving down the height of the sub-View.
Changing the animation time from 100 to 2000 can display the animation effect at a slower speed.