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布局文件相关代码:






你可能感兴趣的:(androidUI)