Android之跑馬燈

跑馬燈在項(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.

先上效果圖:


18e4017761df3b42cdf56619f51dcdf1.gif

設(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í)主要是想講下多行跑馬燈,首先直接上圖:


68810ab5960062cbb36f7d7383a30e6f.gif

如果要設(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" />
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 178,667評(píng)論 25 709
  • 在市面上,看了很多人的博客,關(guān)于A(yíng)ndroid跑馬燈效果的實(shí)現(xiàn),感覺(jué)寫(xiě)的都不怎么詳細(xì),如EditText搶焦點(diǎn)的問(wèn)...
    gooddaytoyou閱讀 26,825評(píng)論 7 23
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線(xiàn)程,因...
    小菜c閱讀 7,291評(píng)論 0 17
  • 我建議你去乙方的公司鍛煉一下,多寫(xiě)一些PR稿/新媒體稿,體會(huì)一下其中的不同。 現(xiàn)在的公司,在一年前,你就應(yīng)該離開(kāi)了...
    搶飯?bào)H閱讀 477評(píng)論 2 2
  • 一、【時(shí)間】: 兩個(gè)月(20161206~20170206) 二、【目標(biāo)】: 完成本年度的業(yè)績(jī)目標(biāo)(剩最后...
    賴(lài)星閱讀 220評(píng)論 0 0

友情鏈接更多精彩內(nèi)容