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.MaterialButtonandroid: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 使用

<TextViewandroid: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 使用

<ImageViewandroid: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 使用

<CheckBoxandroid: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 使用

<RadioButtonandroid: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 使用

<ImageViewandroid: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 使用样式

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

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/73522.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

pytest+allure+jenkins

本地运行参考&#xff1a;pytestallure 入门-CSDN博客 jenkins运行如下&#xff1a; 安装插件&#xff1a;allure 配置allure安装目录 配置pytest、allure 环境变量 配置流水线 进行build,结果如下 ,点击allure report 查看结果

C#核心笔记——(五)框架概述

.NET Ftamework中几乎所有功能都是通过大量的托管类型提供的。这些类型组织在层次化的命名空间中&#xff0c;并打包为一套程序集&#xff0c;与CLR一起构成了.NET平台。 有些.NET类型是由CLR直接使用的&#xff0c;且对于托管宿主环境而言是必不可少的。这些类型位于一个名为…

phpstudy+phpstorm+xdebug【学习笔记】

配置PHPStudy 配置PHPSTORM phpstorm选择PHP版本 配置DEBUG 设置服务器 编辑配置 学习参考链接&#xff1a;&#xff1a;https://blog.csdn.net/m0_60571842/article/details/133246064

Vue:Vue+TS学习笔记

文章目录 前文Vue 3学习 Vue3 的重要性变更内容 底层逻辑选项式 API 和组合式 API体验组合式 API很多钩子组件通信 和 TS 结合开发为什么要用 TS? 正式上手 Vuets 开发给 ref 添加类型标记。ref, reactive, computed 综合代码模板引用类型传值写法 解决问题: 第三方包想要有类…

ACwing—01背包(暴力bfs+dp+递归+记忆化搜索算法)

问题 有 N件物品和一个容量是 V 的背包。每件物品只能使用一次。 第 i 件物品的体积是 vi&#xff0c;价值是 wi。 求解将哪些物品装入背包&#xff0c;可使这些物品的总体积不超过背包容量&#xff0c;且总价值最大。 输出最大价值。 输入格式 第一行两个整数&#xff0c…

洛谷 P2801 教主的魔法 题解

之前学过 莫队 算法&#xff0c;其运用了分块思想&#xff1b;但是我居然是第一次写纯种的分块题目。 题意 给你一个长度为 n n n 的序列 a a a&#xff08;一开始 ∀ a i ∈ [ 1 , 1000 ] \forall a_i\in[1,1000] ∀ai​∈[1,1000]&#xff09;。要求执行 q q q 次操作&…

leetcode 75.颜色分类(荷兰国旗问题)

题目描述 题目分析 本题是经典的「荷兰国旗问题」&#xff0c;由计算机科学家 Edsger W. Dijkstra 首先提出。 要想单独解决这道题本身还是很简单的&#xff0c;统计0、1、2的数量然后按顺序赋值&#xff0c;或者手写一个冒泡排序&#xff0c;whatever。 但是在这一题中我们主…

rc4算法简单介绍及实现

0. 介绍 RC4是一种流密码&#xff0c;但因为安全性问题已经被弃用。 1. 算法描述 1.1 初始化 Key为生成的随机密钥&#xff1a;1-256B S为一数组&#xff1a;256B T为辅助数组&#xff1a;keylenB for (int i 0;i < 256;i) {S[i] i;T[i] S[i % keylen]; }1.2 初始化…

OpenEuler20.3 安装 Elasticsearch7.17

1、下载elasticsearch wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.17-linux-x86_64.tar.gz wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.17.17-linux-x86_64.tar.gz.sha512 shasum -a 512 -c elasticsea…

单元测试知识总结

&#x1f345; 点击文末小卡片 &#xff0c;免费获取软件测试全套资料&#xff0c;资料在手&#xff0c;涨薪更快 一、什么是单元测试&#xff1f; 单元测试是指&#xff0c;对软件中的最小可测试单元在与程序其他部分相隔离的情况下进行检查和验证的工作&#xff0c;这里的…

python爬虫笔记(一)

文章目录 html基础标签和下划线无序列表和有序列表表格加边框 html的属性a标签&#xff08;网站&#xff09;target属性换行线和水平分割线 图片设置宽高width&#xff0c;height html区块——块元素与行内元素块元素与行内元素块元素举例行内元素举例 表单from标签type属性pla…

【mysql】centOS7安装mysql详细操作步骤!—通过tar包方式

【mysql】centOS7安装mysql详细操作步骤&#xff01; linux系统安装mysql版本 需要 root 权限&#xff0c;使用 root 用户进行命令操作。使用tar文件包&#xff0c;安装&#xff0c;gz包也可以但是还需要配置用户&#xff0c;tar包虽然大&#xff0c;但是全啊&#xff01; 1. …

[新能源]新能源汽车快充与慢充说明

接口示意图 慢充接口为交流充电口&#xff08;七孔&#xff09;&#xff0c;快充接口为直流充电口&#xff08;九孔&#xff09;。 引脚说明 上图给的是充电口的引脚图&#xff0c;充电枪的为镜像的。 慢充接口引脚说明 快充接口引脚说明 充电流程 慢充示意图 慢充&…

Android第二次面试总结(项目拷打理论篇)

&#xff08;一&#xff09;理论基础 LiveData 和 ViewModel 是 Android 架构组件中的重要部分&#xff0c;它们在构建响应式、生命周期感知的 Android 应用程序中发挥着关键作用。下面分别介绍它们的原理。 LiveData 原理 1. 概述 LiveData 是一种可观察的数据持有者类&…

MyBatis SqlSessionFactory 是如何创建的?

SqlSessionFactory 是 MyBatis 的核心接口之一&#xff0c;它是创建 SqlSession 实例的工厂。 SqlSession 实例是 MyBatis 与数据库交互的主要接口&#xff0c;负责执行 SQL 语句、管理事务等。 SqlSessionFactory 的创建过程主要由 SqlSessionFactoryBuilder 类负责。 SqlSes…

玩转python:通俗易懂掌握高级数据结构-collections模块之Counter

引言 Counter是Python中collections模块提供的一个强大工具&#xff0c;用于统计可哈希对象的出现次数。它非常适合用于频率统计、词频分析、数据聚合等场景。本文将详细介绍Counter的关键用法和特性&#xff0c;并通过8个丰富的案例帮助读者掌握其应用。 关键用法和特性表格 …

DeepSeek面试——分词算法

DeepSeek-V3 分词算法 一、核心算法&#xff1a;字节级BPE&#xff08;Byte-level BPE&#xff0c;BBPE&#xff09; DeepSeek-V3 采用 字节级BPE&#xff08;BBPE&#xff09; 作为核心分词算法&#xff0c;这是对传统 BPE&#xff08;Byte Pair Encoding&#xff09;算法的…

机器学习——正则化、欠拟合、过拟合、学习曲线

过拟合&#xff08;overfitting&#xff09;:模型只能拟合训练数据的状态。即过度训练。 避免过拟合的几种方法&#xff1a; ①增加全部训练数据的数量&#xff08;最为有效的方式&#xff09; ②使用简单的模型&#xff08;简单的模型学不够&#xff0c;复杂的模型学的太多&am…

Python:函数(一)

python函数相关的知识点 1. 函数定义与调用 定义&#xff1a;使用 def 关键字&#xff0c;后接函数名和参数列表。 def greet(name):"""打印问候语&#xff08;文档字符串&#xff09;"""print(f"Hello, {name}!") 调用&#xff1a…

关于Flutter中两种Widget的生命周期详解

目录 一、StatelessWidget 生命周期 二、StatefulWidget 生命周期 1. 创建阶段 2. State初始化阶段 3. 构建阶段 4. 更新阶段 5. 销毁阶段 三、核心对比与常见陷阱 四、面试回答技巧 以下是Flutter中两种核心Widget(StatelessWidget和StatefulWidget)生命周…