android實現跑馬燈效果(可以實現兩個以上跑馬燈)
來源:程序員人生 發布時間:2014-11-10 08:44:12 閱讀次數:3494次
本文用了繼承自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設為具體大小也行。--!>
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈