首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android不需要焦点的TextView跑马灯成效

2012-06-28 
Android不需要焦点的TextView跑马灯效果大家可能都尝试过TextView的跑马灯效果,效果很酷,但是有一个缺陷,T

Android不需要焦点的TextView跑马灯效果

大家可能都尝试过TextView的跑马灯效果,效果很酷,但是有一个缺陷,TextView只能在获得焦点的情况下才能显示跑马灯效果。我原先试过重写onDraw()函数,运行成功,但是代码太过烦琐,时间长了自己都懒得看了,一直想找个好的方法把它剃掉,后来无意中遇到下面的方法,给大家推荐一下:
方法依然是重写TextView,但是关键看重写哪里,(后悔当初没好好看就直接重写onDraw()了Android不需要焦点的TextView跑马灯成效):

public class ScrollingTextView extends TextView {     public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }     public ScrollingTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }     public ScrollingTextView(Context context) {        super(context);    }    @Override    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {        if(focused)            super.onFocusChanged(focused, direction, previouslyFocusedRect);    }     @Override    public void onWindowFocusChanged(boolean focused) {        if(focused)            super.onWindowFocusChanged(focused);    }      @Override    public boolean isFocused() {        return true;    } }


关键的关键就是在isFocused函数上,大家有空可以看看TextView的源码,上面很清楚。建议大家没事的时候多看看源码,可以从中学到很多好东西。

使用时如下,比如我的自定义类在包com.wlei.test下面:

<com.wlei.test.ScrollingTextView     android:text="Really Long Scrolling Text Goes Here.... ..... ............ .... ...."    android:singleLine="true"    android:ellipsize="marquee"    android:marqueeRepeatLimit="marquee_forever"    android:scrollHorizontally="true"    android:id="@+id/TextView03"    android:padding="5dip"     android:layout_width="wrap_content"     android:layout_height="wrap_content" />



原文详见:http://androidbears.stellarpc.net/?p=185

?

热点排行