跑馬燈在項(xiàng)目了其實(shí)應(yīng)用的還比較多,特別是做多媒體的時(shí)候,音樂(lè)視頻藍(lán)牙等等經(jīng)常用到。
比如音樂(lè)的專(zhuān)輯信息,藍(lán)牙通話(huà)記錄,以及視頻列表等等...。
不好直接上項(xiàng)目效果圖所以就做了一個(gè)簡(jiǎn)單的Demo放手機(jī)上顯示效果了。
單行跑馬燈效果
做起來(lái)也很簡(jiǎn)單,在布局文件里面設(shè)置幾個(gè)屬性就OK.
先上效果圖:

設(shè)置如下。
//設(shè)置為跑馬燈顯示
android:ellipsize="marquee"
//獲取焦點(diǎn)
android:focusable="true"
//可以通過(guò)toucth來(lái)獲得focus
android:focusableInTouchMode="true"
//單行顯示文字
android:singleLine="true"
//設(shè)置重復(fù)的次數(shù)
android:marqueeRepeatLimit="marquee_forever"
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:text="eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen" />
多行跑馬燈效果圖
其實(shí)主要是想講下多行跑馬燈,首先直接上圖:

如果要設(shè)置多行的話(huà)
首先我們寫(xiě)一個(gè)類(lèi),繼承TextView這個(gè)類(lèi),實(shí)現(xiàn)它的構(gòu)造方法,重寫(xiě)isFocused()方法 ,將它的返回值都為true
如果有彈窗之類(lèi)的導(dǎo)致無(wú)作用的話(huà)我們可以直接重寫(xiě)onWindowFocusChanged()方法,將super方法屏注釋掉。
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
public class MarqueeTextView extends TextView{
public MarqueeTextView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
public boolean isFocused() {
// TODO Auto-generated method stub
return true;
}
@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
// super.onWindowFocusChanged(hasWindowFocus);
}
}
核心代碼就是isFocused()和onWindowFocusChanged()方法。
布局文件如下:
其實(shí)就和前面是一樣的 當(dāng)然也可以在代碼里面設(shè)置這些屬性,布局里面就不用設(shè)置了。
<combd.textviewmarquee.MarqueeTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:marqueeRepeatLimit="marquee_forever"
android:text="eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen" />