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

2016-08-23  本文已影响1849人  Qsy_Zer0

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

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

单跑马灯效果

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

<TextView    
  android:layout_width="wrap_content"    
  android:layout_height="wrap_content"    
  android:ellipsize="marquee"    
  android:marqueeRepeatLimit="marquee_forever"    
  android:focusable="true"    
  android:focusableInTouchMode="true"    
  android:singleLine="true"    
  android:text="是时候表演真正的跑马灯了1 2 3 4 5 6 7 8 9 10" />

多跑马灯效果

自定义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;    
  }
}
<TextView    
  android:layout_width="wrap_content"    
  android:layout_height="wrap_content"    
  android:ellipsize="marquee"    
  android:marqueeRepeatLimit="marquee_forever"    
  android:singleLine="true"    
  android:text="是时候表演真正的跑马灯了1 2 3 4 5 6 7 8 9 10" />

可调节跑马灯效果

<TextView    
  android:layout_width="fill_parent"    
  android:layout_height="wrap_content"    
  android:ellipsize="none"    
  android:singleLine="true"    
  android:text="是时候表演真正的跑马灯了1 2 3 4 5 6 7 8 9 10"    />
上一篇 下一篇

猜你喜欢

热点阅读