2025-05-17

textview 垂直自動滾動字幕實現

前段時間,在網上搜到一個帖子,textview 水平自動滾動字幕的實現,今日項目需要垂直滾動字幕。其實現原理和水品一樣。都是重寫textview的onDraw方法。
實現垂直自動滾動字幕需要2點需要註意
1.需要根據textview的寬度和字體的大小計算滾動字幕有多少行。實現這個功能,需要重新寫兩個方法:1.onMeasure,2.onDraw.因為需要獲取textview的寬度,於是需要在onMeasure方法裡面調用如下代碼。具體方法如下:

[java]
package com.test; 
public VerticalScrollTextView extends TextView { 
    private float step =0f;   
    private Paint mPaint; 
    private String text; 
    private float width; 
    private List<String> textList = new ArrayList<String>();    //分行保存textview的顯示信息。 
 
    public VerticalScrollTextView(Context context, AttributeSet attrs) { 
        super(context, attrs);         
    } 
     
 
    public VerticalScrollTextView(Context context) { 
        super(context);         
    } 
     
    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {         
        super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
        width = MeasureSpec.getSize(widthMeasureSpec);    
              final int widthMode = MeasureSpec.getMode(widthMeasureSpec);   
        if (widthMode != MeasureSpec.EXACTLY) {    
            throw new IllegalStateException("ScrollLayout only canmCurScreen run at EXACTLY mode!"); 
        }       
       
        float length = 0;         
        if(text==null|text.length()==0){ 
                return ; 
        }       
         
            //下面的代碼是根據寬度和字體大小,來計算textview顯示的行數。 
 
        textList.clear(); 
         
        StringBuilder builder = new StringBuilder(); 
        for(int i=0;i<text.length();i++){ 
            Log.e("textviewscroll",""+i+text.charAt(i)); 
            if(length<width){ 
                builder.append(text.charAt(i)); 
                length += mPaint.measureText(text.substring(i, i+1)); 
                if(i==text.length()-1){ 
                    Log.e("textviewscroll",""+i+text.charAt(i)); 
                    textList.add(builder.toString()); 
                } 
            }else{ 
                textList.add(builder.toString().substring(0,builder.toString().length()-1)); 
            builder.delete(0, builder.length()-1) ; 
            length= mPaint.measureText(text.substring(i, i+1)); 
            i–; 
            } 
             
        } 
    } 
 
 
    //下面代碼是利用上面計算的顯示行數,將文字畫在畫佈上,實時更新。 
     @Override 
    public void onDraw(Canvas canvas) { 
       if(textList.size()==0)  return; 
       for (int i = 0; i < textList.size(); i++) { 
            canvas.drawText(textList.get(i), 0, this.getHeight()+(i+1)*mPaint.getTextSize()-step, getPaint()); 
        } 
             
        invalidate();     
        step = step+0.3f; 
        if (step >= this.getHeight()+textList.size()*mPaint.getTextSize()) { 
            step = 0; 
        }         
    } 
 

還有一些擴展功能沒有加,比方說滾動的速度設定,滾動多長時間就停止滾動。

發上此帖,有任何建議的朋友,歡迎留言,共同討論。
具體調用方法 寫一個layout文件。如

[html]
<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
            android:layout_width="300dp" 
            android:layout_height="400dp"            
            android:orientation="vertical">  
<pre name="code" class="html"><pre name="code" class="html"><com.test.VerticalScrollTextView                  
<pre name="code" class="html">                      android:layout_width="200dp" 
            android:layout_height="300dp"    
                    android:textSize="20dp" 
                    android:text="好雨知時節, 當春乃發生。隨風潛入夜, 潤物細無聲。野徑雲俱黑, 江船火獨明。曉看紅濕處, 花重錦官城"/> 
</LinearLayout> 

   

摘自  fengyoujie的專欄
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *