当前位置:网站首页>用 Compose 实现个空调,为你的夏日带去清凉
用 Compose 实现个空调,为你的夏日带去清凉
2022-06-28 09:34:00 【朱 江】
热
夏天到了,体验就如小标题的那一个字,于是乎, 就想着画个空调吧,看着就能凉快点。。。
先来看看成品样式吧。

哈哈哈,看着是不是挺凉快。
搞空调
先来捋一下需要实现的功能吧:
1、首先要有开关,用来控制空调的开关状态;
2、然后可以设置冷风和暖风,保证冬天和夏天都能用嘛;
3、最后就是增加和减小温度了;
OK,需求差不多就是这样,接下来就该码代码了。
写标题
先来写下空调的标题吧:
Column(
modifier = Modifier
.fillMaxSize()
.statusBarsPadding()
.navigationBarsPadding(),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.CenterHorizontally,
) {
Spacer(modifier = Modifier.height(50.dp))
Text(text = "便携小空调", fontSize = 25.sp)
Text(
text = "Tip: 为你的夏日带去清凉!",
fontSize = 15.sp,
modifier = Modifier.padding(bottom = 20.dp)
)
}
上面代码没什么,只是一个线性布局包裹着两个Text,没啥可说的。
画空调样式
再来画下空调的样式吧:
@Composable
private fun Conditioner() {
Card(
modifier = Modifier
.fillMaxWidth()
.height(100.dp)
.padding(horizontal = 20.dp),
shape = RoundedCornerShape(
topStart = 18f,
topEnd = 18f,
bottomStart = 36f,
bottomEnd = 36f
)
) {
Column {
Divider(
modifier = Modifier.padding(top = 6.dp),
thickness = 0.4.dp,
)
Row(
modifier = Modifier
.fillMaxSize()
.background(color = Color.White)
.padding(6.dp)
) {
Image(
painter = painterResource(id = R.mipmap.logo),
contentDescription = "",
)
Column(
modifier = Modifier
.fillMaxSize(),
verticalArrangement = Arrangement.Top,
horizontalAlignment = Alignment.End,
) {
Image(
painter = painterResource(id = R.drawable.ic_snow),
contentDescription = "",
modifier = Modifier.size(25.dp)
)
Spacer(modifier = Modifier.height(5.dp))
Text(
text = "23°C",
fontSize = 20.sp,
fontFamily = FontFamily(Font(R.font.lcd, FontWeight.SemiBold))
)
}
}
}
}
}
这块简单说下吧,空调的样式会有圆角,这里使用Card来实现,然后有一个节能能效的一个图片,右边放着冷风还是暖风的标识和温度。
控制空调
空调样式已经画好,只剩控制空调了:
// 空调开关
val airSwitch = rememberSaveable { mutableStateOf(false) }
// 温度
val temperature = rememberSaveable { mutableStateOf(23) }
// 类型,制冷还是制热
val airType = rememberSaveable { mutableStateOf(true) }
Row(modifier = Modifier.padding(10.dp)) {
Button(onClick = { airType.value = true }, shape = RoundedCornerShape(15.dp)) {
Text(text = "冷风")
}
Switch(checked = airSwitch.value, onCheckedChange = {
airSwitch.value = !airSwitch.value
})
Button(onClick = { airType.value = false }, shape = RoundedCornerShape(15.dp)) {
Text(text = "热风")
}
}
Button(onClick = {
if (temperature.value >= 31) {
showToast(context, "温度不能再高了!!!")
Log.e(TAG, "AirConditioner: 太热了")
[email protected]
}
temperature.value = temperature.value + 1
}, shape = RoundedCornerShape(15.dp)) {
Text(text = "+")
}
Button(onClick = {
if (temperature.value <= 16) {
showToast(context, "温度不能再低了!!!")
Log.e(TAG, "AirConditioner: 太冷了")
[email protected]
}
temperature.value = temperature.value - 1
}, shape = RoundedCornerShape(15.dp)) {
Text(text = "-")
}
控制空调就需要State来进行控制了,代码也很简单,只是修改了下状态值。
吹空调
整个应用代码很简单,加上各种空行和imports都不到二百行。不到二百行就能写一个小空调,虽然没有实际的温度体验。。。。但是,心静自然凉嘛
大家可以复制下代码,然后自己在手机上也吹一吹试试效果,哈哈哈
我把这个小Demo上传到Github了,大家可以自行取代码和图片:https://github.com/zhujiang521/AirConditioner
后记
代码都很简单,但是希望大家在炎热的夏天都能保持一颗平静的心,未来也能从容的面对生活中的各种事情,努力,共勉。
边栏推荐
- A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
- ==And eqauls()
- June 27, 2022: give a 01 string with a length of N. now please find two intervals so that the number of 1 is equal and the number of 0 is equal in the two intervals. The two intervals can intersect bu
- Redis5.0 slot migration, free play (single machine migration cluster)
- PHP curl forged IP address and header information code instance - Alibaba cloud
- Settings of gift giving module and other custom controls in one-to-one video chat system code
- When the interviewer asks you to write binarysort in two ways
- Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
- 代理模式(Proxy)
- Decorator
猜你喜欢

PMP考试重点总结六——图表整理

Valentine's Day - VBS learning (sentences, love words)

Full link service tracking implementation scheme

Data visualization makes correlation analysis easier to use

桥接模式(Bridge)

1182: effets de la photo de groupe

Matplotlib属性及注解

Decorator

JDBC connection database (MySQL) steps

Function sub file writing
随机推荐
Unity 从服务器加载AssetBundle资源写入本地内存,并将下载保存的AB资源从本地内存加载至场景
手机号、邮箱正则验证[通俗易懂]
JDBC connection database (MySQL) steps
Two interview demo
Wechat applet development log
new URL(“www.jjj.com“)
Play SFTP upload file
1182: effets de la photo de groupe
分而治之之经典Hanoi
[ybtoj advanced training guide] maximum separation [hash] [Floyd]
创建多线程的方法---1创建Thread类的子类及多线程原理
满电出发加速品牌焕新,长安电动电气化产品吹响“集结号”
异常处理4种方法
The constructor is never executed immediately after new()!!!!!
Comprehensive evaluation of outline note taking software workflow: advantages, disadvantages and evaluation
Static page of pinyougou mall
2020-10-27
结巴分词器_分词器原理
Numpy array: join, flatten, and add dimensions
PMP考试重点总结七——监控过程组(1)