android 跑马灯实现

来源:互联网 发布:程序员接活网 编辑:程序博客网 时间:2024/04/29 18:17

必须设置这2个,否则没效果!

android:singleLine="true"      

android:focusableInTouchMode="true"


网上有的根本不能实现效果。莫非和版本有关?


另外:可参考http://www.eoeandroid.com/forum.php?mod=viewthread&tid=165922&reltid=183394&pre_thread_id=0&pre_pos=2&ext=

文本框内容文本的“跑马灯”效果在有些地方很有用。通过搜索学习,附件的源码把“跑马灯”的几种做法归纳在一起,供大家参考!!!!!!

下面是我的简单实现

XML定义:

默认的TextView

<TextView
        android:id="@+id/marqueebyxml"
        android:layout_width="5dip"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:singleLine="true"        
        android:layout_marginLeft="10dip"      
        android:focusableInTouchMode="true"
        android:text="这是通过XML文件设置的效果!" />
   
  自定义:
  
 <com.wletv.MarqueeText
    android:id="@+id/AMTV1" 
    android:layout_width="100dip"
    android:layout_height="wrap_content"        
    android:layout_marginLeft="80dip"    
    android:textSize="25sp"        
    android:textColor="@android:color/black" 
    android:lines="1"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:scrollHorizontally="true"  
    android:marqueeRepeatLimit="marquee_forever"  
    android:ellipsize="marquee" 
    android:background="#2FFFFFFF"
    android:singleLine="true"
    android:text="这才是真正的文字跑马灯效果,文字移动速度,文字移动方向,文字移动的样式,动画等等……"
    />


package com.wletv;


import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;


public class MarqueeText extends TextView {
public MarqueeText(Context con) {
  super(con);
}


public MarqueeText(Context context, AttributeSet attrs) {
  super(context, attrs);
}
public MarqueeText(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
}
@Override
public boolean isFocused() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
   Rect previouslyFocusedRect) {  
}
}