Android控件封装之跑马灯(二)

Android开发时,经常遇到标题或用户昵称比较长而显示不下的时候,我们希望通过跑马灯的效果来向用户展示全部的文字信息,但是利用android自带的TextView却有诸多不便,比如不能控制暂停、开始、不能控制速度,而且还受焦点的影响。这时开发一个自定义的跑马灯控件显得非常有必要。
话不多说,java代码如下:

package cn.emag.utils.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.TextView;

/**
 * 跑马灯的文本框.
 */
public class CustomMarqueeTextView extends TextView implements Runnable {

	/** 当前滚动的位置 */
	private int currentScrollX = 0;

	/** 是否停止 */
	private boolean isStop = false;

	/** 文本的真实宽度 */
	private int textWidth;

	private boolean isMeasure = false;

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

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

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

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// 文字宽度只需获取一次就可以了
		if (!isMeasure) {
			getTextWidth();
			isMeasure = true;
		}
	}

	@Override
	public void setText(CharSequence text, BufferType type) {
		super.setText(text, type);
		this.startScroll();
	}

	/**
	 * 获取文字宽度
	 */
	private void getTextWidth() {
		Paint paint = this.getPaint();
		String str = this.getText().toString();
		textWidth = (int) paint.measureText(str);
	}

	private long stime = 0;

	@Override
	public void run() {
		if (this.textWidth == 0 || this.getWidth() == 0) {
			postDelayed(this, 200);
			return;
		} else if (this.getWidth() >= this.textWidth) {
			return;
		} else if (isStop) {
			return;
		}
		if (stime == 0) {
			stime = System.currentTimeMillis();
		}
		double dx = (System.currentTimeMillis() - stime) * 2.0 / 20;
		currentScrollX = (int) dx;
		scrollTo(currentScrollX, 0);

		if (getScrollX() >= this.textWidth) {
			currentScrollX = 0 - this.getWidth();
			stime = System.currentTimeMillis() - currentScrollX * 10;
			scrollTo(currentScrollX, 0);
		}
		postDelayed(this, 20);
	}

	/**
	 * 开始滚动
	 */
	public void startScroll() {
		isStop = false;
		this.removeCallbacks(this);
		post(this);
	}

	/**
	 * 停止滚动
	 */
	public void stopScroll() {
		isStop = true;
	}

	/**
	 * 从头开始滚动
	 */
	public void restartScroll() {
		currentScrollX = 0;
		startScroll();
	}
}


在layout中使用的时候,配置如下:

    <cn.emag.utils.view.CustomMarqueeTextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:singleLine="true"
        android:text="http://schemas.android.com/apk/res/android" />


 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值