Android-兩種方式實現走馬燈效果,android-走馬燈

來源:互聯網
上載者:User

Android-兩種方式實現走馬燈效果,android-走馬燈

第一種方法(很普遍,很簡單的在xml布局檔案中設定TextView的屬性):

<TextView         android:id="@+id/tv_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:marqueeRepeatLimit="marquee_forever"        android:singleLine="true"        android:textSize="28sp"        android:text="路漫漫其修遠兮,吾將上下而求索"        android:textColor="#00ff00"        android:scrollHorizontally="true"/>

重要代碼:

           //設定為跑馬燈顯示

           android:ellipsize="marquee"

           //擷取焦點
          android:focusable="true"

          //可以通過toucth來獲得focus
          android:focusableInTouchMode="true"

          //設定重複的次數
          android:marqueeRepeatLimit="marquee_forever"

          //單行顯示文字
        android:singleLine="true"

2.第二種方法,由於大部分走馬燈文字會在手機螢幕的右側開始,這需要自訂控制項來實現了

   java代碼:

package com.example.cameratestdemo;import android.content.Context;import android.graphics.Canvas;import android.graphics.Paint;import android.os.Handler;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.widget.TextView;/** * 自訂控制項迴圈走馬燈的實現 *  * @author cyf 繼承自TextView */public class Util extends TextView implements Runnable {    private static final String TAG = "MarqueeTextView";    // 設定跑馬燈重複的次數,次數    private int circleTimes = 3;    //記錄已經重複了多少遍    private int hasCircled = 0;    private int currentScrollPos = 0;    // 跑馬燈走一遍需要的時間(秒數)    private int circleSpeed = 10;    // 文字的寬度    private int textWidth = 0;    private boolean isMeasured = false;    // Handler機制    private Handler handler;    private boolean flag = false;    // 構造方法    public Util(Context context, AttributeSet attrs) {        super(context, attrs);        // TODO Auto-generated constructor stub        this.removeCallbacks(this);        post(this);    }    /**     * 畫筆工具     */    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if (!isMeasured) {            getTextWidth();            isMeasured = true;        }    }    @Override    public void setVisibility(int visibility) {        // 二次進入時初始化成員變數        flag = false;        isMeasured = false;        this.hasCircled = 0;        super.setVisibility(visibility);    }    @Override    public void run() {        // 起始滾動位置        currentScrollPos += 1;        scrollTo(currentScrollPos, 0);        // Log.i(TAG, "pos"+currentScrollPos);        // 判斷滾動一次        if (currentScrollPos >= textWidth) {            // 從螢幕右側開始出現            currentScrollPos = -this.getWidth();            //記錄的滾動次數大設定的次數代表滾動完成,這個控制項就可以隱藏了            if (hasCircled >= this.circleTimes) {                this.setVisibility(View.GONE);                flag = true;            }            hasCircled += 1;        }        if (!flag) {            // 滾動時間間隔            postDelayed(this, circleSpeed);        }    }    /**     * 擷取文本顯示長度     */    private void getTextWidth() {        Paint paint = this.getPaint();        String str = this.getText().toString();        Log.i(TAG, str);        if (str == null) {            textWidth = 0;        }        textWidth = (int) paint.measureText(str);    }    /**     * 設定滾動次數,達到次數後設定不可見     *      * @param circleTimes     */    public void setCircleTimes(int circleTimes) {        this.circleTimes = circleTimes;    }    public void setSpeed(int speed) {        this.circleSpeed = speed;    }    public void startScrollShow() {        if (this.getVisibility() == View.GONE)            this.setVisibility(View.VISIBLE);        this.removeCallbacks(this);        post(this);    }    private void stopScroll() {        handler.removeCallbacks(this);    }}

布局檔案中呀使用自訂控制項了:

<com.example.cameratestdemo.Util        android:id="@+id/tv_text"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:ellipsize="marquee"        android:focusable="true"        android:focusableInTouchMode="true"        android:marqueeRepeatLimit="marquee_forever"        android:scrollHorizontally="true"        android:singleLine="true"        android:text="路漫漫其修遠兮,吾將上下而求索"        android:textColor="#00ff00"        android:textSize="28sp" >    </com.example.cameratestdemo.Util>

源碼下載:

http://yunpan.cn/c3kL7ILLL7tCt  訪問密碼 bd75

歡迎提出意見,希望可以給大家帶來協助,謝謝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.