close
Blogtrottr
海芋小站
提供實用軟體教學,介紹有趣網站及資訊,豐富電腦人生 
Get the newest, latest, hottest

Find shoes, heels, flats, active, essentials and more in the latest styles. Save up to 40% on select items.
From our sponsors
thumbnail [Android 開發] 如何在利用 TextView 做左右滾動的文字跑馬燈?
Nov 22nd 2013, 16:37, by 張海芋


一般我們做跑馬燈,都是使用 WebView 來實做,但是 WebView 的效能並不好,而其實 TextView 就有內建跑馬燈的功能了,但不好控制,所以還是自己寫程式碼來控制是最好的,接下來就讓我們看一下怎麼做吧!

而這篇文章是參考「自定义TextView跑马灯效果,可控制启动,停止,和速度(含源码)」一文改的,主要是原文在特定的情況下會有殘影產生,所以海芋加以改寫,程式碼如下:

public class MarqueeTextView extends TextView implements Runnable 
{
    public enum MarqueeDirection 
    {
        LEFT, RIGHT
    } 
    private volatile boolean  m_bIsStop = false;
    private int      m_iTextWidth = 0;// 文字寬度
    private boolean  m_bIsMeasure = false;
    private int      m_iDistance = 1;      
    private int      m_iSpeed = 25;      
    private int      m_iScrollPosition = 0;

    private MarqueeDirection m_Direction  = MarqueeDirection.RIGHT;         

    public MarqueeTextView(Context context) 
    {
        super(context);
        setEllipsize(TruncateAt.MARQUEE);
        setSingleLine(true);
        onResume();
    }

    @Override
    protected void onDraw(Canvas canvas) 
    {  
        if (!m_bIsMeasure) 
        {  // 文字寬度只要獲取一次就好啦!
            getTextWidth();
            startScroolFrom0();
            m_bIsMeasure = true;
        }  
        super.onDraw(canvas);  // 先設定好再畫,才不會有殘影
    }

    public void onResume()
    {
        startScroll();
    }
 
    public void onPause()
    {
        stopScroll();
    }
 
    private void getTextWidth() 
    {
        Paint paint = getPaint();
        String str = getText().toString();
        m_iTextWidth = (int) paint.measureText(str);
    }

    protected void startScroolFrom0()
    {
        // 跑馬燈初始值方向
        if (m_Direction == MarqueeDirection.RIGHT)
            m_iScrollPosition = -getWidth();
        else
            m_iScrollPosition = m_iTextWidth;
        
        scrollTo(m_iScrollPosition, 0);  
    }
 
    @Override
    public void run() 
    {
        if (m_bIsStop)    
           return;
  
        // 跑馬燈初始值方向
        if (m_Direction == MarqueeDirection.RIGHT)
           m_iScrollPosition += m_iDistance;
        else
           m_iScrollPosition -= m_iDistance;
        scrollTo(m_iScrollPosition,0);
        if ((m_Direction == MarqueeDirection.RIGHT && getScrollX() >= m_iTextWidth) ||  // 由右向左捲
             (m_Direction == MarqueeDirection.LEFT && getScrollX() <= -getWidth()))    // 由左向右捲
        { 
            startScroolFrom0();  
        }
        postDelayed(this, m_iSpeed);
    }
 
    private void startScroll() 
    {
        m_bIsStop = false;
        removeCallbacks(this);
        post(this);
    }

    private void stopScroll() 
    {
        m_bIsStop = true;
        removeCallbacks(this);
    }
}

而以上如果你是程式設計師的話,應該會有一點小幫助,如果不是程式設計師,就略過這篇文章吧!
覺得這篇文章實用嗎?請按讚來分享給更多好朋友知道唷!
喜歡本站的文章嗎?歡迎透過以下方式追蹤本站
版權說明
本文章發佈於海芋小站,內容僅歡迎「部份」引用,引用時請註明出處及原文連結,謝謝。
若圖像無法顯示,可能因流量太大,敬請重新整理或透過留言與我回報,也歡迎「訂閱」本站文章喔,感恩!!
Related Posts with Thumbnails

This entry passed through the Full-Text RSS service — if this is your content and you're reading it on someone else's site, please read the FAQ at fivefilters.org/content-only/faq.php#publishers.

You are receiving this email because you subscribed to this feed at blogtrottr.com.

If you no longer wish to receive these emails, you can unsubscribe from this feed, or manage all your subscriptions
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 jmuko90 的頭像
    jmuko90

    2016【韓劇】奶酪陷阱劇情簡介及人物介紹奶酪陷阱 EP03 預告奶酪陷阱 線上看奶酪陷阱(捕鼠器裡的奶酪) 第1集

    jmuko90 發表在 痞客邦 留言(0) 人氣()