/**
* @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);
}
}
* @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);
}
}