Window Background 和 UI Speed

本文探讨如何通过调整自定义Theme和背景设置来提升UI速度,包括使用不透明图片作为背景以减少DecorView的影响,并通过新建Theme来快速实现特定效果,特别适用于包含全屏不透明View的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

英文原文:http://android-developers.blogspot.com/2009/03/window-backgrounds-ui-speed.html。正文是我自己做的笔记。

提高UI速度有很多方法,其中的一个是使用自定义的Theme。当你在Activity中调用setContentView()这个方法的时候,Android系统会将你自己的views加入到Activity的窗口当中。但是这个窗口不仅仅是包含你的views,还有一些是Android系统自带的views。其中最重要的一个view是Decorview,下面高亮的就是DecorView:

                                                                                      A typical Android view hierarchy

DecorView实际上是包含Activity的窗口的背景的。在Activity中调用getWindow().setBackgroundDrawable()可以改变Decorview的背景。

如果你使用的是Android自带的标准Theme的话,那么标准Theme的背景就会在你的view出现之前展现出来,也就是说,你首先看到的不是setContentView中的View,而是DecorView的背景图。

当我们使用一张不透明的图片作为Activity的背景时,就像下面这个例子这样:

                                                                                                      An opaque user interface doesn't need a window background

上面是使用Android标准Theme的例子,可以看到帧率是44fps,也就是22毫秒一帧。一个可以使图像渲染得更快的办法是将DecorView的背景去掉。因为我们的图片不透明的,所以DecorView看起来就是多余的了。去掉DecorView的背景之后,帧率变成了51fps,看来速度的确有加快:

                                                                                                          Remove the background for faster drawing

那到底应该怎么做呢?很简单,我们只要在res/values/theme.xml中新建一个style,然后其继承android:Theme,重写其中的android:windowBackground,将其值改为null。如下所示:

<resources>
    <style name="Theme.NoBackground" parent="android:Theme">
        <item name="android:windowBackground">@null</item>
    </style>
</resources>
接着,我们可以将这个自定义的Theme加入到我们的Activity或者Application中,在AndroidManifest.xml中Activity标签和Application标签都有一个熟悉是android:theme,我们只要加上android:theme="@style/Theme.NoBackground"就可以了。这个方法是非常方便的,如果app中使用了MapView,WebView或者一些全屏不透明的View。


还有一种方法是我们可以将我们自己的View中的背景加入到Theme中,这样一开始的时候马上就能都得到加载View的背景,具体的使用方法是:在res/drawable/background_shelf.xml中定义bitmap标签:

<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/shelf_panel"
    android:tileMode="repeat" />
然后在style/theme.xml中自定义theme:

<resources>
    <style name="Theme.Shelves" parent="android:Theme">
        <item name="android:windowBackground">@drawable/background_shelf</item>
        <item name="android:windowNoTitle">true</item>
    </style>
</resources>
总结:有时候最好的方法就是最简单的方法,一旦你要创建一个带有不透明UI或者一个自定义的背景的时候,记得要改变window的背景。



<think>我们之前已经为UI添加了样式表,现在需要将完整的UI代码(包括样式表)整合后输出。 注意:样式表是添加在QMainWindow的styleSheet属性中,因此我们需要在QMainWindow的标签内添加一个property节点。 同时,我们之前已经为各个控件添加了颜色,现在将整个UI代码输出,包括样式表部分。 由于UI代码较长,我将按照要求整合并输出完整的XML。</think>以下是将样式表整合后的完整UI代码,包含了所有按钮布局的视觉优化: ```xml <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> <class>MainWindow</class> <widget class="QMainWindow" name="MainWindow"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>826</width> <height>565</height> </rect> </property> <property name="windowTitle"> <string>智能家居控制面板</string> </property> <!-- 添加全局样式表 --> <property name="styleSheet"> <string notr="true"> /* 全局字体 */ * { font-family: 'Segoe UI'; font-size: 10pt; } /* 连接设置区域 */ #groupBox_connection { background-color: #e6f7ff; border: 1px solid #91d5ff; border-radius: 8px; } /* 设备控制区域 */ #groupBox_control { background-color: #f6ffed; border: 1px solid #b7eb8f; border-radius: 8px; } /* 风扇设置区域 */ #groupBox_fan { background-color: #fffbe6; border: 1px solid #ffe58f; border-radius: 8px; } /* 传感器区域 */ #groupBox_sensor { background-color: #f9f0ff; border: 1px solid #d3adf7; border-radius: 8px; } /* 摄像头区域 */ #groupBox_camera { background-color: #fff0f6; border: 1px solid #ffadd2; border-radius: 8px; } /* 按钮通用样式 */ QPushButton { min-width: 80px; padding: 6px; border-radius: 4px; font-weight: bold; } /* 连接按钮 */ #pb_connect { background-color: #1890ff; color: white; } /* 灯光控制按钮 */ #pb_led, #pb_led_2, #pb_led_3 { background-color: #52c41a; color: white; } /* 风扇控制按钮 */ #pb_fan, #pb_fan_speed_up, #pb_fan_speed_down { background-color: #faad14; color: white; } /* 门锁按钮 */ #pb_doorlock { background-color: #722ed1; color: white; } /* 蜂鸣器按钮 */ #pb_beeper { background-color: #f5222d; color: white; } /* 关闭全部按钮 */ #disable { background-color: #8c8c8c; color: white; } /* 摄像头按钮 */ #pb_camera { background-color: #eb2f96; color: white; } /* 标签样式 */ QLabel { padding: 4px; } /* 输入框样式 */ QLineEdit { padding: 5px; border: 1px solid #d9d9d9; border-radius: 4px; } /* 悬停效果 */ QPushButton:hover { opacity: 0.9; } /* 按下效果 */ QPushButton:pressed { transform: scale(0.98); } </string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout"> <item> <layout class="QHBoxLayout" name="horizontalLayout_top"/> </item> <item> <layout class="QHBoxLayout" name="horizontalLayout_main"> <item> <layout class="QVBoxLayout" name="verticalLayout_left"> <item> <widget class="QGroupBox" name="groupBox_connection"> <property name="title"> <string>MQTT连接设置</string> </property> <layout class="QFormLayout" name="formLayout"> <item row="0" column="0"> <widget class="QLabel" name="label"> <property name="text"> <string>服务器:</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLineEdit" name="hostname"> <property name="text"> <string>mqtt.yyzlab.com.cn</string> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_2"> <property name="text"> <string>端 口:</string> </property> </widget> </item> <item row="1" column="1"> <layout class="QHBoxLayout" name="horizontalLayout_port"> <item> <widget class="QLineEdit" name="le_port"> <property name="text"> <string>1883</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_connect"> <property name="text"> <string>连接</string> </property> </widget> </item> </layout> </item> <item row="2" column="0"> <widget class="QLabel" name="label_3"> <property name="text"> <string>发布主题:</string> </property> </widget> </item> <item row="2" column="1"> <widget class="QLineEdit" name="pub_topic"> <property name="text"> <string>1753019767598/APP2AIOTSIM</string> </property> </widget> </item> <item row="3" column="0"> <widget class="QLabel" name="label_6"> <property name="text"> <string>订阅主题:</string> </property> </widget> </item> <item row="3" column="1"> <widget class="QLineEdit" name="sub_topic"> <property name="text"> <string>1753019767598/AIOTSIM2APP</string> </property> </widget> </item> <item row="4" column="0"> <widget class="QLabel" name="label_sensor_pub"> <property name="text"> <string>传感器发布主题:</string> </property> </widget> </item> <item row="4" column="1"> <widget class="QLineEdit" name="sensor_pub_topic"> <property name="text"> <string>1753019767598/Device2AIOTSIM</string> </property> </widget> </item> </layout> </widget> </item> <item> <widget class="QGroupBox" name="groupBox_control"> <property name="title"> <string>设备控制</string> </property> <layout class="QGridLayout" name="gridLayout_control"> <item row="0" column="0"> <layout class="QVBoxLayout" name="verticalLayout_led1"> <item> <widget class="QPushButton" name="pb_led"> <property name="text"> <string>开灯</string> </property> </widget> </item> <item> <widget class="QLabel" name="label_4"> <property name="text"> <string>电灯1</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </item> <item row="0" column="1"> <layout class="QVBoxLayout" name="verticalLayout_led2"> <item> <widget class="QPushButton" name="pb_led_2"> <property name="text"> <string>开灯</string> </property> </widget> </item> <item> <widget class="QLabel" name="label_5"> <property name="text"> <string>电灯2</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </item> <item row="0" column="2"> <layout class="QVBoxLayout" name="verticalLayout_led3"> <item> <widget class="QPushButton" name="pb_led_3"> <property name="text"> <string>开灯</string> </property> </widget> </item> <item> <widget class="QLabel" name="label_7"> <property name="text"> <string>电灯3</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </item> <item row="1" column="0" colspan="3"> <layout class="QHBoxLayout" name="horizontalLayout_fan"> <item> <layout class="QVBoxLayout" name="verticalLayout_fan"> <item> <widget class="QPushButton" name="pb_fan"> <property name="text"> <string>风扇开</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_doorlock"> <property name="text"> <string>门锁开</string> </property> </widget> </item> </layout> </item> <item> <layout class="QVBoxLayout" name="verticalLayout_fan_speed"> <item> <widget class="QPushButton" name="pb_fan_speed_up"> <property name="text"> <string>风扇加速</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_fan_speed_down"> <property name="text"> <string>风扇减速</string> </property> </widget> </item> </layout> </item> <item> <layout class="QVBoxLayout" name="verticalLayout_beeper"> <item> <widget class="QPushButton" name="pb_beeper"> <property name="text"> <string>蜂鸣器开</string> </property> </widget> </item> <item> <widget class="QPushButton" name="disable"> <property name="text"> <string>关闭全部</string> </property> </widget> </item> </layout> </item> </layout> </item> </layout> </widget> </item> <item> <widget class="QGroupBox" name="groupBox_fan"> <property name="title"> <string>风扇设置</string> </property> <layout class="QFormLayout" name="formLayout_fan"> <item row="0" column="0"> <widget class="QLabel" name="label_fan_speed_title"> <property name="text"> <string>当前风速:</string> </property> </widget> </item> <item row="0" column="1"> <widget class="QLabel" name="label_fan_speed"> <property name="text"> <string>风速:--</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="0"> <widget class="QLabel" name="label_fan_speed_topic"> <property name="text"> <string>风速主题:</string> </property> </widget> </item> <item row="1" column="1"> <widget class="QLineEdit" name="fan_speed_topic"/> </item> </layout> </widget> </item> </layout> </item> <item> <layout class="QVBoxLayout" name="verticalLayout_right"> <item> <widget class="QGroupBox" name="groupBox_sensor"> <property name="title"> <string>传感器数据</string> </property> <layout class="QGridLayout" name="gridLayout_sensor"> <item row="1" column="0"> <widget class="QLabel" name="label_light"> <property name="text"> <string>光照:-- lux</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="1" column="1"> <widget class="QLabel" name="label_people"> <property name="text"> <string>人体红外:无人</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="0" column="0"> <widget class="QLabel" name="label_tem"> <property name="text"> <string>温度:-- ℃</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> <item row="0" column="1"> <widget class="QLabel" name="label_hum"> <property name="text"> <string>湿度:-- %</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </widget> </item> <item> <widget class="QGroupBox" name="groupBox_camera"> <property name="title"> <string>摄像头</string> </property> <layout class="QVBoxLayout" name="verticalLayout_camera"> <item> <layout class="QHBoxLayout" name="horizontalLayout_camera"> <item> <widget class="QLabel" name="label_10"> <property name="text"> <string>摄像头</string> </property> </widget> </item> <item> <widget class="QPushButton" name="pb_camera"> <property name="text"> <string>开启摄像头</string> </property> </widget> </item> </layout> </item> <item> <widget class="QLabel" name="camera_label"> <property name="minimumSize"> <size> <width>400</width> <height>300</height> </size> </property> <property name="styleSheet"> <string notr="true">border: 2px solid gray;</string> </property> <property name="text"> <string>摄像头预览区域</string> </property> <property name="alignment"> <set>Qt::AlignCenter</set> </property> </widget> </item> </layout> </widget> </item> </layout> </item> </layout> </item> </layout> </widget> <widget class="QMenuBar" name="menubar"> <property name="geometry"> <rect> <x>0</x> <y>0</y> <width>826</width> <height>28</height> </rect> </property> </widget> <widget class="QStatusBar" name="statusbar"/> </widget> <resources/> <connections/> </ui> ``` ### 主要视觉优化说明: 1. **功能区域区分**: - 使用不同背景色区分五大功能区(连接设置、设备控制、风扇设置、传感器、摄像头) - 每个区域有对应的柔底色边框颜色 - 圆角设计提升现代感(`border-radius: 8px`) 2. **按钮颜色编码**: ```css /* 灯光控制 - 绿色 */ #pb_led, #pb_led_2, #pb_led_3 { background-color: #52c41a; } /* 风扇控制 - 橙色 */ #pb_fan, #pb_fan_speed_up, #pb_fan_speed_down { background-color: #faad14; } /* 门锁控制 - 紫色 */ #pb_doorlock { background-color: #722ed1; } /* 蜂鸣器 - 红色 */ #pb_beeper { background-color: #f5222d; } /* 全局操作 - 灰色 */ #disable { background-color: #8c8c8c; } /* 连接/摄像头 - 蓝色/粉色 */ #pb_connect { background-color: #1890ff; } #pb_camera { background-color: #eb2f96; } ``` 3. **交互效果**: - 悬停效果:`QPushButton:hover { opacity: 0.9; }` - 按下效果:`QPushButton:pressed { transform: scale(0.98); }` - 所有按钮有统一的内边距圆角 4. **布局优化**: - 输入框增加内边距圆角 - 标签增加内边距 - 按钮设置最小宽度保证一致性 ### 使用说明: 1. 直接复制完整XML到Qt Designer中使用 2. 在Qt Creator中打开.ui文件即可预览效果 3. 可通过修改颜色值快速调整主题 4. 添加了悬停/按下动画增强交互体验
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值