Android中TextView不获取焦点可以实现跑马灯的效果
2017-06-24 00:00
615 查看
之前在网上找了很多关于TextView的跑马灯效果实现的例子,实现起来都存在一些问题,例如一种是完全重画一个跑马灯,还有就是只设置TextView的相关属性使其具有跑马灯的效果,总的来说这两种方法都是可行的,但是都有其不足之处,第一种太复杂,实现起来比较麻烦,第二种呢,它只能在TextView获得焦点的时候才有跑马灯的效果,这样有时候并不能达到我们所要求的效果。我通过网上的一些例子自己在做了一些改动,就实现了现在不用获取焦点也能“跑”起来的效果。
首先,写一个类,让其继承自TextView:
然后再将我们已经写好的这个控件(MarqueeText)放到布局文件中,例如main.xml:
关于MarqueeText类中为什么要复写onFocusChanged()方法,那是因为如果不写,在Textview 获得焦点后,再失去焦点时字就会停止“跑”了,所以如果想让它一直跑下去就复写onFocusChanged(),并且里面什么也不做(主要是不能调用父类的方法)。
首先,写一个类,让其继承自TextView:
public class MarqueeText extends TextView { public MarqueeText(Context con) { super(con); } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); } public MarqueeText(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean isFocused() { return true; } @Override protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) { } }
然后再将我们已经写好的这个控件(MarqueeText)放到布局文件中,例如main.xml:
<!-- 在布局文件中用自己写的控件只需要写类的全名就行,如下com.example这是包名,后面再跟类名就行了 -->
<com.example.MarqueeText android:id="@+id/AMTV1" android:layout_width="400dip" android:layout_height="wrap_content" android:layout_marginLeft="80dip" android:textSize="25sp" android:textColor="@android:color/black" android:lines="1" android:focusable="true" android:focusableInTouchMode="true" android:scrollHorizontally="true" android:marqueeRepeatLimit="marquee_forever" android:ellipsize="marquee" android:background="#2FFFFFFF" android:text="这才是真正的文字跑马灯效果,文字移动速度,文字移动方向,文字移动的样式,动画等等……" />
关于MarqueeText类中为什么要复写onFocusChanged()方法,那是因为如果不写,在Textview 获得焦点后,再失去焦点时字就会停止“跑”了,所以如果想让它一直跑下去就复写onFocusChanged(),并且里面什么也不做(主要是不能调用父类的方法)。
相关文章推荐
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android:TextView不获取焦点可以实现跑马灯的效果
- Android中TextView不获取焦点可以实现跑马灯的效果
- Android基于TextView不获取焦点实现跑马灯效果
- android TextView不用获得焦点实现跑马灯效果 两种方法
- android:TextView实现文字走马灯效果(欺骗系统获取持久的焦点)
- TextView实现跑马灯效果,不用获取焦点
- AndroidTextView的跑马灯效果,解决复杂布局不能获取焦点的问题
- Android自定义不需要焦点的TextView以实现跑马灯效果
- Android TextView的跑马灯(不获取焦点情况下也可以跑)
- Android实现多个TextView同时显示跑马灯效果
- android 倒计时、有清除功能编辑框、popupwindow、跑马灯效果实现(基于TextView)
- Android实现多个TextView同时显示跑马灯效果
- Android TextView实现跑马灯效果
- Android中TextView实现垂直滚动(轮换效果,非跑马灯)
- Android实现多个TextView同时显示跑马灯效果
- Android实现多个跑马灯效果,多个文本框TextView的跑马灯
- Android TextView实现跑马灯效果
- android用TextView实现跑马灯效果