Skip to content

Commit c640258

Browse files
author
zhujiang2
committed
修改日出日落控件
1 parent 80124cf commit c640258

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import androidx.compose.ui.Modifier
1515
import androidx.compose.ui.geometry.Offset
1616
import androidx.compose.ui.graphics.*
1717
import androidx.compose.ui.graphics.drawscope.Stroke
18+
import androidx.compose.ui.graphics.drawscope.clipRect
1819
import androidx.compose.ui.platform.LocalContext
1920
import androidx.compose.ui.res.stringResource
2021
import androidx.compose.ui.unit.dp
@@ -23,6 +24,7 @@ import androidx.core.content.ContextCompat
2324
import com.zj.model.weather.WeatherDailyBean
2425
import com.zj.utils.XLog
2526
import com.zj.weather.R
27+
import kotlin.math.cos
2628
import kotlin.math.sin
2729

2830

@@ -69,6 +71,9 @@ fun SunriseSunsetContent(dailyBean: WeatherDailyBean.DailyBean?) {
6971
*/
7072
@Composable
7173
fun SunriseSunsetProgress(context: Context, sunrise: String, sunset: String) {
74+
if (sunrise.isEmpty() || sunset.isEmpty()) {
75+
return
76+
}
7277
val result = getAccounted(sunrise, sunset)
7378
val bitmap = getBitmapFromVectorDrawable(context, R.drawable.x_sunny)
7479
val image = bitmap?.asImageBitmap()
@@ -81,32 +86,29 @@ fun SunriseSunsetProgress(context: Context, sunrise: String, sunset: String) {
8186
Canvas(
8287
modifier = Modifier
8388
.fillMaxWidth()
84-
.height(50.dp)
89+
.height(150.dp)
8590
.padding(horizontal = 10.dp)
8691
) {
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-
)
9892

99-
drawPath(
100-
path = path, color = Color(red = 255, green = 193, blue = 7, alpha = 255),
101-
style = Stroke(width = 3f)
102-
)
93+
94+
clipRect(0f, 0f, size.width, size.height/2, ClipOp.Intersect) {
95+
drawCircle(
96+
color = Color(red = 255, green = 193, blue = 7, alpha = 255),
97+
radius = size.width / 2,
98+
center = Offset(size.width / 2, size.height),
99+
style = Stroke(
100+
width = 3f,
101+
)
102+
)
103+
}
103104

104105
val mRadius = size.width / 2
105-
val y = mRadius - (mRadius * sin((180 * result) * Math.PI / 180)) - 45
106+
val x = mRadius - (mRadius * cos((180 * result) * Math.PI / 180)) - 40
107+
val y = mRadius - (mRadius * sin((180 * result) * Math.PI / 180)) - 30
106108
if (image != null) {
107109
drawImage(
108110
image = image,
109-
topLeft = Offset(size.width * result, y.toFloat())
111+
topLeft = Offset(x.toFloat(), y.toFloat())
110112
)
111113
}
112114

@@ -200,9 +202,12 @@ private fun getAccounted(sunrise: String, sunset: String): Float {
200202
*
201203
* @return 总分钟数
202204
*/
203-
fun getMinutes(sunrise: String): Int {
204-
val hour = sunrise.substring(0, 2).toInt()
205-
val minutes = sunrise.substring(3, 5).toInt()
205+
fun getMinutes(sunrise: String?): Int {
206+
val s = if (sunrise == null || sunrise.isEmpty()) {
207+
"00:00"
208+
} else sunrise
209+
val hour = s.substring(0, 2).toInt()
210+
val minutes = s.substring(3, 5).toInt()
206211
val total = hour * 60 + minutes
207212
XLog.w("hour:$hour minutes:$minutes total:$total")
208213
return total

0 commit comments

Comments
 (0)