- package com.jxs.app.gridview.utils;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.TextView;
- /**
- *
- * @Author Jiangxs
- * @Date 2011-7-21 上午12:06:24
- * @Des 本类为跑马灯工具类 只要提供给一个要显示的字符串,一个TextView,就可以在这个TextView中实现跑马灯。
- */
- public class Marquee {
- private final int TEXT_OUT = 4;
- private final int TEXT_IN = 5;
- private String titleStr;// 跑马灯字符串
- private int place = 1;// replace count
- private TextView titleTextView;
- public Marquee(String titleStr, TextView titleTextView) {
- this.titleStr = titleStr;
- this.titleTextView = titleTextView;
- }
- private Handler myHandler = new Handler() {
- @Override
- public void handleMessage(Message msg) {// 重写的方法用于接收Handler消息
- super.handleMessage(msg);
- if (msg.what == TEXT_OUT) {// 跑马灯向外出来时
- titleTextView.setText(titleStr.substring(0, place));
- } else if (msg.what == TEXT_IN) {// 跑马灯向里进去时
- titleTextView.setText(titleStr.substring(place, titleStr
- .length()));
- }
- }
- };
- public void menuThread() {
- new Thread() {// 该线程用于标题栏跑马灯的实现
- public void run() {
- boolean control = true;
- while (true) {
- if (control) {// 出来时
- myHandler.sendEmptyMessage(TEXT_OUT);
- try {
- Thread.sleep(300);// 睡觉300毫秒
- } catch (Exception e) {// 捕获异常
- e.printStackTrace();// 打印异常
- }
- if (place >= titleStr.length()) {
- place = 1;
- control = false;
- } else {
- place++;
- }
- } else {// 进去
- myHandler.sendEmptyMessage(TEXT_IN);// 发送Handler消息
- try {
- Thread.sleep(200);// 睡觉300毫秒
- } catch (Exception e) {// 捕获异常
- e.printStackTrace();// 打印异常
- }
- if (place >= titleStr.length()) {
- place = 1;
- control = true;
- } else {
- place++;// 将place加一
- }
- }
- }
- }
- }.start();
- }
- }
Android 的跑马灯工具类
最新推荐文章于 2025-08-07 22:32:50 发布