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


首先继承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就可以了。



更多相关文章

  1. Android开发便签3:TextView的自动检测文本中的超链接和自动补全
  2. Android源码修改后的语音录音代码
  3. webkit中的javascript和android交互
  4. SQLiteException: no such column:好
  5. android中引入okhttp
  6. WKWebView的JS与Native的交互,适配iOS和andriod的HTML代码
  7. Android基于自带的DownloadManager实现下载功能示例
  8. Android应用程序组件Content Provider的启动过程源代码分析(7)

随机推荐

  1. “小而美”的数据治理实践
  2. 什么是Python爬虫?大部分人都不懂!
  3. Flutter从入门到进阶 实战携程网App
  4. 前端技术分享:Nginx负载均衡视频,基础的实
  5. 苹果Mac好用的录屏软件:Filmage Screen
  6. Java企业级电商项目架构演进之路 Tomcat
  7. linux系统执行读取jar包同级目录的外部配
  8. Linux常用文件类型有哪些?六大类!
  9. 永久免费!国产操作系统 Deepin V20 Beta版
  10. 为什么写爬虫用Python语言?原因很简单!