实现跑马灯的三种方式:分别是直接用控件、自定义一个HorizontalScrollView和自定义一个TextView。
1 直接使用控件
优点:使用简单
缺点:可扩展性差
使用:直接在XML文件中添加如下代码即可:
<TextView
android:id="@+id/tv_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"
android:singleLine="true"
android:text="春花秋月何时了,往事知多少。问君能有几多愁,恰似一江春水向东流。"
android:textColor="#0000ff"
android:textSize="80sp" />
2 自定义一个HorizontalScrollView
利用水平滑动的ScrollView来实现。
使用:首先自定义一个HorizontalScrollView控件,然后在XML文件中使用即可(注意在XML中使用自定义控件需要控件的全包名)。
第一步:自定义的控件代码如下:
public class HorizontalScorllTextView extends HorizontalScrollView implements Runnable{
int currentScrollX = 0;// 当前滚动的位置
TextView tv;
public HorizontalScorllTextView(Context context) {
super(context);
initView(context);
}
public HorizontalScorllTextView(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
public HorizontalScorllTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView(context);
}
void initView(Context context){
View v = LayoutInflater.from(context).inflate(R.layout.scroll_layout, null);
tv = (TextView)v.findViewById(R.id.tv_video_name);
this.addView(v);
}
public void setText(String text){
tv.setText(text);
startScroll();
}
private void startScroll(){
this.removeCallbacks(this);
post(this);
}
@Override
public void run() {
currentScrollX ++;// 滚动速度
scrollTo(currentScrollX, 0);
if (currentScrollX >= tv.getWidth()) {
scrollTo(0, 0);
currentScrollX = 0;
}
postDelayed(this, 50);
}
}
第二步:在XML中使用时如下
<com.example.marqueedemo.utils.MarqueeTextView
android:id="@+id/marqueeTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:singleLine="true"
android:text=""
android:textColor="#0f0"
android:textSize="80dp" />
3 使用自定义的TextView来实现
使用:首先自定义一个TextView控件,然后在XML文件中使用即可(注意在XML中使用自定义控件需要控件的全包名)。
第一步,自定义的控件代码如下:
public class MarqueeTextViewNew extends TextView implements Runnable {
private int screenWidth = 80;
private int currentScrollX = -screenWidth;// 当前滚动的位置
private boolean isStop = false;
private int textWidth;
private boolean isMeasure = false;
public MarqueeTextViewNew(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public MarqueeTextViewNew(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MarqueeTextViewNew(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (!isMeasure) {// 文字宽度只需获取一次就可以了
getTextWidth();
isMeasure = true;
}
}
/**
* 获取文字宽度
*/
private void getTextWidth() {
Paint paint = this.getPaint();
String str = this.getText().toString();
textWidth = (int) paint.measureText(str);
}
@Override
public void run() {
// currentScrollX -= 1;// 滚动速度,方向:从左向右
currentScrollX += 1;// 滚动速度 方向:从右向左
scrollTo(currentScrollX, 0);
if (isStop) {
return;
}
if (currentScrollX > textWidth) {
currentScrollX = -screenWidth;
}
// if (getScrollX() <= -(this.getWidth())) {
// scrollTo(textWidth, 0);
// currentScrollX = textWidth;
// }
postDelayed(this, 5);
}
// 开始滚动
public void startScroll() {
isStop = false;
this.removeCallbacks(this);
post(this);
}
// 停止滚动
public void stopScroll() {
isStop = true;
}
// 从头开始滚动
public void startFor0() {
currentScrollX = 0;
startScroll();
}
@Override
public void setText(CharSequence text, BufferType type) {
// TODO Auto-generated method stub
super.setText(text, type);
startScroll();
}
@Override
public void destroyDrawingCache() {
// TODO Auto-generated method stub
super.destroyDrawingCache();
}
}
第二步,在XML中的使用如下:
<com.example.marqueedemo.utils.MarqueeTextViewNew
android:id="@+id/marqueeTextViewNew"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="80sp"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="@android:color/holo_red_light"
android:text="春花秋月何时了,往事知多少。问君能有几多愁,恰似一江春水向东流。"
android:layout_centerHorizontal="true"
/>
总结
上面已将详细代码贴出,比较简单,就不再复述。
网友评论