Android垂直方向滚动的跑马灯,带gif

效果图:

Android垂直方向滚动的跑马灯,带gif

直接上代码:

[java] view plain copy
  1. /************************************************************************************************* 
  2.  * 作   者: 高永好 
  3.  * 完成日期:2017-06-19 11:05 
  4.  * 说明: 
  5.  ************************************************************************************************/  
  6.   
  7. public class VerticalScrollTextView extends TextView {  
  8.     int perLineCount = 20;  
  9.     int currentNum = 0;  // currentNum*perLineCount =一行(为 了慢慢移动把一行分成了perLineCount份)  
  10.     int totalLine = 0;  
  11.     int lineHeight;  
  12.     int totalHeight;  
  13.     int i=0;  
  14.     boolean canScroll = true;  
  15.     Handler mhander = new Handler() {  
  16.         @Override  
  17.         public void handleMessage(Message msg) {  
  18.             super.handleMessage(msg);  
  19.             //循环滚动  
  20.             scrollTo(0, lineHeight * currentNum);  
  21.         }  
  22.     };  
  23.   
  24.     public VerticalScrollTextView(Context context) {  
  25.         super(context);  
  26.         init();  
  27.     }  
  28.   
  29.     public VerticalScrollTextView(Context context, @Nullable AttributeSet attrs) {  
  30.         super(context, attrs);  
  31.         init();  
  32.     }  
  33.   
  34.     public VerticalScrollTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {  
  35.         super(context, attrs, defStyleAttr);  
  36.         init();  
  37.     }  
  38.   
  39.     public void init() {  
  40.         new Thread(new Runnable() {  
  41.             @Override  
  42.             public void run() {  
  43.                 while (true) {  
  44.                     if (!canScroll) {  
  45.                         continue;  
  46.                     }  
  47.                     if (totalHeight != 0) {  
  48.                         currentNum = currentNum + 1;  
  49.                         if (currentNum == (totalLine-3)*perLineCount) {  
  50.                             currentNum = 0;  
  51.                         }  
  52.                         Message message = mhander.obtainMessage();  
  53.                         message.arg1 = currentNum;  
  54.                         message.sendToTarget();  
  55.                         try {  
  56.                             if (currentNum==0){  
  57.                                 Thread.sleep(1500);  
  58.                             }else  
  59.                             Thread.sleep(50);  
  60.                         } catch (InterruptedException e) {  
  61.                             e.printStackTrace();  
  62.                         }  
  63.                     }  
  64.                 }  
  65.             }  
  66.         }).start();  
  67.     }  
  68.   
  69.     @Override  
  70.     protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
  71.         super.onSizeChanged(w, h, oldw, oldh);  
  72.         currentNum = 0;  
  73.         postInvalidate();  
  74.         //获取行高  
  75.         lineHeight = getLineHeight() / perLineCount;  
  76.         //获取总共的行数  
  77.         totalLine = getLineCount();  
  78.         //获取总共的高度  
  79.         totalHeight = getLineCount() * lineHeight*perLineCount;  
  80.         //获取控件高度  
  81.         int height =getMeasuredHeight();  
  82.         i = (int) (height / getTextSize());  
  83.         if (totalLine <= i) {  
  84.             canScroll = false;  
  85.         }else {  
  86.             canScroll=true;  
  87.         }  
  88.     }  
  89. }  

支持的话就点个赞吧!