可控制的启动关闭跑马灯效果
2021/5/18 10:55:15
本文主要是介绍可控制的启动关闭跑马灯效果,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
前言
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee"/>
Android中当文本显示不全时可以通过设置跑马灯方式轮播显示所以文字
但是我们有时希望在指定情况下才启用跑马灯轮播,这就需要重写TextView来实现
自定义跑马灯
public class MarqueeText extends AppCompatTextView { private static final String TAG = "MarqueeText"; public MarqueeText(Context context) { super(context); } public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean isFocused() { return true; }}
如上代码所示,我们通过重写isFocused()方法,使它总是返回true就可以让文字开始轮播显示了
启动关闭轮播
如果一个界面有好多文本需要轮播,如果按照上面的方式实现,则会出现很多文本同时轮播的情况,这样就显得很乱,因此我们需要添加开关来进行控制
public class MarqueeText extends AppCompatTextView { private static final String TAG = "MarqueeText"; private boolean enableFocus = false; private CustomRunnable mCustomRunnable; public MarqueeText(Context context) { super(context); init(); } public MarqueeText(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public MarqueeText(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { mCustomRunnable = new CustomRunnable(this); } public void setEnableFocus(boolean b) { //LogUtils.i(TAG, "setEnableFocus::b = " + b + "::txt = " + getText()); setFocusable(b); enableFocus = b; if (b) { setEllipsize(TextUtils.TruncateAt.MARQUEE); postDelayed(mCustomRunnable, 500); } else { setEllipsize(TextUtils.TruncateAt.END); removeCallbacks(mCustomRunnable);//删除延时runnable post(mCustomRunnable);//触发焦点切换,设置跑马灯循环为0 } } @Override//实现了都获取焦点 public boolean isFocused() { //LogUtils.i(TAG, "isFocused::enableFocus = " + enableFocus + "::txt = " + getText()); return enableFocus; } @Override protected void onDetachedFromWindow() { super.onDetachedFromWindow(); removeCallbacks(mCustomRunnable); } static class CustomRunnable implements Runnable { private WeakReference<MarqueeText> mWeakReference = new WeakReference<>(null); public CustomRunnable(MarqueeText marqueeText) { mWeakReference = new WeakReference<>(marqueeText); } @Override public void run() { if (mWeakReference.get() == null) { return; } MarqueeText marqueeText = mWeakReference.get(); //跑马灯循环次数设置为0则关闭跑马灯,否则设置无穷大 marqueeText.setMarqueeRepeatLimit(marqueeText.enableFocus ? Integer.MAX_VALUE : 0); marqueeText.onWindowFocusChanged(marqueeText.enableFocus); } }}
通过setEnableFocus(boolean b)方法来设置打开\关闭开关
通过setMarqueeRepeatLimit设置跑马灯循环次数打开\关闭跑马灯
通过onWindowFocusChanged刷新通知轮播效果
这篇关于可控制的启动关闭跑马灯效果的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-06-19《2023版Java工程师》课程升级公告
- 2024-06-15matplotlib作图不显示3D图,怎么办?
- 2024-06-1503-Loki 日志监控
- 2024-06-1504-让LLM理解知识 -Prompt
- 2024-06-05做软件测试需要懂代码吗?
- 2024-06-0514-ShardingSphere的分布式主键实现
- 2024-06-03为什么以及如何要进行架构设计权衡?
- 2024-05-31全网首发第二弹!软考2024年5月《软件设计师》真题+解析+答案!(11-20题)
- 2024-05-31全网首发!软考2024年5月《软件设计师》真题+解析+答案!(21-30题)
- 2024-05-30【Java】百万数据excel导出功能如何实现