diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java b/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java index df739994d..c7a795471 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageViewManager.java @@ -61,11 +61,6 @@ public void setSource(FastImageViewWithUrl view, @Nullable ReadableMap source) { view.setSource(source); } - @ReactProp(name = "disableTransformation") - public void setDisableTransformation(FastImageViewWithUrl view, @Nullable Boolean disableTransformation) { - view.disableTransformation(disableTransformation); - } - @ReactProp(name = "defaultSource") public void setDefaultSource(FastImageViewWithUrl view, @Nullable String source) { view.setDefaultSource( diff --git a/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java b/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java index 34e9b4e32..9dfec0c97 100644 --- a/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java +++ b/android/src/main/java/com/dylanvann/fastimage/FastImageViewWithUrl.java @@ -39,7 +39,6 @@ class FastImageViewWithUrl extends AppCompatImageView { private boolean mNeedsReload = false; private ReadableMap mSource = null; private Drawable mDefaultSource = null; - private boolean mDisableTransformation = false; public GlideUrl glideUrl; @@ -52,11 +51,6 @@ public void setSource(@Nullable ReadableMap source) { mSource = source; } - public void disableTransformation(@Nullable boolean disableTransform) { - mNeedsReload = true; - mDisableTransformation = disableTransform; - } - public void setDefaultSource(@Nullable Drawable source) { mNeedsReload = true; mDefaultSource = source; @@ -182,11 +176,8 @@ public boolean onResourceReady(File resource, Object model, Target target, .apply(FastImageViewConverter .getOptions(context, imageSource, mSource) .placeholder(mDefaultSource) // show until loaded - .fallback(mDefaultSource)); // null will not be treated as error - - if (mDisableTransformation) { - builder = builder.dontTransform(); - } + .fallback(mDefaultSource)) + .transform(new ResizeTransformation()); if (key != null) builder.listener(new FastImageRequestListener(key)); diff --git a/android/src/main/java/com/dylanvann/fastimage/ResizeTransformation.java b/android/src/main/java/com/dylanvann/fastimage/ResizeTransformation.java new file mode 100644 index 000000000..ec44d9524 --- /dev/null +++ b/android/src/main/java/com/dylanvann/fastimage/ResizeTransformation.java @@ -0,0 +1,41 @@ +package com.dylanvann.fastimage; + +import android.content.Context; +import android.graphics.Bitmap; + +import androidx.annotation.NonNull; + +import com.bumptech.glide.load.Transformation; +import com.bumptech.glide.load.engine.Resource; +import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; +import com.bumptech.glide.load.resource.bitmap.BitmapResource; + +import java.security.MessageDigest; + +public class ResizeTransformation implements Transformation { + + private final double MAX_BYTES = 25000000.0; + + @NonNull + @Override + public Resource transform(@NonNull Context context, @NonNull Resource resource, int outWidth, int outHeight) { + Bitmap toTransform = resource.get(); + + if (toTransform.getByteCount() > MAX_BYTES) { + double scaleFactor = Math.sqrt(MAX_BYTES / (double) toTransform.getByteCount()); + int newHeight = (int) (outHeight * scaleFactor); + int newWidth = (int) (outWidth * scaleFactor); + + BitmapPool pool = GlideApp.get(context).getBitmapPool(); + Bitmap scaledBitmap = Bitmap.createScaledBitmap(toTransform, newWidth, newHeight, true); + return BitmapResource.obtain(scaledBitmap, pool); + } + + return resource; + } + + @Override + public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) { + messageDigest.update(("ResizeTransformation").getBytes()); + } +} diff --git a/src/index.tsx b/src/index.tsx index 891c0975a..d68fadd25 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,20 +1,20 @@ import React, { forwardRef, memo } from 'react' import { - View, - Image, - NativeModules, - requireNativeComponent, - StyleSheet, + AccessibilityProps, + ColorValue, FlexStyle, + Image, + ImageRequireSource, LayoutChangeEvent, + NativeModules, + Platform, ShadowStyleIOS, StyleProp, + StyleSheet, TransformsStyle, - ImageRequireSource, - Platform, - AccessibilityProps, + View, ViewProps, - ColorValue, + requireNativeComponent, } from 'react-native' export type ResizeMode = 'contain' | 'cover' | 'stretch' | 'center' @@ -54,8 +54,8 @@ export type Source = { export interface OnLoadStartEvent { nativeEvent: { - cachePath: string | null; - }; + cachePath: string | null + } } export interface OnLoadEvent { @@ -124,13 +124,6 @@ export interface FastImageProps extends AccessibilityProps, ViewProps { */ tintColor?: ColorValue - /** - * If supplied, the original size of the resource without any transformations will be displayed. - * - * @platform android - */ - disableTransformation?: boolean - /** * A unique identifier for this element to be used in UI Automation testing scripts. */