Browse Source

Fix small Toolbar icons in debug builds

Guy Carmeli 8 years ago
parent
commit
064fb1620b

+ 5
- 2
android/app/src/main/java/com/reactnativenavigation/core/objects/Button.java View File

@@ -42,8 +42,11 @@ public class Button extends JsonObject implements Serializable {
42 42
         return mIconSource != null;
43 43
     }
44 44
 
45
-    public Drawable getIcon(Context ctx) {
46
-       return IconUtils.getIcon(ctx, mIconSource);
45
+    /**
46
+     * @param dimensions The requested icon dimensions
47
+     */
48
+    public Drawable getIcon(Context ctx, int dimensions) {
49
+       return IconUtils.getIcon(ctx, mIconSource, dimensions);
47 50
     }
48 51
 
49 52
     public int getItemId() {

+ 14
- 1
android/app/src/main/java/com/reactnativenavigation/utils/IconUtils.java View File

@@ -1,11 +1,13 @@
1 1
 package com.reactnativenavigation.utils;
2 2
 
3 3
 import android.content.Context;
4
+import android.content.res.Resources;
4 5
 import android.graphics.Bitmap;
5 6
 import android.graphics.BitmapFactory;
6 7
 import android.graphics.drawable.BitmapDrawable;
7 8
 import android.graphics.drawable.Drawable;
8 9
 import android.net.Uri;
10
+import android.util.DisplayMetrics;
9 11
 
10 12
 import com.reactnativenavigation.BuildConfig;
11 13
 
@@ -19,6 +21,15 @@ public class IconUtils {
19 21
     private static ResourceDrawableIdHelper sResDrawableIdHelper = new ResourceDrawableIdHelper();
20 22
 
21 23
     public static Drawable getIcon(Context ctx, String iconSource) {
24
+        return getIcon(ctx, iconSource, -1);
25
+    }
26
+
27
+    /**
28
+     * @param iconSource Icon source. In release builds this would be a path in assets, In debug it's
29
+     *                   a url and the image needs to be decoded from input stream.
30
+     * @param dimensions The requested icon dimensions
31
+     */
32
+    public static Drawable getIcon(Context ctx, String iconSource, int dimensions) {
22 33
         if (iconSource == null) {
23 34
             return null;
24 35
         }
@@ -32,7 +43,9 @@ public class IconUtils {
32 43
             } else {
33 44
                 URL url = new URL(iconUri.toString());
34 45
                 Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
35
-                icon = new BitmapDrawable(bitmap);
46
+                bitmap = dimensions > 0 ?
47
+                        Bitmap.createScaledBitmap(bitmap, dimensions, dimensions, false) : bitmap;
48
+                icon = new BitmapDrawable(ctx.getResources(), bitmap);
36 49
             }
37 50
             return icon;
38 51
         } catch (Exception e) {

+ 4
- 3
android/app/src/main/java/com/reactnativenavigation/views/RnnToolBar.java View File

@@ -169,12 +169,14 @@ public class RnnToolBar extends Toolbar {
169 169
         private final List<Button> mNewButtons;
170 170
         private final WeakReference<RnnToolBar> mToolbarWR;
171 171
         private final Integer mTintColor;
172
+        private final int mIconDimensions;
172 173
 
173 174
         public SetupToolbarButtonsTask(RnnToolBar toolBar, Screen oldScreen, Screen newScreen) {
174 175
             mToolbarWR = new WeakReference<>(toolBar);
175 176
             mOldButtons = oldScreen == null ? null : oldScreen.getButtons();
176 177
             mNewButtons = newScreen.getButtons();
177 178
             mTintColor = newScreen.buttonsTintColor;
179
+            mIconDimensions = (int) (toolBar.getHeight() * 0.4f);
178 180
         }
179 181
 
180 182
         @Override
@@ -187,7 +189,7 @@ public class RnnToolBar extends Toolbar {
187 189
             Map<String, Drawable> icons = new HashMap<>();
188 190
             for (Button button : mNewButtons) {
189 191
                 if (button.hasIcon()) {
190
-                    icons.put(button.id, button.getIcon(context));
192
+                    icons.put(button.id, button.getIcon(context, mIconDimensions));
191 193
                 }
192 194
             }
193 195
             return icons;
@@ -221,8 +223,7 @@ public class RnnToolBar extends Toolbar {
221 223
             }
222 224
 
223 225
             // Add new screen buttons
224
-            int i;
225
-            for (i = 0; i < mNewButtons.size(); i++) {
226
+            for (int i = 0; i < mNewButtons.size(); i++) {
226 227
                 Button button = mNewButtons.get(i);
227 228
                 MenuItem item = menu.add(Menu.NONE, button.getItemId(), i, button.title);
228 229
                 item.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);