跑马灯效果的实现
(1)Android自带的跑马灯效果
<TextView
android:id="@+id/tv_marquee_normal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:marqueeRepeatLimit="marquee_forever"
android:scrollHorizontally="true"
android:singleLine="true"
android:text="hahahahahahahahahahahahahahhahahahahahahahahahaha" />
(2)通过自定义TextView来实现
public class MarqueeView1 extends TextView{
private boolean isScroll;
public MarqueeView1(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
createView();
}
public MarqueeView1(Context context, AttributeSet attrs) {
super(context, attrs);
createView();
}
public MarqueeView1(Context context) {
super(context);
createView();
}
private void createView() {
setEllipsize(TruncateAt.MARQUEE);
setMarqueeRepeatLimit(-1);
setFocusableInTouchMode(true);
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
if (focused) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
}
@Override
public void onWindowFocusChanged(boolean focused) {
if (focused) {
super.onWindowFocusChanged(focused);
}
}
@Override
public boolean isFocused() {
return isScroll;
}
public void Scroll(boolean b){
isScroll = b;
if (isScroll) {
super.onWindowFocusChanged(true);
}else {
super.onWindowFocusChanged(false);
}
}
@Override
protected void onDraw(Canvas canvas) {
final int textWidth = getWidth() - getCompoundPaddingLeft() -
getCompoundPaddingRight();
final float lineWidth = this.getLayout().getLineWidth(0);
final float gap = textWidth / 3.0f;
float mGhostStart = lineWidth - textWidth + gap;
float mMaxScroll = mGhostStart + textWidth;
float mGhostOffset = lineWidth + gap;
float mFadeStop = lineWidth + textWidth / 6.0f;
float mMaxFadeScroll = mGhostStart + lineWidth + lineWidth;
Log.e("point_X",mMaxFadeScroll +"");
super.onDraw(canvas);
}
}
(3)也是通过自定义TextView实现,但是他添加了开始,暂停,滚动速度,以及开始滚动的停留时间
还有就是正常滚模式,多条广告一条一条在同一个textView中挨个展示
ublic class BulletinView extends TextView implements Runnable{
protected static final int STATUE_RUN = 0;
private int currentScrollX;// 当前滚动的位置
private boolean isStop = false;
private int textWidth;
private List<String> mList;
private final int REPEAT = 1;
private int repeatCount = 0;
private int currentNews = 0;
private int defaultSpeed = 1;
private long defaulRemain = 2000;
private Marquee mType = Marquee.NORMAL_CODE;
private Handler mHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case STATUE_RUN:
post(BulletinView.this);
break;
default:
break;
}
};
};
public BulletinView(Context context) {
super(context);
init();
// TODO Auto-generated constructor stub
}
public BulletinView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public BulletinView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init(){
setClickable(true);
setSingleLine(true);
setEllipsize(TruncateAt.MARQUEE);
setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
}
public void setData(List<String> mList){
if(mList == null || mList.size()==0){
return;
}
this.mList = mList;
currentNews = 0;
String n = mList.get(currentNews);
setText(n);
setTag(n);
startScroll();
}
@Override
public void setText(CharSequence text, BufferType type) {
super.setText(text, type);
MeasureTextWidth();
}
@Override
public void onScreenStateChanged(int screenState) {
super.onScreenStateChanged(screenState);
if(screenState == SCREEN_STATE_ON){
startScroll();
}else{
stopScroll();
}
}
/**
* 获取文字宽度
*/
private void MeasureTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
@Override
public void run() {
/* if(textWidth < 1){
//title null api error.
if(mList != null && mList.size() > 0){
nextNews();
}else{
return;
}
}*/
currentScrollX += defaultSpeed;// 滚动速度
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (getScrollX() >= textWidth) {
currentScrollX = -getWidth();
scrollTo(currentScrollX, 0);
if (mType == Marquee.NORMAL_CODE) {
}else if (mType == Marquee.ADVERTISE_CODE) {
if(repeatCount >= REPEAT){
//reach max times
nextNews();
}else{
repeatCount ++;
}
}
}
postDelayed(this, 50);
}
private void nextNews(){
repeatCount = 0;
currentNews ++;
currentNews = currentNews%mList.size();//cycle index
String n = mList.get(currentNews);
currentScrollX = 0;
setText(n);
setTag(n);
}
// 开始滚动
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
mHandler.sendEmptyMessageDelayed(STATUE_RUN, defaulRemain);
}
// 停止滚动
public void stopScroll() {
isStop = true;
}
public void setModeStyle(Marquee type){
mType = type;
}
public int getDefaultSpeed() {
return defaultSpeed;
}
public void setDefaultSpeed(int defaultSpeed) {
this.defaultSpeed = defaultSpeed;
}
public long getDefaulRemain() {
return defaulRemain;
}
public void setDefaulRemain(long defaulRemain) {
this.defaulRemain = defaulRemain;
}
final static int ADVERTISE_CODE = 0;
final static int NORMAL_CODE = 0;
enum Marquee{
ADVERTISE_CODE,NORMAL_CODE
}
}
Demo: http://download.csdn.net/detail/u012808234/9110929
跑马灯的效果没有达到要求,最后只能在进行修改,最后实现了一个公告切换显示的公告栏,有需要的看看: