android实现跑马灯效果(能够实现两个以上跑马灯)

本文用了继承自TextView的MarqueeTextView来实现跑马灯效果。原因是,跑马灯效果是须要TextView拥有焦点才会跑动的。而有时候TextView获得焦点会有点耗时,造成要等待一段时间跑马灯效果才会出来。

另外。系统默认是仅仅有一个TextView处于focused状态,而当页面有不少于两个跑马灯时,用TextView就不好实现了。



MarqueeTextView类代码:

public class MarqueeTextView extends TextView{

	public MarqueeTextView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	
	@Override
	protected void onFocusChanged(boolean focused, int direction,
			Rect previouslyFocusedRect) {
		// TODO Auto-generated method stub
		super.onFocusChanged(focused, direction, previouslyFocusedRect);
	}
	
	@Override
	public void onWindowFocusChanged(boolean hasWindowFocus) {
		// TODO Auto-generated method stub
		if(hasWindowFocus) super.onWindowFocusChanged(hasWindowFocus);
	}
	
	@Override
	@ExportedProperty(category = "focus")
	public boolean isFocused() {
		return true;
	}

}


XML布局文件相关代码:

<com.barry.components.MarqueeTextView
    android:layout_width="0dp"
    android:layout_height="match_parent"
    android:layout_weight="1.0"
    android:ellipsize="marquee"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:freezesText="true"
    android:marqueeRepeatLimit="marquee_forever"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:scrollHorizontally="true"
    android:singleLine="true" />
<!--width设为详细大小也行。--!>




原文地址:https://www.cnblogs.com/liguangsunls/p/6856924.html