|
| 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 | +} |
0 commit comments