/**
* 重写TextView实现文字跑马灯效果
*/
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
/**@Discription:
* @author Rose
*/
public class MarqueeTextView extends TextView{
private boolean scroll=false;
/**
* @param context
* @param attrs
*/
public MarqueeTextView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
if(focused)
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
@Override
public void onWindowFocusChanged(boolean hasWindowFocus) {
if(hasWindowFocus)
super.onWindowFocusChanged(hasWindowFocus);
}
@Override
public boolean isFocused() {
return scroll;
}
/**
* @return the scroll
*/
public boolean isScroll() {
return scroll;
}
/**
* @param scroll the scroll to set
*/
public void setScroll(boolean scroll) {
this.scroll = scroll;
}
}
第一步:
1.实现以上的MarqueeTextView.
2.通过setScroll(true)来开启跑马灯。
第二步 :
1.xml中Text修改为
<com.包名.MarqueeTextView
android:id="@+id/episode"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:gravity="center"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:text="第1集"
android:textColor="@color/white"
android:textSize="16sp" />
其中重要属性有:
<1> android:ellipsize="marquee"
<2>android:marqueeRepeatLimit="marquee_forever"
<3>android:singleLine="true"