From 2f63393f18ec593659520927e4c015f84c2b1cf9 Mon Sep 17 00:00:00 2001 From: cloudroam <cloudroam> Date: 星期四, 06 三月 2025 14:34:05 +0800 Subject: [PATCH] fix: 热力图矩阵 --- app/src/main/res/layout/layout_year_stats.xml | 43 ++++++++++++++------- app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt | 62 ++++++++++++++++++++++--------- app/src/main/res/drawable/avg_package.xml | 12 ++++++ 3 files changed, 85 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt index 794ece9..dadbe96 100644 --- a/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt +++ b/app/src/main/java/com/example/firstapp/ui/dashboard/DashboardFragment.kt @@ -423,24 +423,44 @@ // 更新UI binding.layoutWeekStats.heatmapYearly.apply { - // 清除现有的子视图 removeAllViews() - // 创建网格布局 val gridLayout = GridLayout(context).apply { - rowCount = 7 - columnCount = 52 + rowCount = 8 // 增加一行用于显示月份 + columnCount = 53 // 增加一列用于显示星期标签 } - // 添加日期标签 - val dayLabels = arrayOf("周日", "周一", "周二", "周三", "周四", "周五", "周六") - for (i in 0..6) { + // 添加月份标签 + val months = arrayOf("1月", "2月", "3月", "4月", "5月", "6月", + "7月", "8月", "9月", "10月", "11月", "12月") + months.forEachIndexed { index, month -> val label = TextView(context).apply { - text = dayLabels[i] + text = month textSize = 10f - setPadding(0, 0, 8, 0) + setPadding(0, 0, 8, 4) + // 计算每个月份标签的位置 + val weekPosition = (index * 4.3).toInt() + val params = GridLayout.LayoutParams() + params.columnSpec = GridLayout.spec(weekPosition + 1) + params.rowSpec = GridLayout.spec(0) + layoutParams = params } gridLayout.addView(label) + } + + // 添加星期标签 + val dayLabels = arrayOf("周一", "周二", "周三", "周四", "周五", "周六", "周日") + dayLabels.forEachIndexed { index, label -> + val textView = TextView(context).apply { + text = label + textSize = 10f + setPadding(4, 0, 8, 0) + val params = GridLayout.LayoutParams() + params.columnSpec = GridLayout.spec(0) + params.rowSpec = GridLayout.spec(index + 1) + layoutParams = params + } + gridLayout.addView(textView) } // 添加热力图单元格 @@ -448,12 +468,15 @@ for (week in 0..51) { val count = heatmapMatrix[day][week] val cell = View(context).apply { - layoutParams = ViewGroup.LayoutParams( - resources.getDimensionPixelSize(R.dimen.heatmap_cell_size), - resources.getDimensionPixelSize(R.dimen.heatmap_cell_size) - ) + val params = GridLayout.LayoutParams().apply { + width = resources.getDimensionPixelSize(R.dimen.heatmap_cell_size) + height = resources.getDimensionPixelSize(R.dimen.heatmap_cell_size) + columnSpec = GridLayout.spec(week + 1) + rowSpec = GridLayout.spec(day + 1) + setMargins(1, 1, 1, 1) + } + layoutParams = params setBackgroundColor(getHeatmapColor(count)) - setPadding(1, 1, 1, 1) } gridLayout.addView(cell) } @@ -465,12 +488,11 @@ } private fun getHeatmapColor(count: Int): Int { - // 根据数量返回不同深浅的颜色 return when { count == 0 -> Color.parseColor("#EBEDF0") - count <= 2 -> Color.parseColor("#9BE9A8") - count <= 4 -> Color.parseColor("#40C463") - count <= 6 -> Color.parseColor("#30A14E") + count == 1 -> Color.parseColor("#9BE9A8") + count == 2 -> Color.parseColor("#40C463") + count <= 4 -> Color.parseColor("#30A14E") else -> Color.parseColor("#216E39") } } @@ -482,6 +504,7 @@ binding.recyclerPackages.visibility = View.VISIBLE binding.layoutWeekStats.root.visibility = View.GONE binding.layoutYearStats.root.visibility = View.GONE + binding.cardPackageStats.visibility = View.VISIBLE // 显示包裹统计卡片 } DateType.WEEK, DateType.MONTH -> { // 周和月视图显示柱状图和饼图,隐藏包裹列表 @@ -490,6 +513,7 @@ binding.layoutYearStats.root.visibility = View.GONE binding.layoutWeekStats.chartDailyPackages.visibility = View.VISIBLE binding.layoutWeekStats.heatmapYearly.visibility = View.GONE + binding.cardPackageStats.visibility = View.VISIBLE // 显示包裹统计卡片 updateBarChartData() updatePieChartData() } @@ -500,8 +524,10 @@ binding.layoutYearStats.root.visibility = View.VISIBLE binding.layoutWeekStats.chartDailyPackages.visibility = View.GONE binding.layoutWeekStats.heatmapYearly.visibility = View.VISIBLE + binding.cardPackageStats.visibility = View.GONE // 隐藏包裹统计卡片 updateHeatmapData() updatePieChartData() + updateYearlyStats() // 更新年度统计数据 } } } diff --git a/app/src/main/res/drawable/avg_package.xml b/app/src/main/res/drawable/avg_package.xml new file mode 100644 index 0000000..cf552e9 --- /dev/null +++ b/app/src/main/res/drawable/avg_package.xml @@ -0,0 +1,12 @@ +<vector xmlns:android="http://schemas.android.com/apk/res/android" + android:width="32dp" + android:height="32dp" + android:viewportWidth="1024" + android:viewportHeight="1024"> + <path + android:pathData="M789.3,996.3h-537.6c-81.1,0 -147.2,-66.1 -147.2,-147.2V409.6c0,-81.1 66.1,-147.2 147.2,-147.2h537.6c81.1,0 147.2,66.1 147.2,147.2v439.5c0,81.1 -66.1,147.2 -147.2,147.2zM251.7,326.4c-44.8,0 -83.2,36.3 -83.2,83.2v439.5c0,44.8 36.3,83.2 83.2,83.2h537.6c44.8,0 83.2,-36.3 83.2,-83.2V409.6c0,-44.8 -36.3,-83.2 -83.2,-83.2h-537.6z" + android:fillColor="#543E3E"/> + <path + android:pathData="M693.3,437.3c-17.1,0 -32,-14.9 -32,-32V198.4c0,-64 -61.9,-115.2 -138.7,-115.2S384,134.4 384,198.4v206.9c0,17.1 -14.9,32 -32,32s-32,-14.9 -32,-32V198.4c0,-98.1 91.7,-179.2 202.7,-179.2s202.7,78.9 202.7,179.2v206.9c0,17.1 -14.9,32 -32,32zM640,827.7H403.2c-17.1,0 -32,-14.9 -32,-32s14.9,-32 32,-32H640c17.1,0 32,14.9 32,32s-14.9,32 -32,32z" + android:fillColor="#FFBB12"/> +</vector> diff --git a/app/src/main/res/layout/layout_year_stats.xml b/app/src/main/res/layout/layout_year_stats.xml index 7f3c243..aa8a654 100644 --- a/app/src/main/res/layout/layout_year_stats.xml +++ b/app/src/main/res/layout/layout_year_stats.xml @@ -13,24 +13,34 @@ android:orientation="vertical" android:gravity="center"> - <ImageView - android:layout_width="48dp" - android:layout_height="48dp" - android:src="@drawable/ic_package" /> - <TextView android:id="@+id/text_total_packages" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:textStyle="bold" - android:text="0个" /> + android:textColor="@color/purple_500" + android:text="4"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="年度包裹总数" /> + android:layout_marginTop="4dp" + android:text="年度包裹总数" + android:textSize="12sp"/> + + <ImageView + android:layout_width="24dp" + android:layout_height="24dp" + android:layout_marginTop="8dp" + android:src="@drawable/resource_package"/> </LinearLayout> + + <!-- 分隔线 --> + <View + android:layout_width="1dp" + android:layout_height="match_parent" + android:background="#E0E0E0"/> <!-- 平均每天包裹数 --> <LinearLayout @@ -40,22 +50,27 @@ android:orientation="vertical" android:gravity="center"> - <ImageView - android:layout_width="48dp" - android:layout_height="48dp" - android:src="@drawable/ic_average" /> - <TextView android:id="@+id/text_daily_average" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="24sp" android:textStyle="bold" - android:text="0.00" /> + android:textColor="@color/purple_500" + android:text="0.29"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" - android:text="平均每天包裹数" /> + android:layout_marginTop="4dp" + android:text="平均每天包裹数" + android:textSize="12sp"/> + + <ImageView + android:layout_width="24dp" + android:layout_height="24dp" + android:layout_marginTop="8dp" + android:src="@drawable/avg_package"/> </LinearLayout> + </LinearLayout> \ No newline at end of file -- Gitblit v1.9.3