zoukankan      html  css  js  c++  java
  • 可以一直滚动的跑马灯

     1 public class AutoScrollTextView extends TextView implements Runnable {
     2     private int currentScrollX;// 当前滚动的位置
     3     private boolean isStop = false;
     4     private int textWidth;
     5     private boolean isMeasure = false;
     6 
     7     public AutoScrollTextView(Context context) {
     8         super(context);
     9     }
    10 
    11     public AutoScrollTextView(Context context, AttributeSet attrs) {
    12         super(context, attrs);
    13     }
    14 
    15     public AutoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
    16         super(context, attrs, defStyle);
    17     }
    18 
    19     @Override
    20     protected void onDraw(Canvas canvas) {
    21         super.onDraw(canvas);
    22         if (!isMeasure) {// 文字宽度只需获取一次就可以了
    23             getTextWidth();
    24             isMeasure = true;
    25         }
    26     }
    27 
    28     /**
    29      * 获取文字宽度
    30      */
    31     private void getTextWidth() {
    32         Paint paint = this.getPaint();
    33         String str = this.getText().toString();
    34         textWidth = (int) paint.measureText(str);
    35     }
    36 
    37     @Override
    38     public void run() {
    39         currentScrollX -= 1;// 滚动速度
    40         scrollTo(currentScrollX, 0);
    41         if (isStop) {
    42             return;
    43         }
    44         if (getScrollX() <= -(this.getWidth())) {
    45             scrollTo(textWidth, 0);
    46             currentScrollX = textWidth;
    47         }
    48         postDelayed(this, 5);
    49     }
    50 
    51     // 开始滚动
    52     public void startScroll() {
    53         isStop = false;
    54         this.removeCallbacks(this);
    55         post(this);
    56     }
    57 
    58     // 停止滚动
    59     public void stopScroll() {
    60         isStop = true;
    61     }
    62 
    63     // 从头开始滚动
    64     public void startFor0() {
    65         currentScrollX = 0;
    66         startScroll();
    67     }
    68 }
  • 相关阅读:
    jar
    8月21日23:38
    WPF之UI虚拟化
    (转)Windows系统白名单以及UAC机制
    C#获取文件版本信息
    命名实体识别,使用pyltp提取文本中的地址
    (转载)完成端口(Completion Port, I/OCP)详解
    全国各城市地名抓取,包含街道、村落、小区、商店、景点等
    关于Python打包运行的一些思路
    关于批判性思维(Critical Thinking)
  • 原文地址:https://www.cnblogs.com/yiludugufei/p/4710084.html
Copyright © 2011-2022 走看看