TextView自定义跑马灯效果
首先,使用TextView实现走马灯形式的滚动显示,只需要对其设置两个属性:
android:ellipsize="marquee"android:marqueeRepeatLimit="marquee_forever"
但是,TextView的滚动显示,有一个前提,TextView需要必须处于focus状态。当TextView失去焦点的时候,TextView将会停止滚动。如何实现无限滚动,当然也需要从焦点入手。当然,直接requestFocus()是不行的,这里我使用了另外一个方法。观察到textView有一个名为isFocused()的方法,文档中的注释是这样的:
/***Returnstrueifthisviewhasfocus**@returnTrueifthisviewhasfocus,falseotherwise.*/
也就是说当TextView拥有焦点的时候会返回true.同时可以发现,TextView中很多地方都是直接调用这个方法作为判断条件,最关键的,这个方法被声明为public! ok, 实现方法已经初现端倪了!
做法是这样:比如,现在写一个类命名为一个AlwaysMarqueeTextView, 继承自TextView, 同时override isFocused()方法,并使其返回值为true, 样例如下:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;
/**
* 自定义TextView跑马灯的效果
*
* @作者: 刘倩</br>
* @时间 : 2014年5月29日 下午1:16:13</br>
* @描述: 主要定义TextView的跑马灯效果</br>
*/
public class AlwaysMarqueeTextView extends TextView
{
public AlwaysMarqueeTextView(Context context)
{
super(context);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public AlwaysMarqueeTextView(Context context, AttributeSet attrs,
int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public boolean isFocused()
{
return true;
}
}
更多相关文章
- Android(安卓)Launcher源码研究(二) 加载app流程1
- Android(安卓)Java生成随机数的方法
- Android(安卓)PathMeasure的使用详解
- [转载]Android(安卓)ContentProvider和Uri详解 (绝对全面)
- Android中对/data/data//files下文件的读写操作
- Android(安卓)RecyclerView多布局的实现
- cocos2d-js如何在android平台上使用js直接调用Java方法
- Android(安卓)使用Pull方法解析XML文件的方法
- Android中的信使Messenger的源码解析