您当前的位置: 首页 > 

蓝不蓝编程

暂无认证

  • 0浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

安卓实现单行文字跑马灯效果

蓝不蓝编程 发布时间:2019-08-07 09:43:47 ,浏览量:0

背景

按照产品设计,文字过长时,需要采用跑马灯显示. 如果是多行文字上下左右切换的跑马灯,可以参考《安卓实现多行文字跑马灯效果》. @ 实现效果图

实现方案
class MarqueeTextView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null)
    : AppCompatTextView(context, attrs) {

    init {
        //设置单行
        setSingleLine()
        //设置Ellipsize
        ellipsize = TextUtils.TruncateAt.MARQUEE
        //获取焦点
        isFocusable = true
        //走马灯的重复次数,-1代表无限重复
        marqueeRepeatLimit = -1
        //强制获得焦点
        isFocusableInTouchMode = true
    }

    /*
     *这个属性这个View得到焦点,在这里我们设置为true,这个View就永远是有焦点的
     */
    override fun isFocused(): Boolean {
        return true
    }

    /*
     * 用于EditText抢注焦点的问题
     * */
    override fun onFocusChanged(focused: Boolean, direction: Int, previouslyFocusedRect: Rect?) {
        if (focused) {
            super.onFocusChanged(focused, direction, previouslyFocusedRect)
        }
    }

    /*
     * Window与Window间焦点发生改变时的回调
     * */
    override fun onWindowFocusChanged(hasWindowFocus: Boolean) {
        if (hasWindowFocus) {
            super.onWindowFocusChanged(hasWindowFocus)
        }
    }
}
完整源代码

https://gitee.com/cxyzy1/marqueeView.git

附录
  1. 安卓实现多行文字跑马灯效果
  2. 参考资料: Android之跑马灯详解 Android中实现跑马灯效果
关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0732s