Skip to content

Commit 57cdcec

Browse files
committed
修改UI配色
1 parent 94e72ef commit 57cdcec

File tree

13 files changed

+128
-76
lines changed

13 files changed

+128
-76
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
| ![5](./docs/img/screenshot5.jpg) <br /> 程序员模式 | ![6](./docs/img/screenshot6.jpg) <br /> 程序员模式 |
3434

3535
## 其他
36-
布局和处理逻辑参考了 *微软计算器*
36+
处理逻辑参考了 *微软计算器*
37+
38+
布局参考了 *小米计算器**微软计算器*
3739

3840
实现细节:*待补充*

app/src/main/java/com/equationl/calculator_compose/dataModel/KeyBoardData.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ const val KeyIndex_Back = 1003
4848

4949

5050
@Composable
51-
fun numberColor(): Color = MaterialTheme.colors.secondary
51+
fun numberColor(): Color = Color.Unspecified // MaterialTheme.colors.secondary
5252

5353
@Composable
5454
fun functionColor(): Color = MaterialTheme.colors.primary
@@ -89,15 +89,15 @@ fun standardKeyBoardBtn(): List<List<KeyBoardData>> = listOf(
8989
KeyBoardData(Operator.ADD.showText, functionColor(), KeyIndex_Add),
9090
),
9191
listOf(
92-
KeyBoardData("+/-", functionColor(), KeyIndex_NegativeNumber),
92+
KeyBoardData("+/-", numberColor(), KeyIndex_NegativeNumber),
9393
KeyBoardData("0", numberColor(), KeyIndex_0),
94-
KeyBoardData(".", functionColor(), KeyIndex_Point),
95-
KeyBoardData("=", equalColor(), KeyIndex_Equal),
94+
KeyBoardData(".", numberColor(), KeyIndex_Point),
95+
KeyBoardData("=", equalColor(), KeyIndex_Equal, isFilled = true),
9696
)
9797
)
9898

9999
@Composable
100-
fun programmerLeftKeyBoardBtn(): List<List<KeyBoardData>> = listOf(
100+
fun programmerNumberKeyBoardBtn(): List<List<KeyBoardData>> = listOf(
101101
listOf(
102102
KeyBoardData("D", numberColor(), KeyIndex_D),
103103
KeyBoardData("E", numberColor(), KeyIndex_E),
@@ -131,7 +131,7 @@ fun programmerLeftKeyBoardBtn(): List<List<KeyBoardData>> = listOf(
131131
)
132132

133133
@Composable
134-
fun programmerRightKeyBoardBtn(): List<List<KeyBoardData>> = listOf(
134+
fun programmerFunctionKeyBoardBtn(): List<List<KeyBoardData>> = listOf(
135135
listOf(
136136
KeyBoardData("C", functionColor(), KeyIndex_Clear),
137137
KeyBoardData("", functionColor(), KeyIndex_Back)
@@ -154,7 +154,7 @@ fun programmerRightKeyBoardBtn(): List<List<KeyBoardData>> = listOf(
154154
),
155155
listOf(
156156
KeyBoardData("OR", functionColor(), KeyIndex_Or),
157-
KeyBoardData("=", equalColor(), KeyIndex_Equal)
157+
KeyBoardData("=", equalColor(), KeyIndex_Equal, isFilled = true)
158158
)
159159
)
160160

@@ -169,8 +169,15 @@ val BitOperationList = listOf(
169169

170170
data class KeyBoardData(
171171
val text: String,
172+
/**
173+
* 设置按钮颜色,设置范围取决于 [isFilled]
174+
* */
172175
val background: Color,
173176
val index: Int,
177+
/**
178+
* 是否填充该按钮,如果为 true 则 [background] 用于填充该按钮背景;否则,[background] 用于设置该按钮字体颜色
179+
* */
180+
val isFilled: Boolean = false,
174181
val isAvailable: Boolean = true
175182
)
176183

app/src/main/java/com/equationl/calculator_compose/ui/theme/Theme.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ import androidx.compose.material.MaterialTheme
55
import androidx.compose.material.darkColors
66
import androidx.compose.material.lightColors
77
import androidx.compose.runtime.Composable
8-
import androidx.compose.ui.graphics.Color
98

109
private val DarkColorPalette = darkColors(
1110
primary = Grey500,
1211
primaryVariant = Grey700,
1312
secondary = Grey200,
14-
background = Color.LightGray
13+
//background = Color.LightGray
1514
)
1615

1716
private val LightColorPalette = lightColors(
1817
primary = Amber500,
1918
primaryVariant = Amber700,
2019
secondary = AmberA200,
21-
background = Amber200,
20+
//background = Color.White,
2221
)
2322

2423
@Composable

app/src/main/java/com/equationl/calculator_compose/utils/VibratorHelper.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import androidx.appcompat.app.AppCompatActivity
1515
* Description: Vibrator帮助类,用于解决旧版本兼容问题
1616
*/
1717
class VibratorHelper {
18-
private lateinit var vibrator: Vibrator
18+
private var vibrator: Vibrator? = null
1919

2020
companion object {
2121
val instance by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
@@ -39,24 +39,24 @@ class VibratorHelper {
3939
}
4040

4141
fun cancel() {
42-
vibrator.cancel()
42+
vibrator?.cancel()
4343
}
4444

4545
fun hasVibrator(): Boolean {
46-
return vibrator.hasVibrator()
46+
return vibrator?.hasVibrator() == true
4747
}
4848

4949
fun hasAmplitudeControl(): Boolean {
5050
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
5151
return false
5252
}
53-
return vibrator.hasAmplitudeControl()
53+
return vibrator?.hasAmplitudeControl() == true
5454
}
5555

5656
fun vibrate(timings: LongArray, amplitudes: IntArray, repeat: Int) {
5757
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
5858
val vibrationEffect = VibrationEffect.createWaveform(timings, amplitudes, repeat)
59-
vibrator.vibrate(vibrationEffect)
59+
vibrator?.vibrate(vibrationEffect)
6060
}
6161
else {
6262
val pattern = mutableListOf<Long>()
@@ -76,25 +76,25 @@ class VibratorHelper {
7676

7777
val patternA = pattern.toLongArray()
7878
@Suppress("DEPRECATION")
79-
vibrator.vibrate(patternA, repeat)
79+
vibrator?.vibrate(patternA, repeat)
8080
}
8181
}
8282

8383
fun vibrateOneShot(milliseconds: Long, amplitude: Int) {
8484
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
8585
val vibrationEffect = VibrationEffect.createOneShot(milliseconds, amplitude)
86-
vibrator.vibrate(vibrationEffect)
86+
vibrator?.vibrate(vibrationEffect)
8787
}
8888
else {
8989
@Suppress("DEPRECATION")
90-
vibrator.vibrate(milliseconds)
90+
vibrator?.vibrate(milliseconds)
9191
}
9292
}
9393

9494
fun vibratePredefined(predefined: Int) {
9595
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
9696
val vibrationEffect = VibrationEffect.createPredefined(predefined)
97-
vibrator.vibrate(vibrationEffect)
97+
vibrator?.vibrate(vibrationEffect)
9898
}
9999
else {
100100
// 系统预设效果就暂时不适配了

app/src/main/java/com/equationl/calculator_compose/view/HIstoryWidget.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fun HistoryWidget(
3434
onDelete: (item: HistoryData?) -> Unit
3535
) {
3636

37-
Column(Modifier.fillMaxSize().background(MaterialTheme.colors.primaryVariant)) {
37+
Column(Modifier.fillMaxSize().background(MaterialTheme.colors.background)) {
3838
LazyColumn(modifier = Modifier.weight(9f)) {
3939
items(
4040
items = historyList,

app/src/main/java/com/equationl/calculator_compose/view/ProgrammerScreen.kt

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.equationl.calculator_compose.view
22

33
import androidx.compose.animation.*
4+
import androidx.compose.foundation.BorderStroke
45
import androidx.compose.foundation.background
56
import androidx.compose.foundation.clickable
67
import androidx.compose.foundation.layout.*
7-
import androidx.compose.material.Card
8-
import androidx.compose.material.ExperimentalMaterialApi
9-
import androidx.compose.material.MaterialTheme
10-
import androidx.compose.material.Text
8+
import androidx.compose.material.*
119
import androidx.compose.runtime.Composable
1210
import androidx.compose.ui.Alignment
1311
import androidx.compose.ui.Modifier
@@ -19,8 +17,8 @@ import androidx.compose.ui.unit.dp
1917
import androidx.compose.ui.unit.sp
2018
import androidx.hilt.navigation.compose.hiltViewModel
2119
import com.equationl.calculator_compose.dataModel.InputBase
22-
import com.equationl.calculator_compose.dataModel.programmerLeftKeyBoardBtn
23-
import com.equationl.calculator_compose.dataModel.programmerRightKeyBoardBtn
20+
import com.equationl.calculator_compose.dataModel.programmerFunctionKeyBoardBtn
21+
import com.equationl.calculator_compose.dataModel.programmerNumberKeyBoardBtn
2422
import com.equationl.calculator_compose.ui.theme.*
2523
import com.equationl.calculator_compose.utils.formatNumber
2624
import com.equationl.calculator_compose.view.widgets.AutoSizeText
@@ -34,18 +32,22 @@ fun ProgrammerScreen(
3432
Row(modifier = Modifier.fillMaxWidth(),
3533
horizontalArrangement = Arrangement.SpaceBetween) {
3634
// 左侧键盘
37-
Row(modifier = Modifier.weight(1.5f)) {
38-
LeftKeyBoard(viewModel = viewModel)
35+
Row(modifier = Modifier.weight(1.3f)) {
36+
FunctionKeyBoard(viewModel = viewModel)
3937
}
4038

39+
Divider(modifier = Modifier.fillMaxHeight().width(1.dp). padding(vertical = 16.dp, horizontal = 0.dp))
40+
4141
// 显示数据
4242
Row(modifier = Modifier.weight(2f)) {
4343
CenterScreen(viewModel = viewModel)
4444
}
4545

46+
Divider(modifier = Modifier.fillMaxHeight().width(1.dp). padding(vertical = 16.dp, horizontal = 0.dp))
47+
4648
// 右侧键盘
47-
Row(modifier = Modifier.weight(1.3f)) {
48-
RightKeyBoard(viewModel = viewModel)
49+
Row(modifier = Modifier.weight(1.5f)) {
50+
NumberBoard(viewModel = viewModel)
4951
}
5052
}
5153
}
@@ -64,7 +66,13 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
6466
horizontalAlignment = Alignment.End
6567
) {
6668
AnimatedContent(targetState = viewState.showText) { targetState: String ->
67-
Text(text = targetState, modifier = Modifier.padding(8.dp), fontSize = ShowNormalFontSize, fontWeight = FontWeight.Light)
69+
Text(
70+
text = targetState,
71+
modifier = Modifier.padding(8.dp),
72+
fontSize = ShowNormalFontSize,
73+
fontWeight = FontWeight.Light,
74+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
75+
)
6876
}
6977
AnimatedContent(
7078
targetState = viewState.inputValue,
@@ -91,7 +99,8 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
9199
)
92100
,
93101
fontSize = InputLargeFontSize,
94-
fontWeight = FontWeight.Bold
102+
fontWeight = FontWeight.Bold,
103+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
95104
)
96105
}
97106
}
@@ -112,13 +121,15 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
112121
fontSize =
113122
if (viewState.inputBase == InputBase.HEX) InputTitleContentSize
114123
else InputNormalFontSize,
115-
fontWeight = if (viewState.inputBase == InputBase.HEX) FontWeight.Bold else null
124+
fontWeight = if (viewState.inputBase == InputBase.HEX) FontWeight.Bold else null,
125+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
116126
)
117127

118128
Text(
119129
text = viewState.inputHexText.formatNumber(addSplitChar = " ", splitLength = 4),
120130
fontSize = InputNormalFontSize,
121-
modifier = Modifier.padding(start = 8.dp)
131+
modifier = Modifier.padding(start = 8.dp),
132+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
122133
)
123134
}
124135
Row(verticalAlignment = Alignment.CenterVertically,
@@ -131,13 +142,15 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
131142
fontSize =
132143
if (viewState.inputBase == InputBase.DEC) InputTitleContentSize
133144
else InputNormalFontSize,
134-
fontWeight = if (viewState.inputBase == InputBase.DEC) FontWeight.Bold else null
145+
fontWeight = if (viewState.inputBase == InputBase.DEC) FontWeight.Bold else null,
146+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
135147
)
136148

137149
Text(
138150
text = viewState.inputDecText.formatNumber(),
139151
fontSize = InputNormalFontSize,
140-
modifier = Modifier.padding(start = 8.dp)
152+
modifier = Modifier.padding(start = 8.dp),
153+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
141154
)
142155
}
143156
Row(verticalAlignment = Alignment.CenterVertically,
@@ -150,13 +163,15 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
150163
fontSize =
151164
if (viewState.inputBase == InputBase.OCT) InputTitleContentSize
152165
else InputNormalFontSize,
153-
fontWeight = if (viewState.inputBase == InputBase.OCT) FontWeight.Bold else null
166+
fontWeight = if (viewState.inputBase == InputBase.OCT) FontWeight.Bold else null,
167+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
154168
)
155169

156170
Text(
157171
text = viewState.inputOctText.formatNumber(addSplitChar = " "),
158172
fontSize = InputNormalFontSize,
159-
modifier = Modifier.padding(start = 8.dp)
173+
modifier = Modifier.padding(start = 8.dp),
174+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
160175
)
161176
}
162177
Row(verticalAlignment = Alignment.CenterVertically,
@@ -169,26 +184,28 @@ private fun CenterScreen(viewModel: ProgrammerViewModel) {
169184
fontSize =
170185
if (viewState.inputBase == InputBase.BIN) InputTitleContentSize
171186
else InputNormalFontSize,
172-
fontWeight = if (viewState.inputBase == InputBase.BIN) FontWeight.Bold else null
187+
fontWeight = if (viewState.inputBase == InputBase.BIN) FontWeight.Bold else null,
188+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
173189
)
174190

175191
Text(
176192
text = viewState.inputBinText.formatNumber(addSplitChar = " ", splitLength = 4, isAddLeadingZero = viewState.inputBinText != "0"),
177193
fontSize = InputNormalFontSize,
178194
modifier = Modifier
179195
.padding(start = 8.dp),
196+
color = if (MaterialTheme.colors.isLight) Color.Unspecified else MaterialTheme.colors.primary
180197
)
181198
}
182199
}
183200
}
184201
}
185202

186203
@Composable
187-
private fun LeftKeyBoard(viewModel: ProgrammerViewModel) {
204+
private fun NumberBoard(viewModel: ProgrammerViewModel) {
188205
val viewState = viewModel.viewStates
189206

190207
Column(modifier = Modifier.fillMaxSize()) {
191-
for (btnRow in programmerLeftKeyBoardBtn()) {
208+
for (btnRow in programmerNumberKeyBoardBtn()) {
192209
Row(modifier = Modifier
193210
.fillMaxWidth()
194211
.weight(1f)) {
@@ -206,6 +223,7 @@ private fun LeftKeyBoard(viewModel: ProgrammerViewModel) {
206223
onClick = { viewModel.dispatch(ProgrammerAction.ClickBtn(btn.index)) },
207224
isAvailable = isAvailable,
208225
backGround = btn.background,
226+
isFilled = btn.isFilled,
209227
paddingValues = PaddingValues(0.5.dp)
210228
)
211229
}
@@ -216,11 +234,11 @@ private fun LeftKeyBoard(viewModel: ProgrammerViewModel) {
216234
}
217235

218236
@Composable
219-
private fun RightKeyBoard(viewModel: ProgrammerViewModel) {
237+
private fun FunctionKeyBoard(viewModel: ProgrammerViewModel) {
220238
val viewState = viewModel.viewStates
221239

222240
Column(modifier = Modifier.fillMaxSize()) {
223-
for (btnRow in programmerRightKeyBoardBtn()) {
241+
for (btnRow in programmerFunctionKeyBoardBtn()) {
224242
Row(modifier = Modifier
225243
.fillMaxWidth()
226244
.weight(1f)) {
@@ -238,6 +256,7 @@ private fun RightKeyBoard(viewModel: ProgrammerViewModel) {
238256
onClick = { viewModel.dispatch(ProgrammerAction.ClickBtn(btn.index)) },
239257
isAvailable = isAvailable,
240258
backGround = btn.background,
259+
isFilled = btn.isFilled,
241260
paddingValues = PaddingValues(0.5.dp)
242261
)
243262
}
@@ -254,19 +273,28 @@ private fun KeyBoardButton(
254273
onClick: () -> Unit,
255274
isAvailable: Boolean = true,
256275
backGround: Color = Color.White,
276+
isFilled: Boolean = false,
257277
paddingValues: PaddingValues = PaddingValues(0.dp)
258278
) {
259279
Card(
260280
onClick = { onClick() },
261281
modifier = Modifier
262282
.fillMaxSize()
263283
.padding(paddingValues),
264-
backgroundColor = backGround,
284+
backgroundColor = if (isFilled) backGround else MaterialTheme.colors.surface,
265285
shape = MaterialTheme.shapes.large,
286+
elevation = 0.dp,
287+
border = BorderStroke(0.dp, Color.Transparent),
266288
enabled = isAvailable
267289
) {
268290
Row(Modifier.fillMaxSize(), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically) {
269-
Text(text, fontSize = 24.sp, color = if (isAvailable) Color.Unspecified else Color.LightGray)
291+
Text(
292+
text,
293+
fontSize = 24.sp,
294+
color = if (isAvailable) {
295+
if (isFilled) Color.Unspecified else backGround
296+
} else Color.DarkGray
297+
)
270298
}
271299
}
272300
}

0 commit comments

Comments
 (0)