Cache image resource ids

Reviewed By: AaaChiuuu

Differential Revision: D2895691

fb-gh-sync-id: 40824c6fdf5bdd8f0b724a7c81645d3ea911bccf
This commit is contained in:
Andrei Coman 2016-02-05 05:34:26 -08:00 committed by facebook-github-bot-5
parent 10c0758906
commit fef4196c8b
3 changed files with 68 additions and 30 deletions

View File

@ -32,6 +32,7 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
return REACT_CLASS;
}
private ResourceDrawableIdHelper mResourceDrawableIdHelper;
private @Nullable AbstractDraweeControllerBuilder mDraweeControllerBuilder;
private final @Nullable Object mCallerContext;
@ -40,12 +41,14 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
Object callerContext) {
mDraweeControllerBuilder = draweeControllerBuilder;
mCallerContext = callerContext;
mResourceDrawableIdHelper = new ResourceDrawableIdHelper();
}
public ReactImageManager() {
// Lazily initialize as FrescoModule have not been initialized yet
mDraweeControllerBuilder = null;
mCallerContext = null;
mResourceDrawableIdHelper = new ResourceDrawableIdHelper();
}
public AbstractDraweeControllerBuilder getDraweeControllerBuilder() {
@ -70,13 +73,13 @@ public class ReactImageManager extends SimpleViewManager<ReactImageView> {
// In JS this is Image.props.source.uri
@ReactProp(name = "src")
public void setSource(ReactImageView view, @Nullable String source) {
view.setSource(source);
view.setSource(source, mResourceDrawableIdHelper);
}
// In JS this is Image.props.loadingIndicatorSource.uri
@ReactProp(name = "loadingIndicatorSrc")
public void setLoadingIndicatorSource(ReactImageView view, @Nullable String source) {
view.setLoadingIndicatorSource(source);
view.setLoadingIndicatorSource(source, mResourceDrawableIdHelper);
}
@ReactProp(name = "borderColor", customType = "Color")

View File

@ -15,7 +15,6 @@ import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
@ -208,7 +207,9 @@ public class ReactImageView extends GenericDraweeView {
mIsDirty = true;
}
public void setSource(@Nullable String source) {
public void setSource(
@Nullable String source,
ResourceDrawableIdHelper resourceDrawableIdHelper) {
mUri = null;
if (source != null) {
try {
@ -221,7 +222,7 @@ public class ReactImageView extends GenericDraweeView {
// ignore malformed uri, then attempt to extract resource ID.
}
if (mUri == null) {
mUri = getResourceDrawableUri(getContext(), source);
mUri = resourceDrawableIdHelper.getResourceDrawableUri(getContext(), source);
mIsLocalImage = true;
} else {
mIsLocalImage = false;
@ -230,8 +231,10 @@ public class ReactImageView extends GenericDraweeView {
mIsDirty = true;
}
public void setLoadingIndicatorSource(@Nullable String name) {
Drawable drawable = getResourceDrawable(getContext(), name);
public void setLoadingIndicatorSource(
@Nullable String name,
ResourceDrawableIdHelper resourceDrawableIdHelper) {
Drawable drawable = resourceDrawableIdHelper.getResourceDrawable(getContext(), name);
mLoadingImageDrawable =
drawable != null ? (Drawable) new AutoRotateDrawable(drawable, 1000) : null;
mIsDirty = true;
@ -349,27 +352,4 @@ public class ReactImageView extends GenericDraweeView {
// has no control over the original size
return uri != null && (UriUtil.isLocalContentUri(uri) || UriUtil.isLocalFileUri(uri));
}
private static int getResourceDrawableId(Context context, @Nullable String name) {
if (name == null || name.isEmpty()) {
return 0;
}
return context.getResources().getIdentifier(
name.toLowerCase().replace("-", "_"),
"drawable",
context.getPackageName());
}
private static @Nullable Drawable getResourceDrawable(Context context, @Nullable String name) {
int resId = getResourceDrawableId(context, name);
return resId > 0 ? context.getResources().getDrawable(resId) : null;
}
private static Uri getResourceDrawableUri(Context context, @Nullable String name) {
int resId = getResourceDrawableId(context, name);
return resId > 0 ? new Uri.Builder()
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
.path(String.valueOf(resId))
.build() : Uri.EMPTY;
}
}

View File

@ -0,0 +1,55 @@
// Copyright 2004-present Facebook. All Rights Reserved.
package com.facebook.react.views.image;
import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import com.facebook.common.util.UriUtil;
/**
* Helper class for obtaining information about local images.
*/
/* package */ class ResourceDrawableIdHelper {
private Map<String, Integer> mResourceDrawableIdMap;
public ResourceDrawableIdHelper() {
mResourceDrawableIdMap = new HashMap<String, Integer>();
}
public int getResourceDrawableId(Context context, @Nullable String name) {
if (name == null || name.isEmpty()) {
return 0;
}
name = name.toLowerCase().replace("-", "_");
if (mResourceDrawableIdMap.containsKey(name)) {
return mResourceDrawableIdMap.get(name);
}
int id = context.getResources().getIdentifier(
name,
"drawable",
context.getPackageName());
mResourceDrawableIdMap.put(name, id);
return id;
}
public @Nullable Drawable getResourceDrawable(Context context, @Nullable String name) {
int resId = getResourceDrawableId(context, name);
return resId > 0 ? context.getResources().getDrawable(resId) : null;
}
public Uri getResourceDrawableUri(Context context, @Nullable String name) {
int resId = getResourceDrawableId(context, name);
return resId > 0 ? new Uri.Builder()
.scheme(UriUtil.LOCAL_RESOURCE_SCHEME)
.path(String.valueOf(resId))
.build() : Uri.EMPTY;
}
}