AutoText.java
package com.example.kadh.test;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.TextSwitcher;
import android.widget.TextView;
public class AutoText extends TextSwitcher {
String[] mStr = new String[]{"111111111111", "222222222222222222"};
private int mType = Animation.RELATIVE_TO_SELF;
private TranslateAnimation mInDown;
private TranslateAnimation mInUp;
private TranslateAnimation mOutUp;
private TranslateAnimation mOutDown;
public AutoText(Context context) {
super(context);
}
public AutoText(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context) {
for (int i = 0; i < mStr.length; i++) {
TextView tv = new TextView(context);
tv.setBackgroundResource(R.drawable.segment_normal_2_bg);
tv.setGravity(View.TEXT_ALIGNMENT_CENTER);
tv.setTextSize(26);
tv.setText(mStr[i]);
addView(tv);
}
mInUp = creatAnim(mType, 0,0 , 1, 0);
mOutUp = creatAnim(mType, 0, 0, 0, -1);
mInDown = creatAnim(mType, 0 , 0, -1 , 0);
mOutDown = creatAnim(mType, 0, 0, 0, 1);
setInAnimation(mInUp);
setOutAnimation(mOutUp);
}
private TranslateAnimation creatAnim(int Type, float fromXValue, float toXValue, float fromYValue, float toYValue) {
TranslateAnimation anim = new TranslateAnimation(Type, fromXValue, Type, toXValue, Type, fromYValue, Type, toYValue);
anim.setDuration(1000);
anim.setFillAfter(true);
return anim;
}
public void upAnim() {
setInAnimation(mInUp);
setOutAnimation(mOutUp);
}
public void downAnim() {
setInAnimation(mInDown);
setOutAnimation(mOutDown);
}
}
MainActivity
package com.example.kadh.test;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
int count = 0;
private AutoText mAouttext;
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (count%2==1){
mAouttext.showNext();
mAouttext.downAnim();
}else {
mAouttext.showPrevious();
mAouttext.upAnim();
}
count++;
mHandler.sendEmptyMessageDelayed(0, 1000);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAouttext = (AutoText) findViewById(R.id.aouttext);
mHandler.sendEmptyMessageDelayed(0, 1000);
}