Revisão | 96c04f1974b69c3ad5621efcb113a3d56fa566d5 (tree) |
---|---|
Hora | 2017-12-29 03:28:08 |
Autor | HMML <hmml3939@gmai...> |
Commiter | HMML |
Dynamic resize of weather icon for small screen device to avoid memory limit error.
@@ -5,8 +5,11 @@ import android.appwidget.AppWidgetManager; | ||
5 | 5 | import android.appwidget.AppWidgetProvider; |
6 | 6 | import android.content.Context; |
7 | 7 | import android.content.Intent; |
8 | +import android.graphics.Bitmap; | |
9 | +import android.util.DisplayMetrics; | |
8 | 10 | import android.util.Log; |
9 | 11 | import android.view.View; |
12 | +import android.view.WindowManager; | |
10 | 13 | import android.widget.RemoteViews; |
11 | 14 | |
12 | 15 | /** |
@@ -38,6 +41,11 @@ public class WeatherDispWidget extends AppWidgetProvider { | ||
38 | 41 | if (is_unit_f) |
39 | 42 | temp_unit = "℉"; |
40 | 43 | |
44 | + | |
45 | + DisplayMetrics metrics = new DisplayMetrics(); | |
46 | + ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics); | |
47 | + Log.d("widget", "Current screen width: " + metrics.widthPixels); | |
48 | + | |
41 | 49 | if (theme.day_frame != null && theme.day_frame.equals("builtin/white")) { |
42 | 50 | views.setImageViewResource(R.id.weather_background_today, R.drawable.day_frame_white); |
43 | 51 | views.setImageViewResource(R.id.weather_background_tomorrow, R.drawable.day_frame_white); |
@@ -50,7 +58,14 @@ public class WeatherDispWidget extends AppWidgetProvider { | ||
50 | 58 | views.setTextViewText(tid, temp_unit); |
51 | 59 | |
52 | 60 | if (conf.loadWeatherIdent(1) != null) { |
53 | - views.setImageViewBitmap(R.id.weather_image_today, theme.getIconBitmap(conf.loadWeatherIdent(1))); | |
61 | + Bitmap icon = theme.getIconBitmap(conf.loadWeatherIdent(1)); | |
62 | + if (metrics.widthPixels < icon.getWidth()) { | |
63 | + // resize if icon is bigger than twice to avoid memory limit exception... | |
64 | + views.setImageViewBitmap(R.id.weather_image_today, | |
65 | + Bitmap.createScaledBitmap(icon, metrics.widthPixels/2, metrics.widthPixels * icon.getHeight() / icon.getWidth() / 2, true)); | |
66 | + } else { | |
67 | + views.setImageViewBitmap(R.id.weather_image_today, icon); | |
68 | + } | |
54 | 69 | views.setTextViewText(R.id.weather_name_today, conf.loadWeatherLabel(1)); |
55 | 70 | views.setTextViewText(R.id.txt_temp_min_today, toTempString(conf.loadTempMin(1), is_unit_f)); |
56 | 71 | views.setTextViewText(R.id.txt_temp_max_today, toTempString(conf.loadTempMax(1), is_unit_f)); |
@@ -66,7 +81,13 @@ public class WeatherDispWidget extends AppWidgetProvider { | ||
66 | 81 | } |
67 | 82 | |
68 | 83 | if (conf.loadWeatherIdent(2) != null) { |
69 | - views.setImageViewBitmap(R.id.weather_image_tomorrow, theme.getIconBitmap(conf.loadWeatherIdent(2))); | |
84 | + Bitmap icon = theme.getIconBitmap(conf.loadWeatherIdent(2)); | |
85 | + if (metrics.widthPixels < icon.getWidth()) { | |
86 | + views.setImageViewBitmap(R.id.weather_image_tomorrow, | |
87 | + Bitmap.createScaledBitmap(icon, metrics.widthPixels/2, metrics.widthPixels * icon.getHeight() / icon.getWidth() / 2, true)); | |
88 | + } else { | |
89 | + views.setImageViewBitmap(R.id.weather_image_tomorrow, icon); | |
90 | + } | |
70 | 91 | views.setTextViewText(R.id.weather_name_tomorrow, conf.loadWeatherLabel(2)); |
71 | 92 | views.setTextViewText(R.id.txt_temp_min_tomorrow, toTempString(conf.loadTempMin(2), is_unit_f)); |
72 | 93 | views.setTextViewText(R.id.txt_temp_max_tomorrow, toTempString(conf.loadTempMax(2), is_unit_f)); |