To animate a particular label in android we have to use TextSwitcher on screen. TextSwitcher layout is similar as the other layouts in android, like Textview and Edittext. TextSwitcher is used to swap current text with the new one. We can also perform some other operations like Gravity, Textsize, textcolor, typeface, and ShadowLayer.
Some examples of this would be:-
- Navigating through a list of dates with Left and Right buttons
- Changing numbers in a date picker
- Countdown timer clock
- Smooth transition for a news headlines slider
<TextSwitcher
android:id="@+id/textSwitcherDay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"/>
textSwitcherDay.setFactory(new ViewSwitcher.ViewFactory() {
public View makeView() {
// TODO Auto-generated method stub
// create new textView and set the properties like clolr, size etc
TextView myText = new TextView(MainActivity.this);
myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
myText.setTextSize(16);
myText.setTextColor(Color.BLUE);
myText.setTypeface(Typeface.DEFAULT_BOLD);
myText.setShadowLayer(10, 10, 10, Color.BLACK);
return myText;
}
});
// Declare the in and out animations and initialize them
Animation in = AnimationUtils.loadAnimation(this, R.anim.push_up_in);
Animation out = AnimationUtils.loadAnimation(this,R.anim.push_up_out);
// set the animation type of textSwitcher
textSwitcherDay.setInAnimation(in);
textSwitcherDay.setOutAnimation(out);
TextSwitcher animates the current text out(push_up_out.xml) and animates the new text in(push_up_in.xml).
push_up_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="100%p"
android:toYDelta="0" />
<alpha
android:duration="300"
android:fromAlpha="0.0"
android:toAlpha="1.0" />
</set>
push_up_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="300"
android:fromYDelta="0"
android:toYDelta="-100%p" />
<alpha
android:duration="300"
android:fromAlpha="1.0"
android:toAlpha="0.0" />
</set>
0 Comment(s)