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
         return mIconSource != null;
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
     public int getItemId() {
52
     public int getItemId() {

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

1
 package com.reactnativenavigation.utils;
1
 package com.reactnativenavigation.utils;
2
 
2
 
3
 import android.content.Context;
3
 import android.content.Context;
4
+import android.content.res.Resources;
4
 import android.graphics.Bitmap;
5
 import android.graphics.Bitmap;
5
 import android.graphics.BitmapFactory;
6
 import android.graphics.BitmapFactory;
6
 import android.graphics.drawable.BitmapDrawable;
7
 import android.graphics.drawable.BitmapDrawable;
7
 import android.graphics.drawable.Drawable;
8
 import android.graphics.drawable.Drawable;
8
 import android.net.Uri;
9
 import android.net.Uri;
10
+import android.util.DisplayMetrics;
9
 
11
 
10
 import com.reactnativenavigation.BuildConfig;
12
 import com.reactnativenavigation.BuildConfig;
11
 
13
 
19
     private static ResourceDrawableIdHelper sResDrawableIdHelper = new ResourceDrawableIdHelper();
21
     private static ResourceDrawableIdHelper sResDrawableIdHelper = new ResourceDrawableIdHelper();
20
 
22
 
21
     public static Drawable getIcon(Context ctx, String iconSource) {
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
         if (iconSource == null) {
33
         if (iconSource == null) {
23
             return null;
34
             return null;
24
         }
35
         }
32
             } else {
43
             } else {
33
                 URL url = new URL(iconUri.toString());
44
                 URL url = new URL(iconUri.toString());
34
                 Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
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
             return icon;
50
             return icon;
38
         } catch (Exception e) {
51
         } catch (Exception e) {

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

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