Objective
Android TextView can only set the entire TextView animation, but not the animation of each text. Even with Textswitcher, it's hard to achieve the results I want.
So choose Custom One. The general idea is: inherit ViewGroup, set text, each text for a textview, every other fixed time, start each textview animation.
Defines a ctextview that inherits ViewGroup:
To implement the main code:
public class Ctextview extends ViewGroup {
Provides an outward method setText(String text, final Animation animation, int duration) , text for the string to be displayed, animation for each character, and duration for the playback interval of the character animation.
This method is implemented as follows:
public void SetText (String text, final Animation Animation, int duration) {
int time = 0;
if (text!= null &&!text.isempty ()) {
char[] characters = Text.tochararray ();
for (char c:characters) {
final TextView t = new TextView (context);
Iterate through each character of the passed-in string, generate a TextView, and set its animated
T.settext (String.valueof (c));
T.settextsize (a);
Handler h = new Handler ();
Every duration time, play the next TextView animation
h.postdelayed (new Runnable () {
@Override public
void Run () {
AddView (t);
T.setanimation (animation);
}
}, time);
Time + = Duration;
}
}
Ctextview complete implementation is as follows:
Import Android.content.Context;
Import Android.os.Handler;
Import Android.util.AttributeSet;
Import Android.view.View;
Import Android.view.ViewGroup;
Import android.view.animation.Animation;
Import Android.widget.TextView;
/** * Created by Cchen on 2014/9/2.
* * public class Ctextview extends ViewGroup {private context;
Public Ctextview {Super (context);
This.context = context;
Public Ctextview (context, AttributeSet attrs) {Super (context, attrs);
This.context = context;
Public Ctextview (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
This.context = context;
} public void SetText (String text, final Animation Animation, int duration) {int time = 0;
if (text!= null &&!text.isempty ()) {char[] characters = Text.tochararray ();
for (char c:characters) {final TextView t = new TextView (context); Iterate through each character of the passed-in string, generate a TextView, and set its animated T.settext (String.valueof (c));
T.settextsize (28);
Handler h = new Handler (); Every duration time, play the next TextView animation h.postdelayed (new Runnable () {@Override public void run ()
{AddView (t);
T.setanimation (animation);
}, time);
Time + = Duration; }} @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int Measurewi
DTH = Measurewidth (WIDTHMEASURESPEC);
int measureheight = Measureheight (Heightmeasurespec);
Calculates the size of all child controls in a custom ViewGroup Measurechildren (Widthmeasurespec, Heightmeasurespec);
Sets the size of the custom control Myviewgroup setmeasureddimension (Measurewidth, measureheight);
@Override protected void OnLayout (Boolean changed, int l, int t, int r, int b) {int childleft = 0;
Iterate through all the child views int childcount = Getchildcount (); for (int i = 0; i < ChildCount;
i++) {View Childview = Getchildat (i);
Gets the view dimensions computed in onmeasure int measureheight = Childview.getmeasuredheight ();
int measuredwidth = Childview.getmeasuredwidth ();
Align them horizontally childview.layout (childleft, 0, Childleft + measuredwidth, measureheight);
Childleft + = Measuredwidth;
} private int Measurewidth (int pwidthmeasurespec) {int result = 0; int widthmode = Measurespec.getmode (PWIDTHMEASURESPEC);//Get mode int widthsize = measurespec.getsize (Pwidthmeasurespec)
//Get size switch (widthmode) {/** * mode has a total of three cases, the value is measurespec.unspecified, measurespec.exactly,
* Measurespec.at_most. * * * measurespec.exactly is the exact size, * when we specify the control's Layout_width or Layout_height as a specific value such as Andorid *: Lay
Out_width= "50dip", or fill_parent is, is the case that the control size has been determined, is the exact size. * * * Measurespec.at_most is the maximum size, * when the control of the Layout_width or layout_heWhen ight is specified as Wrap_content *, the size of the control is typically changed with the control's subspace or content, and the control's dimensions can be as long as the maximum size allowed by the parent control is not exceeded.
Therefore, the mode at this point is at_most,size gives the maximum size allowed by the parent control.
* * * * measurespec.unspecified is not specified size, this situation is not much, generally is the parent control is Adapterview, * through the measure method of the mode passed in.
* * Case MeasureSpec.AT_MOST:case MeasureSpec.EXACTLY:result = widthsize;
Break
return result;
private int measureheight (int pheightmeasurespec) {int result = 0;
int heightmode = Measurespec.getmode (Pheightmeasurespec);
int heightsize = measurespec.getsize (Pheightmeasurespec);
Switch (heightmode) {case MeasureSpec.AT_MOST:case MeasureSpec.EXACTLY:result = heightsize;
Break
return result; }
}
The custom component is then used in the layout file:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
xmlns:tools= "http:// Schemas.android.com/tools "
android:layout_width=" match_parent "
android:layout_height=" Match_parent "
android:orientation= "vertical"
tools:context= ". Networktestactivity ">
<com.network.cchen.network.ctextview
android:id=" @+id/ctextview "
Android:layout_width= "Match_parent"
android:layout_height= "match_parent" >
</ Com.network.cchen.network.ctextview>
In the activity, call the Ctextview setText method and pass in the relevant parameters:
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.view.animation.AnimationUtils;
public class Testactivity extends activity {
@Override public
void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (r.layout.activity_network_test);
Ctextview Ctextview = (ctextview) Findviewbyid (R.id.ctextview);
Ctextview.settext ("Hello World", animationutils.loadanimation (this, R.anim.myanim),)
The second parameter is animated, and I want the effect to be from transparent to opaque, Myanim.xml:
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<alpha
android:duration=" 1000 "
android:fromalpha=" 0.0 "
android:toalpha=" 1.0 "/>
</set>
If you want to make text fly one by one from the right:
<?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android=
"http://schemas.android.com/apk/res/" Android ">
<translate
android:duration=" 1000 "
android:fillafter=" true "
Android:fromxdelta = "50%p"
android:interpolator= "@android: Anim/anticipate_interpolator"
android:toxdelta= "0"/>
Summarize
The above is the use of Android TextView to achieve the full text of the entire content of the animation, the effect is still very good, interested in the small partners themselves hands-on. If you have questions, you can leave a message for discussion.