当前位置:网站首页>用 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
后记
代码都很简单,但是希望大家在炎热的夏天都能保持一颗平静的心,未来也能从容的面对生活中的各种事情,努力,共勉。
边栏推荐
- 小米旗下支付公司被罚 12 万,涉违规开立支付账户等:雷军为法定代表人,产品包括 MIUI 钱包 App
- Calculation of stock purchase and sale expenses
- Numpy array: join, flatten, and add dimensions
- The private attribute of this class can be used directly? New() in use!!!
- Ingersoll Rand panel maintenance IR Ingersoll Rand microcomputer controller maintenance xe-145m
- Fastjason filter field
- 组合模式(Composite Pattern)
- 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
- 布隆过滤器 课程研究报告
- Decorator
猜你喜欢

Numpy array: join, flatten, and add dimensions

Boundary value analysis method for learning basic content of software testing (2)

Unity AssetBundle资源打包与资源加载

Function sub file writing
Understanding the IO model

File operations in QT

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

代理模式(Proxy)

数字人行业爆发在即,市场格局几何?

The digital human industry is about to break out. What is the market pattern?
随机推荐
Virtual machine 14 installing win7 (Figure tutorial)
TCP实战案例之即时通信、BS架构模拟
HDI的盲孔设计,你注意到这个细节了吗?
Unity AssetBundle资源打包与资源加载
全链路业务追踪落地实践方案
Calculation of stock purchase and sale expenses
When the interviewer asks you to write binarysort in two ways
两道面试小Demo
2020-10-27
104. maximum depth of binary tree
A classic JVM class loaded interview question class singleton{static singleton instance = new singleton(); private singleton() {}
PMP考试重点总结八——监控过程组(2)
==And eqauls()
1. Kimball dimension modeling of data warehouse: what is a fact table?
线程的生命周期
new URL(“www.jjj.com“)
当面试官让你用两种方式写BinarySort
Huawei OSPF single region
Ingersoll Rand面板维修IR英格索兰微电脑控制器维修XE-145M
Do static code blocks always execute first? The pattern is smaller!!!