当前位置: 首页 > news >正文

Android控件Selector封装优化指南:高效实现动态UI效果

本文详细介绍了如何在Android开发中优化selector的封装,涵盖Button、TextView、ImageView、CheckBox、RadioButton等常见控件的动态效果实现。通过结合Material Design组件、矢量图、Ripple效果以及动画Selector,提供了一套现代化、高性能的解决方案,帮助开发者提升代码可维护性和应用性能。


1. Button的Selector封装

使用MaterialButtonripple效果,结合shapecolor选择器。

1.1 背景Selector

<!-- res/drawable/button_background_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下状态 -->
    <item android:state_pressed="true">
        <shape android:shape="rectangle">
            <solid android:color="#FF0000"/> <!-- 红色背景 -->
            <corners android:radius="8dp"/> <!-- 圆角 -->
        </shape>
    </item>
    <!-- 默认状态 -->
    <item>
        <shape android:shape="rectangle">
            <solid android:color="#00FF00"/> <!-- 绿色背景 -->
            <corners android:radius="8dp"/>
        </shape>
    </item>
</selector>

1.2 Ripple效果(API 21+)

<!-- res/drawable/button_ripple_selector.xml -->
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#FF0000"> <!-- 波纹颜色 -->
    <item android:drawable="@drawable/button_background_selector"/> <!-- 默认背景 -->
</ripple>

1.3 使用MaterialButton

<com.google.android.material.button.MaterialButton
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me"
    android:background="@drawable/button_ripple_selector"
    app:cornerRadius="8dp"
    app:strokeColor="#0000FF"
    app:strokeWidth="2dp"/>

2. TextView的Selector封装

通过color选择器实现文本颜色的动态变化。

2.1 文本颜色Selector

<!-- res/color/text_color_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下状态 -->
    <item android:state_pressed="true" android:color="#FF0000"/> <!-- 红色 -->
    <!-- 默认状态 -->
    <item android:color="#0000FF"/> <!-- 蓝色 -->
</selector>

2.2 使用

<TextView
    android:id="@+id/myTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textColor="@color/text_color_selector"/>

3. ImageView的Selector封装

使用矢量图和selector实现不同状态下的图标切换。

3.1 图标Selector

<!-- res/drawable/icon_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 按下状态 -->
    <item android:state_pressed="true" android:drawable="@drawable/ic_pressed"/>
    <!-- 默认状态 -->
    <item android:drawable="@drawable/ic_default"/>
</selector>

3.2 使用

<ImageView
    android:id="@+id/myImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon_selector"/>

4. CheckBox的Selector封装

使用selector实现选中和未选中状态的图标切换。

4.1 图标Selector

<!-- res/drawable/checkbox_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 选中状态 -->
    <item android:state_checked="true" android:drawable="@drawable/ic_checked"/>
    <!-- 未选中状态 -->
    <item android:state_checked="false" android:drawable="@drawable/ic_unchecked"/>
</selector>

4.2 使用

<CheckBox
    android:id="@+id/myCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/checkbox_selector"/>

5. RadioButton的Selector封装

使用selector实现选中和未选中状态的图标切换。

5.1 图标Selector

<!-- res/drawable/radiobutton_selector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 选中状态 -->
    <item android:state_checked="true" android:drawable="@drawable/ic_radio_checked"/>
    <!-- 未选中状态 -->
    <item android:state_checked="false" android:drawable="@drawable/ic_radio_unchecked"/>
</selector>

5.2 使用

<RadioButton
    android:id="@+id/myRadioButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/radiobutton_selector"/>

6. 动画Selector(Animated Selector)

使用<animated-selector>实现状态切换时的动画效果。

6.1 动画Selector

<!-- res/drawable/animated_button_selector.xml -->
<animated-selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- 默认状态 -->
    <item android:id="@+id/default_state" android:drawable="@drawable/ic_default"/>
    <!-- 按下状态 -->
    <item android:id="@+id/pressed_state" android:drawable="@drawable/ic_pressed"/>
    <!-- 过渡动画 -->
    <transition android:fromId="@id/default_state" android:toId="@id/pressed_state">
        <animation-list>
            <item android:duration="100" android:drawable="@drawable/intermediate_1"/>
            <item android:duration="100" android:drawable="@drawable/intermediate_2"/>
        </animation-list>
    </transition>
</animated-selector>

6.2 使用

<ImageView
    android:id="@+id/animatedImageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/animated_button_selector"/>

7. 性能优化建议

  1. 减少层级嵌套:避免在selector中嵌套过多层级,以减少渲染开销。
  2. 使用矢量图:尽量使用矢量图(<vector>)代替位图,以减少资源占用。
  3. 复用资源:提取公共的shapecolordrawable,避免重复定义。
  4. Material Design组件:优先使用MaterialButtonMaterialTextView等组件,它们内置了优化效果。

8. 统一管理样式

通过<style>统一管理控件的selector和样式。

8.1 定义样式

<!-- res/values/styles.xml -->
<style name="AppButtonStyle">
    <item name="android:background">@drawable/button_ripple_selector</item>
    <item name="android:textColor">@color/text_color_selector</item>
    <item name="android:textSize">16sp</item>
    <item name="android:padding">12dp</item>
</style>

8.2 使用样式

<Button
    android:id="@+id/myButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Click Me"
    style="@style/AppButtonStyle"/>

通过以上优化整理,你可以实现高效、可维护且现代化的selector封装方案,适用于各种Android控件和场景。

http://www.dtcms.com/a/64143.html

相关文章:

  • LLM训练中常用的Benchmarks
  • uvm_transaction, uvm_seq_item, uvm_object, uvm_component的关系
  • 仅仅使用pytorch来手撕transformer架构(3):编码器模块和编码器类的实现和向前传播
  • 前端高阶面试题·每日一题
  • 【大模型知识点】RMSNorm(Root Mean Square Normalization)均方根归一化
  • linux 命令 ls
  • AI模型的构建过程是怎样的(下)
  • 华为OD机试-乘坐保密电梯-回溯(Java 2024 C卷 200分)
  • 分布式锁技术全景解析:从传统锁机制到MySQL、Redis/Redisson与ZooKeeper实现
  • Python 配置文件管理库Hydra 和 OmegaConf的区别
  • 图形学面试题总结
  • Conda 常规用法指南
  • css 知识点整理
  • Python个人学习笔记(15):模块(time)
  • 司南评测集社区 2 月上新一览!
  • Flutter 小技巧之通过 MediaQuery 优化 App 性能
  • 严格把控K8S集群中的操作权限,为普通用户生成特定的kubeconfig文件
  • C++和标准库速成(一)——HelloWorld和名称空间
  • 【从零开始学习计算机科学】编译原理(二)高级编程语言及其语法描述
  • Go红队开发—web网络编程
  • TCP三次握手
  • 前馈神经网络 - 自动梯度计算
  • Mac 如何在idea集成SVN
  • JAVA SE 4.Java各版本特性
  • 如何通过自动化测试提升DevOps效率?
  • Dify后端结构与二次开发指南(一)
  • 《PYTHON 语言程序设计》2018版 第1章第21题改进中(十)做到这,这个题下轮再说吧
  • 物联网(IoT)技术在水电站、光伏电站和风电场中的应用
  • 【Node.js】--- win11安装 Node.js
  • 【调研】olmOCR解析PDF