First, let's look at the effect:
/// Project Layout
//// Attrs. xml custom attribute, which controls the animation playback time
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="SlidingText">
<attr name="animationDuration" format="integer" />
</declare-styleable>
</resources>
//// Main. xml
<?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"
xmlns:slidingtext="http://schemas.android.com/apk/res/com.testSildingTextView"
android:layout_height="fill_parent">
<com.testSildingTextView.SlidingTextView
android:id="@+id/sliding_textview" android:layout_width="fill_parent"
android:layout_height="wrap_content"
slidingtext:animationDuration="500"
android:layout_gravity="center">
<TextView android:layout_width="fill_parent" android:gravity="center_horizontal"
android:layout_height="wrap_content" android:text="sssssss" />
</com.testSildingTextView.SlidingTextView>
</LinearLayout>
First, the custom name xmlns: slidingtext =Http://schemas.android.com/apk/res/com.testSildingTextView
The custom name is used here.
Slidingtext: animationDuration ="500"
The following is the key code of the animation effect, which describes its functions. This animation effect is mainly run every time a thread is opened. After the first run, wait 2 o'clock for 5 seconds, and then run textview to the left with the animation effect. After 0.5 seconds, it appears from the right, hide and loop after the animation ends
//// SlidingTextView
Private String [] showTexts = new String [] {"ssssss", "aaaaaa", "bbbbbb "};
// Used to record which string to display
Private int count = 0;
Private int mDuration;
Private TextView text;
Private int textWidth = 200;
// Get custom Variables
Public SlidingTextView (Context context, AttributeSet attrs ){
Super (context, attrs );
Typedarray A = context. obtainstyledattributes (attrs,
R. styleable. slidingtext );
Mduration =
. Getinteger (R. styleable. slidingtext_animationduration, 750 );
}
// Set the string to be displayed
Public void setshowtext (string [] showtexts ){
This. showtexts = showtexts;
}
// Call the callback function interface at the end of initialization
Protected void onfinishinflate (){
Super. onfinishinflate ();
TEXT = (textview) This. getchildat (0 );
Mhandler. postdelayed (appear, 1000 );
}
Private handler mhandler = new handler (){
@ Override
Public void handlemessage (Message MSG ){
// 1 is displayed, 2 is hidden
Switch (msg. arg1 ){
Case 1:
DoAnimationOpen ();
Break;
Case 2:
DoAnimationClose ();
Break;
}
}
};
Public void doAnimationOpen (){
Post (appear );
}
// Effect
Runnable appear = new Runnable (){
Public void run (){
TranslateAnimation animation;
Int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0;
Int calculatedDuration = 0;
FromXDelta = textWidth;
ToXDelta = 0;
CalculatedDuration = mDuration * Math. abs (toXDelta-fromXDelta)
/TextWidth;
Animation = new TranslateAnimation (fromXDelta, toXDelta,
Fromydelta, toydelta );
Animation. setduration (calculatedduration );
Animation. setanimationlistener (New animationlistener (){
@ Override
Public void onanimationstart (animation ){
If (showtexts. length! = 0 ){
Count = (count + 1) % showtexts. length;
Text. settext (showtexts [count]);
}
Text. setvisibility (visible );
}
@ Override
Public void onanimationrepeat (animation ){
}
@ Override
Public void onanimationend (animation ){
Mhandler. postdelayed (hide, 2500 );
}
});
StartAnimation (animation );
}
};
Public void doAnimationClose (){
Post (hide );
}
// Hide the effect
Runnable hide = new Runnable (){
Public void run (){
TranslateAnimation animation;
Int fromXDelta = 0, toXDelta = 0, fromYDelta = 0, toYDelta = 0;
Int calculatedDuration = 0;
ToXDelta =-1 * textWidth;
CalculatedDuration = mDuration * Math. abs (toXDelta-fromXDelta)
/TextWidth;
Animation = new TranslateAnimation (fromXDelta, toXDelta,
FromYDelta, toYDelta );
Animation. setDuration (calculatedDuration );
Animation. setAnimationListener (new AnimationListener (){
@ Override
Public void onAnimationStart (Animation animation ){
}
@ Override
Public void onAnimationRepeat (Animation animation ){
}
// Sets the textview status when the animation ends.
@ Override
Public void onAnimationEnd (Animation animation ){
MHandler. postDelayed (appear, 500 );
Text. setVisibility (INVISIBLE );
}
});
StartAnimation (animation );
}
};
This article is original. If you need to reprint it, please indicate the author and the source. Thank you!
Code download
Http://files.cnblogs.com/not-code/testSildingTextView.zip