android 实现文字打印机效果

在很多 AI 问答、大模型对话(如 ChatGPT、Gemini)或者知识库管理应用中,"打字机吐字(Typewriter Effect)"配合"自动平滑滚动"已经成了标配的用户体验。它不仅能缓解用户等待长文本输出时的焦虑感,还能让文字看起来更美观。

自定义 ScrollingTextView 继承自 Android 原生的 AppCompatTextView,三个核心能力:

  • 动态打印机链路 :基于 CoroutineScope(Dispatchers.Main) 控制字符与换行符的非阻塞分级延迟。

  • 平滑滚动 :利用 Handler 配合 Layout 边界计算,每 10 秒(可调)自动平滑卡点滚动至底部。

  • 动态色彩着色器 :集成 LinearGradient 线性渐变纹理,实现文本内容的多色混叠(Shader)渲染。

一、 打字机核心:吐字效果实现

Kotlin 复制代码
   /**
     * 文本按打印机显示
     * @param text 显示的文本
     * @param charDelay
     * @param lineDelay
     */
    fun startPrinting(text: String?, charDelay: Long = 100, lineDelay: Long = 500) {
        if (isPrinting || text.isNullOrEmpty()) return
        isPrinting = true
        this.text = ""
        mJob = CoroutineScope(Dispatchers.Main).launch {
            text.forEachIndexed { index, char ->
                this@ScrollingTextView.text = text.substring(0, index + 1)
                // 根据字符类型设置延迟
                val delay = if (char == '\n') lineDelay else charDelay
                delay(delay)
            }
            isPrinting = false
        }

    }

二、 滚动效果实现:利用 Handler 循环实现自动化动态底部卡点

Kotlin 复制代码
    private val mHandler = Handler(Looper.getMainLooper())
    private val scrollRunnable = object : Runnable {
        override fun run() {
            smoothScrollToBottom()
            // 每10秒再次执行
            mHandler.postDelayed(this, 10000)
        }
    }

    private fun smoothScrollToBottom() {
        layout ?: return
        val scrollAmount = layout.getLineTop(lineCount) - height
        if (scrollAmount > 0) {
            scrollTo(0, scrollAmount)
        } else {
            scrollTo(0, 0)
        }
    }

三、使用LinearGradient(线性渐变着色器)

它能够将配置在 color.xml 中的 6 种渐变色(R.color.shader1 ~ shader6)让文字有渐变颜色效果

Kotlin 复制代码
    <color name="shader1">#BB86FCFF</color>
    <color name="shader2">#F44336</color>
    <color name="shader3">#E91E63</color>
    <color name="shader4">#673AB7</color>
    <color name="shader5">#2196F3</color>
    <color name="shader6">#8BC34A</color>
Kotlin 复制代码
    private fun setGradientText(textView: TextView) {
        val width = textView.paint.measureText(textView.text.toString())
        mTextShader = LinearGradient(
            0f, 0f, width, textView.textSize,
            intArrayOf(
                ContextCompat.getColor(context, R.color.shader1),
                ContextCompat.getColor(context, R.color.shader2),
                ContextCompat.getColor(context, R.color.shader3),
                ContextCompat.getColor(context, R.color.shader4),
                ContextCompat.getColor(context, R.color.shader5),
                ContextCompat.getColor(context, R.color.shader6)
            ),
            null,
            Shader.TileMode.CLAMP
        )
        textView.paint.shader = mTextShader
    }

四、完整代码:

Kotlin 复制代码
package com.example.knowledgemanagement.view

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.LinearGradient
import android.graphics.Shader
import android.os.Handler
import android.os.Looper
import android.text.method.ScrollingMovementMethod
import android.util.AttributeSet
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.example.knowledgemanagement.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

/**
 * Created
 * date : 2026/6/11
 * desc: 打字机效果自定义View
 */
@SuppressLint("AppCompatCustomView")
class ScrollingTextView(context: Context, attrs: AttributeSet? = null) : AppCompatTextView(context, attrs) {

    private var isGradient = true
    private var isScrollStarted = false
    private var mTextShader: Shader? = null
    private var isPrinting = false
    private var mJob: Job? = null

    init {
        movementMethod = ScrollingMovementMethod()
    }

    override fun onTextChanged(
        text: CharSequence?,
        start: Int,
        lengthBefore: Int,
        lengthAfter: Int
    ) {
        super.onTextChanged(text, start, lengthBefore, lengthAfter)
        if (isGradient) { // 放post可能导致颜色失效
            setGradientText(this)
        } else {
            paint.shader = null
        }
    }


    private val mHandler = Handler(Looper.getMainLooper())
    private val scrollRunnable = object : Runnable {
        override fun run() {
            smoothScrollToBottom()
            // 每10秒再次执行
            mHandler.postDelayed(this, 10000)
        }
    }

    fun startScroll() {
        if (isScrollStarted) {
            return
        }

        // 开始滚动任务
        mHandler.postDelayed(scrollRunnable, 10000)
        isScrollStarted = true
    }

    fun stopScroll() {
        // 移除滚动任务,避免内存泄漏
        isScrollStarted = false
        mHandler.removeCallbacks(scrollRunnable)
    }


    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
//        startScroll()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        stopScroll()
    }

    private fun smoothScrollToBottom() {
        layout ?: return
        val scrollAmount = layout.getLineTop(lineCount) - height
        if (scrollAmount > 0) {
            scrollTo(0, scrollAmount)
        } else {
            scrollTo(0, 0)
        }
    }


    private fun setGradientText(textView: TextView) {
        val width = textView.paint.measureText(textView.text.toString())
        mTextShader = LinearGradient(
            0f, 0f, width, textView.textSize,
            intArrayOf(
                ContextCompat.getColor(context, R.color.shader1),
                ContextCompat.getColor(context, R.color.shader2),
                ContextCompat.getColor(context, R.color.shader3),
                ContextCompat.getColor(context, R.color.shader4),
                ContextCompat.getColor(context, R.color.shader5),
                ContextCompat.getColor(context, R.color.shader6)
            ),
            null,
            Shader.TileMode.CLAMP
        )
        textView.paint.shader = mTextShader
    }

    /**
     * 文本是否需要渐变
     *  isGradient true 开始  false 显示默认白色
     */
    fun setGradient(isGradient: Boolean) {
        this.isGradient = isGradient
        if (isGradient) {
            paint.shader = mTextShader
        } else {
            paint.shader = null
        }
    }

    fun isGradient(): Boolean {
        return isGradient
    }


    /**
     * 文本按打印机显示
     * @param text 显示的文本
     * @param charDelay
     * @param lineDelay
     */
    fun startPrinting(text: String?, charDelay: Long = 100, lineDelay: Long = 500) {
        if (isPrinting || text.isNullOrEmpty()) return
        isPrinting = true
        this.text = ""
        mJob = CoroutineScope(Dispatchers.Main).launch {
            text.forEachIndexed { index, char ->
                this@ScrollingTextView.text = text.substring(0, index + 1)
                // 根据字符类型设置延迟
                val delay = if (char == '\n') lineDelay else charDelay
                delay(delay)
            }
            isPrinting = false
        }

    }

    fun stopPrinting() {
        mJob?.cancel()
        isPrinting = false
    }

}

5、最后实现效果

相关推荐
编程技术手记2 小时前
Vue Scoped CSS 与动态创建 DOM 的兼容性问题
前端·css·vue.js
Patrick_Wilson2 小时前
从「框架内部报错」到「请求头被网关截断」:一次 Sentry 排障与前端 Cookie 误用复盘
前端·http·浏览器
如烟花的信页2 小时前
*花顺cookie逆向分析
javascript·爬虫·python·js逆向
Cerrda2 小时前
从 uno.config.ts 看懂 UnoCSS 图标方案
前端·代码规范
大辉狼_音频架构2 小时前
(一)AudioArchitecture
android
爱勇宝2 小时前
《置身钉内》之后:普通前端的出路在哪里?
前端·后端·程序员
KaMeidebaby3 小时前
卡梅德生物技术快报|羊驼免疫:分子生物学实战:基于羊驼免疫的重链抗体制备与全流程验证方案
前端·网络·数据库·人工智能·算法·百度
MacroZheng3 小时前
别再求前端了!这款对标Claude Design的开源工具,让你一秒拥有顶级设计能力!
前端·vue.js·人工智能
qq3621967053 小时前
Telegram APK 下载安装完整指南 — 2026年最新
android·人工智能·爬虫·chatgpt·智能手机