Android 文字自動滾動(跑馬燈)效果的兩種實現方法[特別好使]


有時候在xml中寫的跑馬燈效果不滾動:原因有以下

 

Android系統中TextView實現跑馬燈效果,必須具備以下幾個條件:
1、android:ellipsize=”marquee”
2、TextView必須單行顯示,即內容必須超出TextView大小
3、TextView要獲得焦點才能滾動( 如果還不行,就要用自定義的TextView控件中重寫isFocused()返回true就行【方法代碼在下面的AlwaysMarqueeTextView 類】,但是遇到新問題就是界面有多個這樣的控件顯示時當彈出Dialog后這些自定義的TextView控件就會失去焦點,跑馬燈效果又變成“...”的形式了,當Dialog消失之后又恢復正常
XML代碼:
android:ellipsize="marquee", android:singleLine="true"
Java代碼:
mTVText.setText("哼唱接撒砥礪風節雷鋒精神http://orgcent.com/,很長很長很長很長很長很長的數據");
mTVText.setSingleLine(true);
mTVText.setEllipsize(TruncateAt.MARQUEE);
PS: TextView.setHorizontallyScrolling(true); //讓文字可以水平滑動
TextView還可以設置跑馬燈效果的滾動次數,如下:
XML代碼設置:
android:marqueerepeatlimit="1"。1代表1次,-1代表無限循環。
Java代碼設置:
mTVText.setMarqueeRepeatLimit(-1);
 

 

 

總結一下跑馬燈的實現效果,網上比較流行的有兩種,測試過了都可以實現文字滾動效果,建議使用第一種,因為可以更好地控制文字滾動速度、樣式、字體等屬性,第二種方法,還沒有找到控制的方法!

 

 

第一種:

控件類:AutoScrollTextView 繼承了TextView並做了一些修改,實現了寬度的判斷,文本自動滾動及開始和停止滾動等功能。

 

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Parcel;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.TextView;


public class AutoScrollTextView extends TextView implements OnClickListener {
    public final static String TAG = AutoScrollTextView.class.getSimpleName();
   
    private float textLength = 0f;//文本長度
    private float viewWidth = 0f;
    private float step = 0f;//文字的橫坐標
    private float y = 0f;//文字的縱坐標
    private float temp_view_plus_text_length = 0.0f;//用於計算的臨時變量
    private float temp_view_plus_two_text_length = 0.0f;//用於計算的臨時變量
    public boolean isStarting = false;//是否開始滾動
    private Paint paint = null;//繪圖樣式
    private String text = "";//文本內容

   
    public AutoScrollTextView(Context context) {
        super(context);
        initView();
    }

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

    public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }
   
   
    private void initView()
    {
        setOnClickListener(this);
    }
   
   
    public void init(WindowManager windowManager)
    {
        paint = getPaint();
        text = getText().toString();
        textLength = paint.measureText(text);
        viewWidth = getWidth();
        if(viewWidth == 0)
        {
            if(windowManager != null)
            {
                Display display = windowManager.getDefaultDisplay();
                viewWidth = display.getWidth();
            }
        }
        step = textLength;
        temp_view_plus_text_length = viewWidth + textLength;
        temp_view_plus_two_text_length = viewWidth + textLength * 2;
        y = getTextSize() + getPaddingTop();
    }
   
    @Override
    public Parcelable onSaveInstanceState()
    {
        Parcelable superState = super.onSaveInstanceState();
        SavedState ss = new SavedState(superState);
       
        ss.step = step;
        ss.isStarting = isStarting;
       
        return ss;
       
    }
   
    @Override
    public void onRestoreInstanceState(Parcelable state)
    {
        if (!(state instanceof SavedState)) {
            super.onRestoreInstanceState(state);
            return;
        }
        SavedState ss = (SavedState)state;
        super.onRestoreInstanceState(ss.getSuperState());
       
        step = ss.step;
        isStarting = ss.isStarting;

    }
   
    public static class SavedState extends BaseSavedState {
        public boolean isStarting = false;
        public float step = 0.0f;
        SavedState(Parcelable superState) {
            super(superState);
        }

        @Override
        public void writeToParcel(Parcel out, int flags) {
            super.writeToParcel(out, flags);
            out.writeBooleanArray(new boolean[]{isStarting});
            out.writeFloat(step);
        }


        public static final Parcelable.Creator<SavedState> CREATOR
                = new Parcelable.Creator<SavedState>() {
           
            public SavedState[] newArray(int size) {
                return new SavedState[size];
            }

            @Override
            public SavedState createFromParcel(Parcel in) {
                return new SavedState(in);
            }
        };

        private SavedState(Parcel in) {
            super(in);
            boolean[] b = null;
            in.readBooleanArray(b);
            if(b != null && b.length > 0)
                isStarting = b[0];
            step = in.readFloat();
        }
    }

   
    public void startScroll()
    {
        isStarting = true;
        invalidate();
    }
   
   
    public void stopScroll()
    {
        isStarting = false;
        invalidate();
    }
   

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawText(text, temp_view_plus_text_length - step, y, paint);
        if(!isStarting)
        {
            return;
        }
        step += 0.5;//0.5為文字滾動速度。
        if(step > temp_view_plus_two_text_length)
            step = textLength;
        invalidate();

    }

    @Override
    public void onClick(View v) {
        if(isStarting)
            stopScroll();
        else
            startScroll();
       
    }

}

 

xml中使用方法:

    <cn.tigertian.ui.AutoScrollTextView

    android:id="@+id/TextViewNotice"
    android:layout_height="30px"

    android:layout_width="fill_parent"
    android:text="@string/test_notice_1"

    android:textColor="#000"

    android:inputType="text"
    android:background="#EEE"

    android:textSize="20px">

   </cn.tigertian.ui.AutoScrollTextView>

 

Activity中使用方法:

        //啟動公告滾動條
        autoScrollTextView = (AutoScrollTextView)findViewById(R.id.TextViewNotice);
        autoScrollTextView.init(getWindowManager());
        autoScrollTextView.startScroll();

注:如果想改變跑馬燈的文字內容或者文字效果,則在調用完setText方法之后,需要再調用一下init方法,重新進行初始化和相關參數的計算。

 

第二種:

TextView實現文字滾動需要以下幾個要點:
1.文字長度長於可顯示范圍:android:singleLine="true"
2.設置可滾到,或顯示樣式:android:ellipsize="marquee"
3.TextView只有在獲取焦點后才會滾動顯示隱藏文字,因此需要在包中新建一個類,繼承TextView。重寫isFocused方法,這個方法默認行為是,如果TextView獲得焦點,方法返回true,失去焦點則返回false。跑馬燈效果估計也是用這個方法判斷是否獲得焦點,所以把它的返回值始終設置為true。
 
以下轉自他人:
 

publicclassAlwaysMarqueeTextViewextendsTextView{

publicAlwaysMarqueeTextView(Contextcontext){
super(context);
}

publicAlwaysMarqueeTextView(Contextcontext,AttributeSetattrs){
super(context,attrs);
}

publicAlwaysMarqueeTextView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
}

@Override
publicbooleanisFocused(){
returntrue;
}

在布局XML文件中加入這么一個AlwaysMarqueeTextView,這個加入方法也是剛剛學的。

XML語言: layout.xml
<com.examples.AlwaysMarqueeTextView
android:id=“@+id/AMTV1″
android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:lines=“1″
android:focusable=“true”
android:focusableInTouchMode=“true”
android:scrollHorizontally=“true”
android:marqueeRepeatLimit=“marquee_forever”
android:ellipsize=“marquee”
android:background=“@android:color/transparent”
/>

ellipsize屬性
設置當文字過長時,該控件該如何顯示。有如下值設置:”start”—–省略號顯示在開頭;”end”——省略號顯示在結尾;”middle”—-省略號顯示在中間;”marquee” ——以跑馬燈的方式顯示(動畫橫向移動)

marqueeRepeatLimit屬性
在ellipsize指定marquee的情況下,設置重復滾動的次數,當設置為marquee_forever時表示無限次。

focusable屬性
自己猜測的,應該是能否獲得焦點,同樣focusableInTouchMode應該是滑動時能否獲得焦點。

組合View的問題:

XML語言: 組合View
< LinearLayout
xmlns:android =“http://schemas.android.com/apk/res/android”
android:orientation =“vertical”
android:gravity =“center_vertical”
android:background =“@drawable/f_background”
android:layout_width =“fill_parent”
android:focusable =“true”
android:layout_height =“50px” >
< TextView
android:id =“@+id/info_text”
android:focusable =“true”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:text =“test marquee .. “
android:textColor =“@color/black”
android:singleLine =“true”
android:ellipsize =“marquee”
android:marqueeRepeatLimit =“3″
android:textSize =“18sp”
/>
< TextView
android:id =“@+id/date_text”
android:layout_width =“fill_parent”
android:layout_height =“wrap_content”
android:layout_gravity =“bottom”
android:textColor =“@color/gray”
android:text =“2010/05/28″
android:textSize =“12sp”
/>
</ LinearLayout >

上面示例中2個TextView組合為一個View,由於設置了LinearLayout為focusable而TextView就沒法取得焦點了,這樣 這個TextView的跑馬燈效果就顯示不出來,就算你也設置TextView的android:focusable="true"也是 沒用的. 這個時候就要使用addStatesFromChildren 這個屬性了,在LinearLayout中設置這個屬性,然后設置TextView的focusable="true"就可以了.關於 addStatesFromChildren的說明:

Sets whetherthisViewGroup's drawable statesalso include its children's drawable states.

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM