diff --git a/android/app/src/main/kotlin/com/tntlikely/beecount/BeeCountWidgetProvider.kt b/android/app/src/main/kotlin/com/tntlikely/beecount/BeeCountWidgetProvider.kt index b804de86a..aa2bd3bad 100644 --- a/android/app/src/main/kotlin/com/tntlikely/beecount/BeeCountWidgetProvider.kt +++ b/android/app/src/main/kotlin/com/tntlikely/beecount/BeeCountWidgetProvider.kt @@ -5,9 +5,9 @@ import android.appwidget.AppWidgetManager import android.content.Context import android.content.Intent import android.content.SharedPreferences +import android.graphics.Bitmap import android.graphics.BitmapFactory import android.net.Uri -import android.os.Build import android.util.Log import android.widget.RemoteViews import es.antonborri.home_widget.HomeWidgetProvider @@ -38,7 +38,7 @@ class BeeCountWidgetProvider : HomeWidgetProvider() { val file = File(imagePath) Log.d(TAG, "File exists: ${file.exists()}, size: ${if(file.exists()) file.length() else 0}") - val bitmap = BitmapFactory.decodeFile(imagePath) + val bitmap = loadWidgetBitmap(imagePath) if (bitmap != null) { Log.d(TAG, "Bitmap decoded successfully: ${bitmap.width}x${bitmap.height}") setImageViewBitmap(R.id.widget_image, bitmap) @@ -96,4 +96,26 @@ class BeeCountWidgetProvider : HomeWidgetProvider() { intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TOP) return intent } + + private fun loadWidgetBitmap(imagePath: String): Bitmap? { + val bounds = BitmapFactory.Options().apply { + inJustDecodeBounds = true + } + BitmapFactory.decodeFile(imagePath, bounds) + if (bounds.outWidth <= 0 || bounds.outHeight <= 0) return null + + val maxWidth = 600 + val maxHeight = 300 + var sampleSize = 1 + while (bounds.outWidth / sampleSize > maxWidth || + bounds.outHeight / sampleSize > maxHeight + ) { + sampleSize *= 2 + } + + val options = BitmapFactory.Options().apply { + inSampleSize = sampleSize + } + return BitmapFactory.decodeFile(imagePath, options) + } } diff --git a/android/app/src/main/res/drawable/widget_background_dark.xml b/android/app/src/main/res/drawable-night/widget_background.xml similarity index 75% rename from android/app/src/main/res/drawable/widget_background_dark.xml rename to android/app/src/main/res/drawable-night/widget_background.xml index 6aa2594e3..4ee03d4e8 100644 --- a/android/app/src/main/res/drawable/widget_background_dark.xml +++ b/android/app/src/main/res/drawable-night/widget_background.xml @@ -1,6 +1,7 @@ + + android:layout_height="match_parent" + android:background="@drawable/widget_background"> diff --git a/lib/widget/home_widget_view.dart b/lib/widget/home_widget_view.dart index d91218eac..98e098176 100644 --- a/lib/widget/home_widget_view.dart +++ b/lib/widget/home_widget_view.dart @@ -1,4 +1,3 @@ -import 'dart:io'; import 'package:flutter/material.dart'; /// Flutter widget that will be rendered as the home screen widget @@ -44,32 +43,22 @@ class HomeWidgetView extends StatelessWidget { @override Widget build(BuildContext context) { - // For Android, add top/bottom padding to achieve 2:1 ratio (364x182) - // iOS uses natural 364x169 size - final isAndroid = Platform.isAndroid; - final verticalPadding = isAndroid ? (182 - 169) / 2 : 0.0; // 6.5 pixels top and bottom - return Container( width: width, height: height, - color: Colors.transparent, // Transparent background for padding area - padding: EdgeInsets.symmetric(vertical: verticalPadding), - child: Container( - width: 364, - height: 169, // Always render content at 364x169 - decoration: BoxDecoration( - gradient: LinearGradient( - begin: Alignment.topLeft, - end: Alignment.bottomRight, - colors: [ - themeColor, - Color.lerp(themeColor, Colors.black, 0.15)!, - ], - ), - borderRadius: BorderRadius.circular(16), + decoration: BoxDecoration( + gradient: LinearGradient( + begin: Alignment.topLeft, + end: Alignment.bottomRight, + colors: [ + themeColor, + Color.lerp(themeColor, Colors.black, 0.15)!, + ], ), - padding: const EdgeInsets.all(12), - child: Column( + borderRadius: BorderRadius.circular(16), + ), + padding: const EdgeInsets.all(12), + child: Column( children: [ // Header Row( @@ -172,7 +161,6 @@ class HomeWidgetView extends StatelessWidget { ), ), ], - ), ), ); } @@ -258,5 +246,4 @@ class HomeWidgetView extends StatelessWidget { ), ); } - } diff --git a/lib/widget/widget_manager.dart b/lib/widget/widget_manager.dart index b969504d3..2d66cf740 100644 --- a/lib/widget/widget_manager.dart +++ b/lib/widget/widget_manager.dart @@ -116,7 +116,9 @@ class WidgetManager { ), key: 'widgetImage', logicalSize: widgetSize, - pixelRatio: 4.0, // @4x for high resolution + // RemoteViews 会通过 Binder 传递位图;过大的 @4x 图在部分小米桌面上会更新失败, + // 表现为小部件占位但透明。1.5x 保持清晰度,同时把位图控制在安全大小内。 + pixelRatio: 1.5, ); print('✅ 小组件渲染完成');