实现自定义的垂直滚动的TextView和

/**
 * @author sun Apr 2, 2014 定义垂直滚动的TextView
 */
public class VeticalMarqueeTextView extends TextView {


protected static final int FLAG_MARQUEE_TEXT = 0x1;
protected static final int FLAG_MARQUEE_MOVING = 0x2;
protected long delayMillis = 1500;
protected long firstdelayMillis = 500;
protected boolean isFirst = false;
private int count;

private static boolean isStop = true;


public VeticalMarqueeTextView(Context context) {


super(context);
}


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


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

public void setDelayMillis(long delayMillisParam){
delayMillis = delayMillisParam;
}


/**
* sun Apr 2, 2014 TODO 初始化TextView,被调用后开始自动垂直滚动
*/
String text;

public void init() {
isStop = false;

count = getLineCount();
if (count == 1) {
return;
}
text = this.getText().toString();
// 设置抗锯齿
this.getPaint().setAntiAlias(true);


isFirst = true;
lineNumber = 0;
// this.setMovementMethod(ScrollingMovementMethod.getInstance());
Message msg = Message.obtain();
msg.what = FLAG_MARQUEE_TEXT;
handler.sendMessageDelayed(msg, firstdelayMillis);
}


private int lineNumber = 0;
int velocity = 3;
int scrollY = 0;
int delayMillis4Move = 80;

private static class VeticalMarqueeHandler extends
WeakReferenceHandler<VeticalMarqueeTextView> {


public VeticalMarqueeHandler(VeticalMarqueeTextView reference) {
super(reference);
// TODO Auto-generated constructor stub
}


@Override
protected void handleMessage(final VeticalMarqueeTextView reference,
Message msg) {
if (isStop) {
return;
}

switch (msg.what) {
case FLAG_MARQUEE_TEXT:
reference.handleMarqueeText(msg);
break;
case FLAG_MARQUEE_MOVING:
reference.handleMarqueeMove(msg);
break;
}
}
}


private void handleMarqueeText(Message msg) {
try {
if (lineNumber == 0 && !isFirst) {
// VeticalMarqueeTextView.this.setText("");
// VeticalMarqueeTextView.this.setText(text);
if(lineNumber <= getLineCount()){
scrollTo(getScrollX(), -getLayout().getLineTop(lineNumber + 1));
}else{
YLog.d("VeticalMarqueeTextView", "lineNumber out of total count");
}
} else {
isFirst = false;
}
scrollY = getScrollY();
msg = Message.obtain();
msg.what = FLAG_MARQUEE_MOVING;
handler.sendMessageDelayed(msg, delayMillis4Move);
} catch (Exception e) {
 
// TODO Auto-generated catch block  
e.printStackTrace();  

}
}


private void handleMarqueeMove(Message msg) {
try {
scrollY += velocity;
// java.lang.IndexOutOfBoundsException: 2, 1 这里有可能报错
if (count >= lineNumber) {
if (scrollY < getLayout().getLineTop(lineNumber)) { // scrollY的值小於需要滑動到的位置的Y值


scrollTo(getScrollX(), scrollY);
msg = Message.obtain();
msg.what = FLAG_MARQUEE_MOVING;
handler.sendMessageDelayed(msg, delayMillis4Move);


} else { // scrollY的值大於或者等於需要滑動到的位置的Y值
if(lineNumber <= getLineCount()){
scrollTo(getScrollX(), getLayout().getLineTop(lineNumber));
}
msg = Message.obtain();
msg.what = FLAG_MARQUEE_TEXT;


if (lineNumber < getLineCount() - 1) {
lineNumber++;
} else {
lineNumber = 0;
}
handler.sendMessageDelayed(msg, delayMillis);
}
}
} catch (Exception e) {
 
// TODO Auto-generated catch block  
e.printStackTrace();  

}
}



private VeticalMarqueeHandler handler = new VeticalMarqueeHandler(this);


/**
* 取消滾動
*/
public void cancel() {
isStop = true;

scrollTo(getScrollX(), getLayout().getLineTop(0));
handler.removeMessages(FLAG_MARQUEE_MOVING);
handler.removeMessages(FLAG_MARQUEE_TEXT);
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值