@@ -15,7 +15,6 @@ import androidx.compose.ui.Modifier
1515import androidx.compose.ui.geometry.Offset
1616import androidx.compose.ui.graphics.*
1717import androidx.compose.ui.graphics.drawscope.Stroke
18- import androidx.compose.ui.graphics.drawscope.clipRect
1918import androidx.compose.ui.platform.LocalContext
2019import androidx.compose.ui.res.stringResource
2120import androidx.compose.ui.unit.dp
@@ -24,8 +23,6 @@ import androidx.core.content.ContextCompat
2423import com.zj.model.weather.WeatherDailyBean
2524import com.zj.utils.XLog
2625import com.zj.weather.R
27- import kotlin.math.cos
28- import kotlin.math.sin
2926
3027
3128@Composable
@@ -71,9 +68,6 @@ fun SunriseSunsetContent(dailyBean: WeatherDailyBean.DailyBean?) {
7168 */
7269@Composable
7370fun SunriseSunsetProgress (context : Context , sunrise : String , sunset : String ) {
74- if (sunrise.isEmpty() || sunset.isEmpty()) {
75- return
76- }
7771 val result = getAccounted(sunrise, sunset)
7872 val bitmap = getBitmapFromVectorDrawable(context, R .drawable.x_sunny)
7973 val image = bitmap?.asImageBitmap()
@@ -86,29 +80,41 @@ fun SunriseSunsetProgress(context: Context, sunrise: String, sunset: String) {
8680 Canvas (
8781 modifier = Modifier
8882 .fillMaxWidth()
89- .height(150 .dp)
83+ .height(50 .dp)
9084 .padding(horizontal = 10 .dp)
9185 ) {
86+ val path = Path ()
87+ path.moveTo(0f , size.height)
88+ // 三阶贝塞尔曲线
89+ path.cubicTo(
90+ 0f ,
91+ size.height,
92+ size.width / 2 ,
93+ - size.height,
94+ size.width,
95+ size.height
96+ )
9297
98+ drawPath(
99+ path = path, color = Color (red = 255 , green = 193 , blue = 7 , alpha = 255 ),
100+ style = Stroke (width = 3f )
101+ )
93102
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- }
104103
105- val mRadius = size.width / 2
106- val x = mRadius - (mRadius * cos((180 * result) * Math .PI / 180 )) - 40
107- val y = mRadius - (mRadius * sin((180 * result) * Math .PI / 180 )) - 30
104+ val evaluate = evaluate(
105+ result,
106+ floatArrayOf(0f , size.height),
107+ floatArrayOf(size.width / 4 , - size.height / 2 ),
108+ floatArrayOf(size.width / 4 * 3 , 0f ),
109+ floatArrayOf(size.width, size.height)
110+ )
108111 if (image != null ) {
109112 drawImage(
110113 image = image,
111- topLeft = Offset (x.toFloat(), y.toFloat())
114+ topLeft = Offset (
115+ evaluate[0 ] - 45 ,
116+ evaluate[1 ] - 45
117+ )
112118 )
113119 }
114120
@@ -150,6 +156,33 @@ fun SunriseSunsetProgress(context: Context, sunrise: String, sunset: String) {
150156
151157}
152158
159+
160+ /* *
161+ *
162+ * @param fraction 变量
163+ * @param point0 贝塞尔曲线起点
164+ * @param point3 贝塞尔曲线终点
165+ * @return 因为需要的点是从下到上....所以p0,p1,p2,p3的点是从下打上的
166+ */
167+ fun evaluate (
168+ fraction : Float ,
169+ point0 : FloatArray ,
170+ point1 : FloatArray ,
171+ point2 : FloatArray ,
172+ point3 : FloatArray
173+ ): FloatArray {
174+ val currentPosition = FloatArray (2 )
175+ // 贝塞尔公式计算X点
176+ currentPosition[0 ] =
177+ point0[0 ] * (1 - fraction) * (1 - fraction) * (1 - fraction) + point1[0 ] * 3 * fraction * (
178+ 1 - fraction) * (1 - fraction) + point2[0 ] * 3 * (1 - fraction) * fraction * fraction + point3[0 ] * fraction * fraction * fraction
179+ // 贝塞尔公式计算Y点
180+ currentPosition[1 ] =
181+ point0[1 ] * (1 - fraction) * (1 - fraction) * (1 - fraction) + point1[1 ] * 3 * fraction * (1 - fraction) * (1 - fraction) + point2[1 ] * 3 * (
182+ 1 - fraction) * fraction * fraction + point3[1 ] * fraction * fraction * fraction
183+ return currentPosition
184+ }
185+
153186/* *
154187 * SVG 转 Bitmap
155188 * @param context 上下文
@@ -202,13 +235,10 @@ private fun getAccounted(sunrise: String, sunset: String): Float {
202235 *
203236 * @return 总分钟数
204237 */
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()
238+ fun getMinutes (sunrise : String ): Int {
239+ val hour = sunrise.substring(0 , 2 ).toInt()
240+ val minutes = sunrise.substring(3 , 5 ).toInt()
211241 val total = hour * 60 + minutes
212242 XLog .w(" hour:$hour minutes:$minutes total:$total " )
213243 return total
214- }
244+ }
0 commit comments