android TextView 垂直自动滚动字幕实现

参考网上一些做法然后进行了修改,

android TextView 垂直自动滚动字幕实现_第1张图片

首先继承TextView

/**
 * VerticalScrollTextView.java
 * 版权所有(C) 2013
 * 创建者:cuiran 2013-12-9 下午2:48:55
 */
package com.cayden.videodemo.view;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.TextView;

/**
 * TODO 
 * @author cuiran
 * @version 1.0.0
 */
public class VerticalScrollTextView extends TextView {

		private float step =0f;   
	    private Paint mPaint=new Paint(); ; 
	    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!"); 
	        }       
	       
	        text=getText().toString();
	        if(text==null|text.length()==0){ 
	               
	        	return ; 
	        }       
	         
	       //下面的代码是根据宽度和字体大小,来计算textview显示的行数。 
	 
	        textList.clear(); 
	        StringBuilder builder =null;
	        for(int i=0;i<text.length();i++){
	        	if(i%12==0){
	        		builder = new StringBuilder(); 
	        	}
	        	 if(i%12<=11){ 
	        		 builder.append(text.charAt(i)); 
	        	 }
	        	 if(i%12==11){ 
	        		 textList.add(builder.toString()); 
	        	 }
	        	 
	        }
	        Log.e("textviewscroll",""+textList.size()); 
	       

	    } 
	 
	 
	    //下面代码是利用上面计算的显示行数,将文字画在画布上,实时更新。 
	     @Override 
	    public void onDraw(Canvas canvas) { 
	       if(textList.size()==0)  return; 
	       
	       mPaint.setTextSize(40f);//设置字体大小
	        for(int i = 0; i < textList.size(); i++) { 
	            canvas.drawText(textList.get(i), 0, this.getHeight()+(i+1)*mPaint.getTextSize()-step+30, mPaint); 
	        } 
	        invalidate();    
	        
	        step = step+0.3f; 
	        if (step >= this.getHeight()+textList.size()*mPaint.getTextSize()) { 
	            step = 0; 
	        }         
	    } 
	 
}

然后写布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#eee"
    android:orientation="vertical" >

    <com.cayden.videodemo.view.VerticalScrollTextView                  
			android:layout_width="fill_parent" 
            android:layout_height="fill_parent"    
            android:textSize="20dp" 
            android:text="好雨知时节,当春乃发生.随风潜入夜,润物细无声.野径云俱黑,江船火独明.晓看红湿处,花重锦官城."/> 

</LinearLayout>

然后自己写个Activity就可以了。



你可能感兴趣的:(java,android,canvas,textview)