Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -182,11 +176,8 @@ public boolean onResourceReady(File resource, Object model, Target<File> 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Bitmap> {

private final double MAX_BYTES = 25000000.0;

@NonNull
@Override
public Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> 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());
}
}
29 changes: 11 additions & 18 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -54,8 +54,8 @@ export type Source = {

export interface OnLoadStartEvent {
nativeEvent: {
cachePath: string | null;
};
cachePath: string | null
}
}

export interface OnLoadEvent {
Expand Down Expand Up @@ -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.
*/
Expand Down