自定義TextView跑馬燈效果可控制啟動/停止/速度/焦點(diǎn)
Android自帶的跑馬燈效果不太好控制,不能控制速度,不能即時停止和啟動,而且還受焦點(diǎn)的影響不已。由于項(xiàng)目需求需要用的可控制性高的跑馬燈效果,所以自己寫了一個自定義的TextView
注意:在布局文件引用本view時,paddingLeft,paddingRigh都必須為0dp,需要增加這兩個屬性的,大家可以自行修改代碼。
android:ellipsize="marquee" android:singleLine="true" 這兩個屬性也要加上
public class MarqueeText extends TextView implements Runnable {
private int currentScrollX;// 當(dāng)前滾動的位置
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
public MarqueeText(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (!isMeasure) {// 文字寬度只需獲取一次就可以了
getTextWidth();
isMeasure = true;
}
}
/**
* 獲取文字寬度
*/
private void getTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
@Override
public void run() {
currentScrollX -= 2;// 滾動速度
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (getScrollX() <= -(this.getWidth())) {
scrollTo(textWidth, 0);
currentScrollX = textWidth;
// return;
}
postDelayed(this, 5);
}
// 開始滾動
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
// 停止?jié)L動
public void stopScroll() {
isStop = true;
}
// 從頭開始滾動
public void startFor0() {
currentScrollX = 0;
startScroll();
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="start"
android:text="走起" />
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="stop"
android:text="停止" />
<Button
android:id="@+id/startfor0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="startFor0"
android:text="從頭開始" />
<simtice.demo.marqueetext.MarqueeText
android:id="@+id/test"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#339320"
android:ellipsize="marquee"
android:singleLine="true"
android:text="這才是真正的文字跑馬燈效果這才是真正的字跑馬燈效果這才是真正的"
android:textColor="#000000"
android:textSize="20dp" >
</simtice.demo.marqueetext.MarqueeText>
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
private MarqueeText test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
test = (MarqueeText) this.findViewById(R.id.test);
}
public void start(View v) {
test.startScroll();
}
public void stop(View v) {
test.stopScroll();
}
public void startFor0(View v){
test.startFor0();
}
}
- android中設(shè)置TextView/Button 走馬燈(Marquee)效果示例
- Android使用TextView跑馬燈效果
- Android基于TextView實(shí)現(xiàn)跑馬燈效果
- Android基于TextView不獲取焦點(diǎn)實(shí)現(xiàn)跑馬燈效果
- Android 中TextView中跑馬燈效果的實(shí)現(xiàn)方法
- Android基于TextView屬性android:ellipsize實(shí)現(xiàn)跑馬燈效果的方法
- Android 實(shí)現(xiàn)不依賴焦點(diǎn)和選中的TextView跑馬燈
- Android基于TextView實(shí)現(xiàn)的跑馬燈效果實(shí)例
- Android TextView實(shí)現(xiàn)跑馬燈效果的方法
- Android TextView跑馬燈效果實(shí)現(xiàn)方法
- Android中使用TextView實(shí)現(xiàn)文字跑馬燈效果
相關(guān)文章
Android利用AsyncTask異步類實(shí)現(xiàn)網(wǎng)頁內(nèi)容放大縮小
這篇文章主要為大家介紹了利用AsyncTask異步類實(shí)現(xiàn)網(wǎng)頁內(nèi)容放大縮小的相關(guān)資料,感興趣的小伙伴們可以參考一下2016-07-07Android?App頁面滑動標(biāo)題欄顏色漸變詳解
這篇文章主要為大家詳細(xì)介紹了Android?App頁面滑動標(biāo)題欄顏色漸變,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字
本文主要介紹了Android 桌面圖標(biāo)右上角顯示未讀消息數(shù)字的方法。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android基于MLKit實(shí)現(xiàn)條形碼掃碼的代碼示例
這篇文章將借助開源庫?MLKit?實(shí)現(xiàn)條形碼掃描,對于商品條形碼也可以很好地識別成功,該庫的使用內(nèi)容非常豐富,除了條碼識別,還有文字識別、圖像標(biāo)記、人臉檢測等等,本文篇文章就只介紹最基本的條形碼掃描使用,需要的朋友可以參考下2023-08-08Android下拉刷新控件SwipeRefreshLayout源碼解析
這篇文章主要為大家詳細(xì)解析Android下拉刷新控件SwipeRefreshLayout源碼,感興趣的小伙伴們可以參考一下2016-07-07Android 創(chuàng)建與解析XML(四)——詳解Pull方式
本篇文章主要介紹了Android創(chuàng)建與解析XML(二)——詳解Pull方式,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11