android怎样写一个循环文字滚动的TextView

效果图:


在layout中这样来声明:

	<com.kaixin001.view.ScrollText android:id="@+id/news_statustxt" 
		    android:layout_width="wrap_content"
			android:layout_height="wrap_content" 
			android:paddingLeft="10dp"
			android:paddingRight="10dp"
			android:layout_gravity="center_vertical"
			android:textColor="#000000"
			android:textSize="16sp"
			android:hint="@string/news_state_hint"
			android:inputType="text"/>

activity这样来调用:

   private void initStatus(){
    	ivState = (ImageView) findViewById(R.id.news_statusinput);
    	//必须使text长度比控件的宽度大
    	String s = "dsafsdfsdf(#开心)fsgfdg(#闭嘴)fdgdfgdfgdfgdfgdfgfdgfdgfdgfdgdfg";
    	newsModel.setStatus(s);
    	String strModel = newsModel.getStatus();
        setStateText(strModel);
        
	}
    
	private void setStateText(String strModel){
		if(!TextUtils.isEmpty(strModel)){
			tvState.setStateList(newsModel.getStateList());
			tvState.setText(strModel);
            tvState.init(getWindowManager(), handler);
            tvState.startScroll();
            tvState.start();
        }
	}
	<pre name="code" class="java"> <span style="white-space:pre">	</span> public void setStatus(String status){
    	this.status = status;
    	if(!TextUtils.isEmpty(status)){
    		stateList = ParseNewsInfoUtil.parseStr(status);
    	}
   <span style="white-space:pre">	</span>}

private Handler handler = new Handler() {@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case ScrollText.TEXT_TIMER:if(tvState != null){tvState.scrollText();}break;default:break;}}};

 
  
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.WindowManager;
import android.widget.TextView;
import com.kaixin001.item.LinkInfo;
import com.kaixin001.model.StateFaceModel;

public class ScrollText extends TextView {

	public static final int TEXT_TIMER = 100;
	private float textLength = 0f;// 文本长度
	private float viewWidth = 0f;
	private float step = 0f;// 文字的横坐标
	private float y = 0f;// 文字的纵坐标
	private float temp_view_plus_text_length = 0.0f;// 用于计算的暂时变量
	private float temp_view_plus_two_text_length = 0.0f;// 用于计算的暂时变量
	private boolean isStarting = false;// 是否開始滚动
	private int left = 0;
	private int right = 0;
	private Paint paint = null;// 画图样式
	private String text = "";// 文本内容
	private Bitmap txtBmp;
	private Canvas txtCanvas;
	private FontMetrics fontMetrics;
	private Timer timer = new Timer();
	private ArrayList<LinkInfo> stateList;

	Handler handler;

	TimerTask task = new TimerTask() {
		public void run() {
			if (handler != null && isStarting) {
				Message msg = Message.obtain();
				msg.what = TEXT_TIMER;
				handler.sendMessage(msg);
			}
		}
	};

	public ScrollText(Context context) {
		super(context);
	}

	public ScrollText(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ScrollText(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	/** */
	/**
	 * 文本初始化,每次更改文本内容或者文本效果等之后都须要又一次初始化一下
	 */
	public void init(WindowManager windowManager, Handler handler) {
		try {
			this.handler = handler;
			paint = new Paint();
			paint.setAntiAlias(true);
			paint.setStyle(Style.STROKE);
			paint.setTextSize(getTextSize());
			paint.setColor(getCurrentTextColor());
			text = getText().toString();
			textLength = 0;
			// textLength = paint.measureText(text);
			int len = stateList.size();
			for (int i = 0; i < len; i++) {
				LinkInfo info = stateList.get(i);
				if (info.isFace()) {// 表情符排版
					Bitmap faceBmp = StateFaceModel.getInstance()
							.getSmallFaceIcon(info.getContent());
					int xLen = faceBmp.getWidth();
					textLength += xLen + 4;
					continue;
				}
				String strContent = info.getContent();
				float xLen = paint.measureText(strContent);
				textLength += xLen;
			}
			left = this.getPaddingLeft();
			right = this.getPaddingRight();
			step = textLength;
			fontMetrics = paint.getFontMetrics();

			y = getPaddingTop();// getTextSize() + getPaddingTop();
			txtBmp = null;
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public void scrollText() {
		if (!isStarting) {
			return;
		}
		invalidate();
		if (viewWidth < textLength) {
			step += 0.5;
			if (step > temp_view_plus_two_text_length) {
				step = textLength;
			}
		}
	}

	public void setStateList(ArrayList<LinkInfo> stateList) {
		this.stateList = stateList;
	}

	private void setTxtBmp() {
		if (txtBmp == null && fontMetrics != null) {
			y = -paint.ascent();// fontMetrics.bottom -
								// fontMetrics.ascent;//(this.getHeight() -
								// (int)fontMetrics.ascent)/2;
			viewWidth = getWidth() - left - right;
			temp_view_plus_text_length = viewWidth + textLength;
			temp_view_plus_two_text_length = viewWidth + textLength * 2;
			txtCanvas = new Canvas();

			int width = (int) viewWidth;
			float height = getHeight();
			txtBmp = Bitmap.createBitmap(width, (int) height, Config.ARGB_8888);
			txtCanvas.setBitmap(txtBmp);

		}
	}

	/** */
	/**
	 * 開始滚动
	 */
	public void startScroll() {
		isStarting = true;
	}

	/** */
	/**
	 * 停止滚动
	 */
	public void stopScroll() {
		isStarting = false;
		// invalidate();
	}

	public void start() {
		timer.schedule(task, 10, 20);
	}

	public void stop() {
		timer.cancel();
	}

	@Override
	public void onDraw(Canvas canvas) {
		try {
			setTxtBmp();
			if (txtBmp == null) {
				return;
			}
			Paint txtPaint = new Paint();
			txtPaint.setColor(Color.WHITE);
			txtPaint.setStyle(Style.FILL);
			txtCanvas.drawRect(0, 0, txtBmp.getWidth(), txtBmp.getHeight(),
					txtPaint);
			txtPaint.setAntiAlias(true);
			txtPaint.setStyle(Style.STROKE);
			txtPaint.setTextSize(getTextSize());
			txtPaint.setColor(getCurrentTextColor());
			float x = 0;
			// step为text的宽度
			if (viewWidth < textLength) {
				x = temp_view_plus_text_length - step;
			}
			int len = stateList.size();
			float curLen = x;
			for (int i = 0; i < len; i++) {
				LinkInfo info = stateList.get(i);
				if (info.isFace()) {// 表情符排版
					Bitmap faceBmp = StateFaceModel.getInstance()
							.getSmallFaceIcon(info.getContent());
					int xLen = faceBmp.getWidth();
					txtCanvas.drawBitmap(faceBmp, curLen + 2, 0, txtPaint);
					curLen += xLen + 4;
					continue;
				}
				String strContent = info.getContent();
				strContent = strContent.replaceAll("\n", " ");
				float xLen = txtPaint.measureText(strContent);
				//由于x的值一直降低,所以文字能够滚动
				txtCanvas.drawText(strContent, curLen, y, txtPaint); //<span style="font-family: Arial, Helvetica, sans-serif;">txtCanvas早drawtext.txtBmp上也自然也带有这些text</span>

				curLen += xLen;
			}
			canvas.drawBitmap(txtBmp, left, 0, paint);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		txtBmp = null;
		setTxtBmp();
	}
}


代码:http://download.csdn.net/detail/baidu_nod/7736733


Android 中实现 TextView 文字滚动可以使用以下两种方式: 一、使用 Marquee(跑马灯)属性 在布局文件中的 TextView 中添加以下属性: ``` <TextView android:id="@+id/text_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="这是一段需要滚动文字这是一段需要滚动文字这是一段需要滚动文字" android:singleLine="true" android:ellipsize="marquee" android:focusable="true" android:focusableInTouchMode="true" android:marqueeRepeatLimit="marquee_forever" android:scrollHorizontally="true" android:padding="5dp" android:textColor="#000000" android:textSize="20sp" /> ``` 其中,关键属性为: - android:ellipsize="marquee":当文字超出 TextView 的宽度时,显示省略号并开启跑马灯效果。 - android:focusable="true" 和 android:focusableInTouchMode="true":设置为可获得焦点,让 TextView 能够滚动。 - android:marqueeRepeatLimit="marquee_forever":设置跑马灯无限循环。 - android:scrollHorizontally="true":可水平滚动。 在 Java 代码中,调用 setHorizontallyScrolling() 方法也可以实现水平滚动,示例代码如下: ``` TextView textView = findViewById(R.id.text_view); textView.setHorizontallyScrolling(true); ``` 二、使用代码实现 在 Java 代码中使用 TextView 的 setEllipsize() 方法和 setMarqueeRepeatLimit() 方法可以实现 TextView文字滚动效果。示例代码如下: ``` TextView textView = findViewById(R.id.text_view); textView.setText("这是一段需要滚动文字这是一段需要滚动文字这是一段需要滚动文字"); textView.setSingleLine(true); textView.setEllipsize(TextUtils.TruncateAt.MARQUEE); textView.setMarqueeRepeatLimit(-1); textView.setFocusable(true); textView.setFocusableInTouchMode(true); textView.requestFocus(); ``` 需要注意的是,如果在代码中设置了跑马灯效果,还需要在布局文件中设置以下属性: ``` android:singleLine="true" android:scrollHorizontally="true" ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值