您的位置:首页 > 移动开发 > Android开发

Android重写TextView实现文字跑马灯效果

2013-11-28 15:18 876 查看
/**
* 重写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"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: