Skip to content

Commit 80124cf

Browse files
author
zhujiang2
committed
新增日出日落模块
1 parent 2948728 commit 80124cf

File tree

8 files changed

+243
-18
lines changed

8 files changed

+243
-18
lines changed

.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/release/app-release.apk

2.52 KB
Binary file not shown.

app/release/output-metadata.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"type": "SINGLE",
1212
"filters": [],
1313
"attributes": [],
14-
"versionCode": 28,
15-
"versionName": "3.3.0",
14+
"versionCode": 29,
15+
"versionName": "3.3.1",
1616
"outputFile": "app-release.apk"
1717
}
1818
],

app/src/main/java/com/zj/weather/view/weather/widget/DayWeatherContent.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ fun DayWeatherContent(
3737
modifier = Modifier
3838
.weight(1f)
3939
.padding(top = 5.dp, bottom = 5.dp, start = 5.dp),
40-
stringResource(id = R.string.sun_title),
41-
"${stringResource(id = R.string.sun_sunrise)}${dailyBean?.sunrise ?: "07:00"}",
42-
"${stringResource(id = R.string.sun_sunset)}${dailyBean?.sunset ?: "19:00"}"
40+
stringResource(id = R.string.air_pressure_title),
41+
"${dailyBean?.pressure ?: "0"}${stringResource(id = R.string.air_pressure_unit)}",
42+
stringResource(id = R.string.air_pressure_tip)
4343
)
4444
}
4545

@@ -97,7 +97,7 @@ private fun WeatherContentItem(modifier: Modifier, title: String, value: String,
9797
.fillMaxWidth()
9898
.padding(10.dp)
9999
) {
100-
Text(text = title, fontSize = 13.sp)
100+
Text(text = title, fontSize = 11.sp)
101101
Text(
102102
text = value,
103103
modifier = Modifier.padding(top = 8.dp),

app/src/main/java/com/zj/weather/view/weather/widget/LifeWeatherContent.kt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,6 @@ fun LifeWeatherContent(weatherLifeList: List<WeatherLifeIndicesBean.WeatherLifeI
7979
Spacer(modifier = Modifier.height(15.dp))
8080
}
8181
}
82-
Spacer(modifier = Modifier.height(10.dp))
83-
84-
Text(
85-
text = stringResource(id = R.string.data_source),
86-
fontSize = 12.sp,
87-
textAlign = TextAlign.Center,
88-
modifier = Modifier
89-
.fillMaxWidth()
90-
.padding(bottom = 35.dp)
91-
)
9282
}
9383

9484
@Composable
Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
package com.zj.weather.view.weather.widget
2+
3+
import android.content.Context
4+
import android.graphics.Bitmap
5+
import android.icu.util.Calendar
6+
import androidx.compose.foundation.Canvas
7+
import androidx.compose.foundation.layout.*
8+
import androidx.compose.foundation.shape.RoundedCornerShape
9+
import androidx.compose.material.Card
10+
import androidx.compose.material.Divider
11+
import androidx.compose.material.Text
12+
import androidx.compose.runtime.Composable
13+
import androidx.compose.ui.Alignment
14+
import androidx.compose.ui.Modifier
15+
import androidx.compose.ui.geometry.Offset
16+
import androidx.compose.ui.graphics.*
17+
import androidx.compose.ui.graphics.drawscope.Stroke
18+
import androidx.compose.ui.platform.LocalContext
19+
import androidx.compose.ui.res.stringResource
20+
import androidx.compose.ui.unit.dp
21+
import androidx.compose.ui.unit.sp
22+
import androidx.core.content.ContextCompat
23+
import com.zj.model.weather.WeatherDailyBean
24+
import com.zj.utils.XLog
25+
import com.zj.weather.R
26+
import kotlin.math.sin
27+
28+
29+
@Composable
30+
fun SunriseSunsetContent(dailyBean: WeatherDailyBean.DailyBean?) {
31+
if (dailyBean == null) {
32+
XLog.e("dailyBean is null")
33+
return
34+
}
35+
val context = LocalContext.current
36+
Card(
37+
modifier = Modifier.fillMaxWidth(),
38+
shape = RoundedCornerShape(10.dp)
39+
) {
40+
Column(modifier = Modifier.fillMaxWidth()) {
41+
42+
Text(
43+
text = stringResource(id = R.string.sun_title),
44+
fontSize = 14.sp,
45+
modifier = Modifier
46+
.padding(top = 10.dp, bottom = 7.dp, start = 10.dp, end = 10.dp)
47+
)
48+
Divider(
49+
modifier = Modifier.padding(start = 10.dp, end = 10.dp, bottom = 10.dp),
50+
thickness = 0.4.dp
51+
)
52+
53+
SunriseSunsetProgress(
54+
context,
55+
sunrise = dailyBean.sunrise,
56+
sunset = dailyBean.sunset,
57+
)
58+
}
59+
}
60+
Spacer(modifier = Modifier.height(10.dp))
61+
}
62+
63+
64+
/**
65+
* 日出日落图
66+
*
67+
* @param sunrise 日出时间
68+
* @param sunset 日落时间
69+
*/
70+
@Composable
71+
fun SunriseSunsetProgress(context: Context, sunrise: String, sunset: String) {
72+
val result = getAccounted(sunrise, sunset)
73+
val bitmap = getBitmapFromVectorDrawable(context, R.drawable.x_sunny)
74+
val image = bitmap?.asImageBitmap()
75+
Column(
76+
modifier = Modifier
77+
.fillMaxWidth()
78+
.padding(horizontal = 10.dp)
79+
) {
80+
81+
Canvas(
82+
modifier = Modifier
83+
.fillMaxWidth()
84+
.height(50.dp)
85+
.padding(horizontal = 10.dp)
86+
) {
87+
val path = Path()
88+
path.moveTo(0f, size.height)
89+
// 三阶贝塞尔曲线
90+
path.cubicTo(
91+
0f,
92+
size.height,
93+
size.width / 2,
94+
-size.height,
95+
size.width,
96+
size.height
97+
)
98+
99+
drawPath(
100+
path = path, color = Color(red = 255, green = 193, blue = 7, alpha = 255),
101+
style = Stroke(width = 3f)
102+
)
103+
104+
val mRadius = size.width / 2
105+
val y = mRadius - (mRadius * sin((180 * result) * Math.PI / 180)) - 45
106+
if (image != null) {
107+
drawImage(
108+
image = image,
109+
topLeft = Offset(size.width * result, y.toFloat())
110+
)
111+
}
112+
113+
drawPoints(
114+
points = arrayListOf(
115+
Offset(0f, size.height),
116+
Offset(size.width, size.height)
117+
),
118+
pointMode = PointMode.Points,
119+
color = Color(red = 255, green = 193, blue = 7, alpha = 255),
120+
strokeWidth = 20f,
121+
cap = StrokeCap.Round,
122+
)
123+
124+
}
125+
126+
Row(
127+
modifier = Modifier
128+
.fillMaxWidth()
129+
.padding(vertical = 10.dp)
130+
) {
131+
Text(
132+
modifier = Modifier
133+
.wrapContentWidth(Alignment.Start),
134+
text = "${stringResource(id = R.string.sun_sunrise)}$sunrise",
135+
fontSize = 12.sp,
136+
)
137+
138+
Spacer(modifier = Modifier.weight(1f))
139+
140+
Text(
141+
modifier = Modifier
142+
.wrapContentWidth(Alignment.End),
143+
text = "${stringResource(id = R.string.sun_sunset)}$sunset",
144+
fontSize = 12.sp,
145+
)
146+
}
147+
}
148+
149+
}
150+
151+
/**
152+
* SVG 转 Bitmap
153+
* @param context 上下文
154+
* @param drawableId SVG资源
155+
* @return
156+
*/
157+
fun getBitmapFromVectorDrawable(context: Context?, drawableId: Int): Bitmap? {
158+
val drawable = ContextCompat.getDrawable(context!!, drawableId)
159+
val bitmap = Bitmap.createBitmap(
160+
drawable!!.intrinsicWidth,
161+
drawable.intrinsicHeight, Bitmap.Config.ARGB_8888
162+
)
163+
val canvas = android.graphics.Canvas(bitmap)
164+
drawable.setBounds(0, 0, canvas.width, canvas.height)
165+
drawable.draw(canvas)
166+
return bitmap
167+
}
168+
169+
/**
170+
* 获取当前时间占白天时间的百分比
171+
*
172+
* @param sunrise 日出
173+
* @param sunset 日落
174+
*
175+
* @return 百分比
176+
*/
177+
private fun getAccounted(sunrise: String, sunset: String): Float {
178+
val calendar = Calendar.getInstance()
179+
val currentMinutes = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE)
180+
val sunriseMinutes = getMinutes(sunrise)
181+
val sunsetMinutes = getMinutes(sunset)
182+
val accounted =
183+
(currentMinutes.toFloat() - sunriseMinutes.toFloat()) / (sunsetMinutes.toFloat() - sunriseMinutes.toFloat())
184+
XLog.w("currentMinutes:$currentMinutes sunriseMinutes:$sunriseMinutes sunsetMinutes:$sunsetMinutes accounted:$accounted")
185+
val result = if (accounted > 1) {
186+
1f
187+
} else if (accounted < 0) {
188+
0f
189+
} else {
190+
accounted
191+
}
192+
XLog.w("result:$result")
193+
return result
194+
}
195+
196+
/**
197+
* 时间转分钟数
198+
*
199+
* @param sunrise 时间,格式为"14:22"
200+
*
201+
* @return 总分钟数
202+
*/
203+
fun getMinutes(sunrise: String): Int {
204+
val hour = sunrise.substring(0, 2).toInt()
205+
val minutes = sunrise.substring(3, 5).toInt()
206+
val total = hour * 60 + minutes
207+
XLog.w("hour:$hour minutes:$minutes total:$total")
208+
return total
209+
}

app/src/main/java/com/zj/weather/view/weather/widget/WeatherContent.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,20 @@ package com.zj.weather.view.weather.widget
33
import androidx.compose.foundation.ScrollState
44
import androidx.compose.foundation.layout.*
55
import androidx.compose.foundation.verticalScroll
6+
import androidx.compose.material.Text
67
import androidx.compose.runtime.Composable
78
import androidx.compose.ui.Alignment
89
import androidx.compose.ui.Modifier
10+
import androidx.compose.ui.res.stringResource
11+
import androidx.compose.ui.text.style.TextAlign
912
import androidx.compose.ui.unit.Dp
1013
import androidx.compose.ui.unit.TextUnit
1114
import androidx.compose.ui.unit.dp
15+
import androidx.compose.ui.unit.sp
1216
import com.zj.model.WeatherModel
1317
import com.zj.model.room.entity.CityInfo
18+
import com.zj.model.weather.WeatherDailyBean
19+
import com.zj.weather.R
1420

1521
@Composable
1622
fun WeatherContent(
@@ -62,8 +68,21 @@ fun WeatherContent(
6268
// 当天具体天气数值
6369
DayWeatherContent(weatherModel)
6470

71+
// 日出日落
72+
SunriseSunsetContent(weatherModel?.dailyBean)
73+
6574
// 当天生活指数
6675
LifeWeatherContent(weatherModel?.weatherLifeList)
76+
77+
// 数据源
78+
Text(
79+
text = stringResource(id = R.string.data_source),
80+
fontSize = 12.sp,
81+
textAlign = TextAlign.Center,
82+
modifier = Modifier
83+
.fillMaxWidth()
84+
.padding(top = 10.dp, bottom = 35.dp)
85+
)
6786
}
6887
}
6988
}

config.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ ext {
33
compileSdkVersion : 31,
44
minSdkVersion : 25,
55
targetSdkVersion : 31,
6-
versionCode : 28,
7-
versionName : "3.3.0",
6+
versionCode : 29,
7+
versionName : "3.3.1",
88
testInstrumentationRunner: "androidx.test.runner.AndroidJUnitRunner",
99
consumerProguardFiles : 'consumer-rules.pro',
1010
jvmTarget : '11',

0 commit comments

Comments
 (0)