Android之跑马灯

跑马灯在项目了其实应用的还比较多,特别是做多媒体的时候,音乐视频蓝牙等等经常用到。

比如音乐的专辑信息,蓝牙通话记录,以及视频列表等等...。

不好直接上项目效果图所以就做了一个简单的Demo放手机上显示效果了。

单行跑马灯效果

做起来也很简单,在布局文件里面设置几个属性就OK.

先上效果图:


4149864-92601cfce88029f1.gif
18e4017761df3b42cdf56619f51dcdf1.gif

设置如下。

//设置为跑马灯显示

android:ellipsize="marquee"

//获取焦点

android:focusable="true"

//可以通过toucth来获得focus

android:focusableInTouchMode="true"

//单行显示文字

android:singleLine="true"

//设置重复的次数
android:marqueeRepeatLimit="marquee_forever"

  <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen" />

多行跑马灯效果图

其实主要是想讲下多行跑马灯,首先直接上图:


4149864-4f8a76aae1c9f65b.gif
68810ab5960062cbb36f7d7383a30e6f.gif

如果要设置多行的话

首先我们写一个类,继承TextView这个类,实现它的构造方法,重写isFocused()方法 ,将它的返回值都为true

如果有弹窗之类的导致无作用的话我们可以直接重写onWindowFocusChanged()方法,将super方法屏注释掉。

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;
 
public class MarqueeTextView extends TextView{
 
    public MarqueeTextView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }
 
    public MarqueeTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }
 
    public MarqueeTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }
 
    @Override
    public boolean isFocused() {
        // TODO Auto-generated method stub
        return true;
    }
    @Override
    protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
        // TODO Auto-generated method stub
//      super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }
    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        // TODO Auto-generated method stub
//      super.onWindowFocusChanged(hasWindowFocus);
    }
 
}
 

核心代码就是isFocused()和onWindowFocusChanged()方法。

布局文件如下:

其实就和前面是一样的 当然也可以在代码里面设置这些属性,布局里面就不用设置了。

    <combd.textviewmarquee.MarqueeTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ellipsize="marquee"
        android:focusable="true"
        android:focusableInTouchMode="true"
        android:singleLine="true"
        android:marqueeRepeatLimit="marquee_forever"
        android:text="eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen.eason.chen" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值