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" />