跑马灯效果实现(多跑马灯, 自定义响应触摸,点击,可设置速度等属性跑马灯)

因为业务需求,需要设置文字的跑马灯效果,查阅相关资料后发现资料比较散乱,现整理如下:

1.单跑马灯效果(系统自带TextView相关属性设置)
2.多跑马灯效果(简单自定义控件)
3.可调节跑马灯效果(自定义控件)

单跑马灯效果

在TextView中设置相关属性即可:


  • Tip: ellipsize, marqueeRepeatLimit, focusable, focusableInTouchMode, singleLine属性需如上设置
  • 优点: 简单,直接调用系统控件
  • 缺点: 因为焦点获取问题多TextView时只有一个有滚动效果,不支持相关属性设置,速度,刷新频率,延迟,点击和触摸事件等

多跑马灯效果

自定义TextView

package com.junseek.zhuikemarketing;
import android.content.Context;
import android.util.AttributeSet;import android.widget.TextView;
/** 
  * @ author      Qsy 
  * @ date        16/8/12 下午8:00 
  * @ description 跑马灯,可多焦点并行 
  */
public class MarqueeTextView extends TextView {
  public MarqueeTextView(Context context) {
    super(context); 
  }

  public MarqueeTextView(Context context, AttributeSet attrs) {        
    super(context, attrs);    
  }    

  public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {        
    super(context, attrs, defStyle);    
  }    

  @Override    
  public boolean isFocused() {        
    return true;    
  }
}

  • Tip: ellipsize,marqueeRepeatLimit,singleLine属性需如上设置
  • 优点: 简单继承TextView,效果基本等同TextVIew效果,可多个控件同时执行滚动效果
  • 缺点: 不支持相关属性设置,速度,刷新频率,延迟,点击和触摸事件等

可调节跑马灯效果


  • Tip: 需要设置ellipsize,singleLine属性,设置gravity=center属性时文字消失(原因待测)
  • 优点: 支持设置控件初始化时候延迟滚动,刷新速度,滚动速度,暂停滚动,开始滚动,点击暂停,再次点击开始,拖动控件时响应水平滑动
  • 缺点: 相对原生控件需要文字完全滚出控件左边界后右边界才回进入新的文字(待优化,TextView中不必等文字完全滚出左边界),需要较多的自定义内容,内存消耗较大(待测)

你可能感兴趣的:(跑马灯效果实现(多跑马灯, 自定义响应触摸,点击,可设置速度等属性跑马灯))