Android TextView 文字跑马灯效果实现

文字跑马灯效果

TextView 的一些属性:

android:ellipsize=“marquee”

  • 以动画横向移动的方式显示,一直是动态的滚播形式

android:marqueeRepeatLimit

  • 设置重复滚动的次数,当设置为marquee_forever时表示无限次。

android:scrollHorizontally

  • 设置文本超出TextView的宽度的情况下,是否出现横拉条。

android:focusable

  • 在控件得到焦点(被点击)后触发事件。

android:focusableInTouchMode

  • 在程序运行开始的时候,无需获取焦点(不需被点击)即可触发事件。

android:singleLine=“true”

  • TextView单行显示不换行

其他属性

android:ellipsize=“start”

  • 省略号显示在开头,即显示最后面文字,前面省略

android:ellipsize=“end”

  • 省略号显示在结尾,即显示最前面文字,后面省略

android:ellipsize=“middle”

  • 省略号显示在中间,显示开头和结尾文字,中间省略

示例:

	<TextView
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:text="djshduuhsudhujufhudjsfreesrdesrdjshduuhsudhujufhudjsfreesrdesrdjshduuhsudhujufhudjsfreesrdesrdjshduuhsudhujufhudjsfreesrdesrdjshduuhsudhujufhudjsfreesrdesrdjshduuhsudhujufhudjsfreesrdesr"
        android:textSize="50dp"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:focusable="false"
        android:scrollHorizontally="true"
        />

在这里插入图片描述
上面这种可能会卡顿

方式二:

<LinearLayout
            android:textSize="@dimen/sp_12"
            android:layout_centerVertical="true"
            android:layout_marginStart="@dimen/dp_100"
            android:layout_width="match_parent"
            android:layout_marginEnd="@dimen/dp_10"
            android:layout_height="@dimen/dp_15">
            <com.board.utils.ScrollingTextView
                android:id="@+id/scrollingtext"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:ellipsize="marquee"
                android:marqueeRepeatLimit="marquee_forever"
                android:singleLine="true"
                android:textSize="@dimen/sp_12"/>
        </LinearLayout>
  • 重写TextView
@SuppressLint("AppCompatCustomView")
public class ScrollingTextView extends TextView {

    public ScrollingTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public ScrollingTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public ScrollingTextView(Context context) {
        super(context);
    }

    @Override
    protected void onFocusChanged(boolean focused, int direction,
                                  Rect previouslyFocusedRect) {
        if (focused)
            super.onFocusChanged(focused, direction, previouslyFocusedRect);
    }

    @Override
    public void onWindowFocusChanged(boolean focused) {
        if (focused)
            super.onWindowFocusChanged(focused);
    }

    @Override
    public boolean isFocused() {
        return true;
    }

}
  • 实现
     ScrollingTextView scrollingtext=findViewById<TextView>(R.id.scrollingtext)
     scrollingtext.text="adssadad......adasd"

即可.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AaVictory.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值