TextView跑馬燈的實現

2021-06-25 23:01:17 字數 1193 閱讀 9995

在textview屬性中有這樣的乙個屬性:

android:ellipsize 設定當文字過長時,該控制項該如何顯示。有如下值設定:」start」省略號顯示在開頭;」end」省略號顯示在結尾;」middle」省略號顯示在中間;;」marquee」 以跑馬燈的方式顯示(動畫橫向移動) 。

好像跑馬燈這種效果已經不用咱們來實現了,呵呵,不過有一點,如果文字不過長,在元件的範圍內,還想要跑馬燈的效果怎麼辦呢?當然的自己自定義啦,那還能怎麼做,哈哈。。。

上**,大家看看哈,注釋寫的很清楚啦,我也不再多說了,copy走就能用啊。。

/**

* 跑馬燈效果的textview。

* * @author luckchoudog

*/public class marqueetextview extends textview implements runnable

public marqueetextview(context context, attributeset attrs)

public marqueetextview(context context, attributeset attrs, int defstyle)

@override

protected void ondraw(canvas canvas)

} /**

* 獲取文字寬度

*/private void gettextwidth()

/*** 重寫settext方法

*/@override

public void settext(charsequence text, buffertype type)

@override

public void run()

if (getscrollx() <= -(this.getwidth()))

// 第二個引數可以控制平移的速度,數值越小表示越快,可以結合滾動跨度來設定平移速度。

postdelayed(this, 5);

} /**

* 開始滾動

*/public void startscroll()

/*** 停止滾動

*/public void stopscroll()

/*** 從頭開始滾動

*/public void startfor0()

}

TextView實現跑馬燈效果

這段 粘上就可用 顯示跑馬燈效果的前提條件就是你的文字內容要比顯示文字的外部元件長,即外部元件無法完整的顯示內部的文字內容。因此要實現跑馬燈效果有兩種設定方式 1 layout width 設定為成比文字內容短的固定值,最好不要寫成wrap content或者fill parent。2 如果layo...

TextView實現跑馬燈效果

一 方法 這裡我們用兩種方法來實現跑馬燈效果,雖然實質上是一種 實質就是 1 textview調出跑馬燈效果 2 textview獲取焦點 第一種 1 textview調出跑馬燈效果 android ellipsize marquee 2 textview獲取焦點 android focusable...

TextView實現跑馬燈效果

一 方法 這裡我們用兩種方法來實現跑馬燈效果,雖然實質上是一種 實質就是 1 textview調出跑馬燈效果 2 textview獲取焦點 第一種 1 textview調出跑馬燈效果 android ellipsize marquee 2 textview獲取焦點 android focusable...