Android实现文字滚动播放效果的示例代码

在 Android 中实现文字滚动播放效果,可以使用 TextView 结合 Marquee 属性,或者自定义 View 实现更灵活的滚动效果。以下是几种实现方式:

图片[1]_Android实现文字滚动播放效果的示例代码_知途无界

方法 1:使用 TextViewmarquee 属性(简单滚动)

适用于简单的单行文字滚动效果。

XML 布局

<TextView
    android:id="@+id/scrollingTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"  <!-- 单行显示 -->
    android:ellipsize="marquee"  <!-- 滚动模式 -->
    android:marqueeRepeatLimit="marquee_forever"  <!-- 无限循环 -->
    android:focusable="true"  <!-- 必须设置 focusable -->
    android:focusableInTouchMode="true"  <!-- 必须设置 focusableInTouchMode -->
    android:text="这是一段很长的文字,用于测试滚动效果,如果文字太短则不会滚动..."
    android:textSize="18sp"
    android:textColor="#FF0000" />

Java/Kotlin 代码

// Java
TextView scrollingTextView = findViewById(R.id.scrollingTextView);
scrollingTextView.setSelected(true);  // 必须调用 setSelected(true) 才能滚动
// Kotlin
val scrollingTextView = findViewById<TextView>(R.id.scrollingTextView)
scrollingTextView.isSelected = true  // 必须调用 isSelected = true 才能滚动

效果

  • 文字会从右向左滚动,如果文字长度超过 TextView 宽度,则会自动滚动。
  • 如果文字较短,默认不会滚动,必须设置 setSelected(true) 才能触发滚动。

方法 2:自定义 View 实现平滑滚动(更灵活)

如果 marquee 效果不够灵活(如需要自定义速度、方向等),可以自定义 View 实现滚动。

自定义 ScrollingTextView

// Java
public class ScrollingTextView extends TextView {
    private float scrollX = 0;
    private float speed = 1f;  // 滚动速度(像素/帧)
    private boolean isScrolling = true;

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

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

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

    @Override
    protected void onDraw(Canvas canvas) {
        if (isScrolling) {
            scrollX -= speed;  // 向左滚动
            if (scrollX <= -getLayout().getLineWidth(0)) {
                scrollX = getWidth();  // 滚动到末尾时重置
            }
            canvas.translate(scrollX, 0);  // 平移画布
        }
        super.onDraw(canvas);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (scrollX == 0) {
            scrollX = getWidth();  // 初始位置
        }
    }

    public void setScrollSpeed(float speed) {
        this.speed = speed;
    }

    public void stopScrolling() {
        isScrolling = false;
    }

    public void startScrolling() {
        isScrolling = true;
        invalidate();  // 触发重绘
    }
}

XML 布局

<com.example.ScrollingTextView
    android:id="@+id/customScrollingTextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:text="这是一段很长的文字,用于测试自定义滚动效果,可以调整速度和方向..."
    android:textSize="18sp"
    android:textColor="#00FF00" />

Java/Kotlin 代码

// Java
ScrollingTextView customScrollingTextView = findViewById(R.id.customScrollingTextView);
customScrollingTextView.setScrollSpeed(2f);  // 设置滚动速度
customScrollingTextView.startScrolling();   // 开始滚动
// Kotlin
val customScrollingTextView = findViewById<ScrollingTextView>(R.id.customScrollingTextView)
customScrollingTextView.scrollSpeed = 2f  // 设置滚动速度
customScrollingTextView.startScrolling()  // 开始滚动

效果

  • 可以自定义滚动速度、方向(修改 scrollX 的计算方式)。
  • 适用于更复杂的滚动需求。

方法 3:使用 ValueAnimator 实现平滑滚动(推荐)

如果希望更流畅的滚动效果,可以使用 ValueAnimator 动态调整 TextViewtranslationX

Kotlin 示例

val textView = findViewById<TextView>(R.id.textView)
val screenWidth = resources.displayMetrics.widthPixels

// 计算需要滚动的总距离(文字宽度 + TextView 宽度)
val textWidth = textView.paint.measureText(textView.text.toString())
val totalDistance = textWidth + screenWidth

// 使用 ValueAnimator 实现平滑滚动
ValueAnimator.ofFloat(0f, -totalDistance).apply {
    duration = (totalDistance / 30 * 1000).toLong()  // 根据速度调整时间
    repeatCount = ValueAnimator.INFINITE  // 无限循环
    addUpdateListener { animator ->
        val offset = animator.animatedValue as Float
        textView.translationX = offset
    }
    start()
}

效果

  • 文字从右向左平滑滚动。
  • 可以调整 duration 控制滚动速度。

总结

方法适用场景优点缺点
marquee 属性简单滚动无需代码,直接 XML 配置灵活性低,无法自定义速度
自定义 View复杂滚动可自定义滚动逻辑需要手动计算滚动位置
ValueAnimator平滑滚动流畅,可调整速度需要计算文字宽度

推荐

  • 如果只是简单滚动,用 marquee
  • 如果需要自定义滚动效果,用 ValueAnimator 或自定义 View

希望这些方法能帮到你!🚀

© 版权声明
THE END
喜欢就点个赞,支持一下吧!
点赞49 分享
评论 抢沙发
头像
欢迎您留下评论!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容