新闻类app中文字上下轮播效果实现:借助handler和动画
package com.longyue.customtextview;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import android.app.Activity;
public class MainActivity extends Activity {
private TextView tv_change;
private boolean isloop = true;
private String [] ss = {"你好","我好","大家好","还有谁"};
private int item = 0;
private AnimationSet set = new AnimationSet(true);
private AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
private TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
0, Animation.RELATIVE_TO_SELF, 2.0f,
Animation.RELATIVE_TO_SELF, 0);
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case 1:
if(item<4){
if(isloop){
tv_change.setText(ss[item]);
tv_change.setAnimation(set);
tv_change.startAnimation(set);
item+=1;
}
}else{
item = 0;
tv_change.setText(ss[item]);
tv_change.setAnimation(set);
tv_change.startAnimation(set);
item+=1;
}
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
tv_change.setAnimation(set);
tv_change.startAnimation(set);
//实现自动切换界面
new Thread(new Runnable() {
@Override
public void run() {
while(isloop){
//系统时钟的睡眠方法---->电量的消耗很少。
SystemClock.sleep(2000);
handler.sendEmptyMessage(1);
}
}
}).start();
}
private void initView() {
tv_change = (TextView) findViewById(R.id.tv_change);
tv_change.setText(ss[0]);
set.addAnimation(animation);
set.addAnimation(ta);
set.setDuration(1000);
set.setRepeatMode(Animation.REVERSE);
}
}