因为最近公司要做一个抽奖功能,里面有一个模块就是中奖用户,对中奖用户实现垂直方向跑马灯效果,最先的思想方式是类似歌词,做了n久,发现刷屏时间会有问题,特自己写了一个控件,希望能帮到大家
“`
package com.demo.xsl.text;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.TextView;
/**
* 文本垂直滚动
* @author eandroidhu
*
*/
public class ScrollTextView extends TextView {
/**
* 每行的字符串
*/
ArrayList lineStrings;
/**
* 当前的位移
*/
float currentY;
/**
* 处理滚动消息
*/
Handler handler;
/**
* 要显示的text
*/
String scrollText="";
/**
* 真实宽度,在配置width="xxdp"里起作用
*/
private int exactlyWidth = -1;
/**
* 真实高度,在配置height="xxdip"里起作用
*/
private int exactlyHeight = -1;
private float index = 0;
public String getScrollText() {
return scrollText;
}
public void setScrollText(String scrollText) {
this.scrollText = scrollText;
reset();
}
/**
* 重置
*/
private void reset() {
if(lineStrings!=null)
lineStrings.clear();
stop();
currentY = 0;
absloutHeight = 0;
this.setText("");
if(handler!=null){
handler.removeMessages(0);
handler.removeMessages(1);
handler.removeMessages(2);
handler.removeMessages(3);
}
requestLayout();
invalidate();
}
public ScrollTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ScrollTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ScrollTextView(Context context) {
super(context);
init();
}
/**
* 是否在滚动
*/
boolean scrolling = false;
/**
* 实际高度:所有字显示完全需要的高度
*/
float absloutHeight = 0;
/**
* handler发消息的时间间隔
*/
private int delayTime = 10; //滚动时间
private int stopTime = 3000; //停休时间
public int getDelayTime() {
return delayTime;
}
public void setDelayTime(int delayTime) {
this.delayTime = delayTime;
}
public int getStopTime() {
return stopTime;
}
public void setStopTime(int stopTime) {
this.stopTime = stopTime;
}
/**
* 每次滚动的距离
*/
float speed = 0.5f;
/**
* 初始化
*/
void init() {
handler = new Handler() {
@Override
public void hand