Android TextView 文字跑马灯效果实现

698 阅读1分钟

我正在参加「掘金·启航计划」

在Textview设置的宽度有限,而需要显示的文字又比较多的情况下,往往需要给Textview设置跑马灯效果才能让用户完整地看到所有设置的文字,所以给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"
        />

在这里插入图片描述

上面这种可能会卡顿

方式二:重写TextView

<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>
  • 新建一个ScrollingTextView类重写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"

即可.