本文用了继承自TextView的MarqueeTextView来实现跑马灯效果。缘由是,跑马灯效果是需要TextView具有焦点才会跑动的。而有时候TextView取得焦点会有点耗时,造成要等待1段时间跑马灯效果才会出来。另外,系统默许时只有1个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设为具体大小也行。--!>