跑馬燈效果實(shí)現(xiàn)(多跑馬燈, 自定義響應(yīng)觸摸,點(diǎn)擊,可設(shè)置速度等屬性跑馬燈)

因?yàn)闃I(yè)務(wù)需求,需要設(shè)置文字的跑馬燈效果,查閱相關(guān)資料后發(fā)現(xiàn)資料比較散亂,現(xiàn)整理如下:

1.單跑馬燈效果(系統(tǒng)自帶TextView相關(guān)屬性設(shè)置)
2.多跑馬燈效果(簡(jiǎn)單自定義控件)
3.可調(diào)節(jié)跑馬燈效果(自定義控件)

單跑馬燈效果

在TextView中設(shè)置相關(guān)屬性即可:

<TextView    
  android:layout_width="wrap_content"    
  android:layout_height="wrap_content"    
  android:ellipsize="marquee"    
  android:marqueeRepeatLimit="marquee_forever"    
  android:focusable="true"    
  android:focusableInTouchMode="true"    
  android:singleLine="true"    
  android:text="是時(shí)候表演真正的跑馬燈了1 2 3 4 5 6 7 8 9 10" />
  • Tip: ellipsize, marqueeRepeatLimit, focusable, focusableInTouchMode, singleLine屬性需如上設(shè)置
  • 優(yōu)點(diǎn): 簡(jiǎn)單,直接調(diào)用系統(tǒng)控件
  • 缺點(diǎn): 因?yàn)榻裹c(diǎn)獲取問(wèn)題多TextView時(shí)只有一個(gè)有滾動(dòng)效果,不支持相關(guān)屬性設(shè)置,速度,刷新頻率,延遲,點(diǎn)擊和觸摸事件等

多跑馬燈效果

自定義TextView

package com.junseek.zhuikemarketing;
import android.content.Context;
import android.util.AttributeSet;import android.widget.TextView;
/** 
  * @ author      Qsy 
  * @ date        16/8/12 下午8:00 
  * @ description 跑馬燈,可多焦點(diǎn)并行 
  */
public class MarqueeTextView extends TextView {
  public MarqueeTextView(Context context) {
    super(context); 
  }

  public MarqueeTextView(Context context, AttributeSet attrs) {        
    super(context, attrs);    
  }    

  public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {        
    super(context, attrs, defStyle);    
  }    

  @Override    
  public boolean isFocused() {        
    return true;    
  }
}
<TextView    
  android:layout_width="wrap_content"    
  android:layout_height="wrap_content"    
  android:ellipsize="marquee"    
  android:marqueeRepeatLimit="marquee_forever"    
  android:singleLine="true"    
  android:text="是時(shí)候表演真正的跑馬燈了1 2 3 4 5 6 7 8 9 10" />
  • Tip: ellipsize,marqueeRepeatLimit,singleLine屬性需如上設(shè)置
  • 優(yōu)點(diǎn): 簡(jiǎn)單繼承TextView,效果基本等同TextVIew效果,可多個(gè)控件同時(shí)執(zhí)行滾動(dòng)效果
  • 缺點(diǎn): 不支持相關(guān)屬性設(shè)置,速度,刷新頻率,延遲,點(diǎn)擊和觸摸事件等

可調(diào)節(jié)跑馬燈效果

<TextView    
  android:layout_width="fill_parent"    
  android:layout_height="wrap_content"    
  android:ellipsize="none"    
  android:singleLine="true"    
  android:text="是時(shí)候表演真正的跑馬燈了1 2 3 4 5 6 7 8 9 10"    />
  • Tip: 需要設(shè)置ellipsize,singleLine屬性,設(shè)置gravity=center屬性時(shí)文字消失(原因待測(cè))
  • 優(yōu)點(diǎn): 支持設(shè)置控件初始化時(shí)候延遲滾動(dòng),刷新速度,滾動(dòng)速度,暫停滾動(dòng),開(kāi)始滾動(dòng),點(diǎn)擊暫停,再次點(diǎn)擊開(kāi)始,拖動(dòng)控件時(shí)響應(yīng)水平滑動(dòng)
  • 缺點(diǎn): 相對(duì)原生控件需要文字完全滾出控件左邊界后右邊界才回進(jìn)入新的文字(待優(yōu)化,TextView中不必等文字完全滾出左邊界),需要較多的自定義內(nèi)容,內(nèi)存消耗較大(待測(cè))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容