android 自定義跑馬燈效果,自由控制跑馬燈 你未必知道這樣用

衆所周知 android sdk中提供了跑馬燈效果,下面我來簡單介紹一下該效果的應用場景:

       就是在TextView以及TextView的子控件中(如還有其他控件支持請留言,必跟新)文本顯示不全的情況,使其文本內容滾動顯示,達到用戶可以看到全部文本信息的效果。

下面我就跟大家分析其幾種用法(最後一種你未必用過,也是本文的重點):

     今天就以TextView爲例 

1.首先想要你的控件支持跑馬燈效果,要先在xml中配置紅色部分代碼:

       <TextView 
         
         android:layout_height="150dp"
         android:layout_width="150dp"
         android:gravity="center" 
         android:textSize="2dp"
         android:marqueeRepeatLimit="marquee_forever"
         android:ellipsize="marquee"
         android:singleLine="true"
/ >  

在配置完紅色部分代碼後你的TextView就具有跑馬燈效果了(必須要獲取到焦點纔會有效,如果你想不獲取焦點也有效果那該怎麼辦呢,請繼續往下看)。

2.如果你想要你的控件在未獲取焦點時也有跑馬燈效果,那就請重寫你的TextView控件,

重寫isFocused方法,返回true; 重點在紅色標誌區域  代碼如下:

import android.content.Context;
import android.util.AttributeSet;

public class FocusLabelTextView extends TextView {

 public FocusLabelTextView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }

 public FocusLabelTextView(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }

 public FocusLabelTextView(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }


 @Override
 public boolean isFocused() {
  // TODO Auto-generated method stub

//返回true的意思是,該控件被欺騙爲 獲取焦點
  return true;

//return super.isFocused();
 }

}


3.第三種方法乃是本文的重點,前面兩種方法目前已經比較常見。 第二種方法有個缺點就是 無法隨時控制跑馬燈 停和跑, 而且跑馬燈在跑的時候很耗資源。

所以我就想是否有方法可以由我們自己來控制跑馬燈的 停和跑呢? 經過研究終於被我找到了:

在配置了1中的xml信息後, 你可以在你需要跑馬燈跑的時候調用TextView.setSelected(true); 停的時候調用TextView.setSelected(false)。這樣就可以自由的控制了。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章