【Android 项目】个人学习demo随笔

文章目录

  • Demo1:自定义打印机效果
    • [1. 理论](#1. 理论)
      • [1.1 baseline定义](#1.1 baseline定义)
      • [1.2 声明自定义视图(View)的属性(attributes)](#1.2 声明自定义视图(View)的属性(attributes))
      • [1.3 Flow和Collect](#1.3 Flow和Collect)

Demo1:自定义打印机效果

1. 理论

1.1 baseline定义

添加链接描述

在 Android 布局中,baseline(基线) 是指文本绘制时所依据的一条虚拟水平线。它是字体设计中的一个概念,用于确保不同字体、不同大小的文本在垂直方向上能够对齐得当。

baseline 的作用:

在 Android 中,baseline 主要用于对齐 TextView 或其他文本控件。

当多个文本控件并排显示时,系统会默认以它们的 基线对齐(baseline alignment)

1.2 声明自定义视图(View)的属性(attributes)

xml 复制代码
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="TypewriterTextView">
        <attr name="typingSpeed" format="integer" />
        <attr name="cursorBlinkSpeed" format="integer" />
        <attr name="showCursor" format="boolean" />
        <attr name="typewriterText" format="string" />
    </declare-styleable>
</resources>
  • xml中引用
xml 复制代码
<com.example.view.TypewriterTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:typingSpeed="100"
    app:cursorBlinkSpeed="500"
    app:showCursor="true"
    app:typewriterText="Hello, this is a typewriter effect!" />
  • Java/Kotlin中使用
kotlin 复制代码
    init {
        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.TypewriterTextView)
            val typingSpeed = typedArray.getInt(R.styleable.TypewriterTextView_typingSpeed, 500)
            val cursorBlinkSpeed = typedArray.getInt(R.styleable.TypewriterTextView_cursorBlinkSpeed, 500)
            val showCursor = typedArray.getBoolean(R.styleable.TypewriterTextView_showCursor, true)
            val typewriterText = typedArray.getString(R.styleable.TypewriterTextView_typewriterText)

            // 使用这些值初始化打字机逻辑...

            typedArray.recycle()
        }
    }

1.3 Flow和Collect

相关推荐
随遇丿而安1 小时前
第2周:`EditText` 不只是输入框,它是 Android 输入体验的第一道门
android
我命由我123451 小时前
Kotlin 开发 - lateinit 关键字
android·java·开发语言·kotlin·android studio·android-studio·android runtime
一起搞IT吧2 小时前
Android性能系列专题理论之十:systrace/perfetto相关指标知识点细节含义总结
android·嵌入式硬件·智能手机·性能优化
小书房6 小时前
Kotlin的by
android·开发语言·kotlin·委托·by
jinanwuhuaguo7 小时前
(第二十八篇)OpenClaw成本与感知的奇点——从“Token封建制”到“全民养虾”的本体论地基
android·人工智能·kotlin·拓扑学·openclaw
xxjj998a8 小时前
Laravel4.x核心特性全解析
android·mysql·laravel
JoshRen8 小时前
2026教程:在Android Termux中集成Gemini 3镜像站实现移动端文档自动处理与摘要生成(附国内免费方案)
android
诸神黄昏EX9 小时前
Android Google KEY
android
一起搞IT吧9 小时前
Android性能系列专题理论之十一:block IO问题分析思路
android·嵌入式硬件·智能手机·性能优化
小妖66610 小时前
怎么用 tauri 创建编译 android 应用程序
android·tauri