Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vector drawable support via appcompat #1946

Merged
merged 1 commit into from
May 24, 2017
Merged
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
2 changes: 1 addition & 1 deletion library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ dependencies {
compile project(':third_party:disklrucache')
compile project(':annotation')
compile "com.android.support:support-v4:${SUPPORT_V4_VERSION}"

provided "com.android.support:appcompat-v7:${SUPPORT_V7_VERSION}"
testCompile project(':testutil')
testCompile 'com.google.guava:guava-testlib:18.0'
testCompile "com.google.truth:truth:${TRUTH_VERSION}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.support.annotation.DrawableRes;
import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.util.Pools;
import android.support.v7.content.res.AppCompatResources;
import android.util.Log;
import com.bumptech.glide.GlideContext;
import com.bumptech.glide.Priority;
Expand Down Expand Up @@ -102,6 +104,7 @@ private enum Status {
private Drawable fallbackDrawable;
private int width;
private int height;
private static boolean shouldCallAppCompatResources = true;

public static <R> SingleRequest<R> obtain(
GlideContext glideContext,
Expand Down Expand Up @@ -343,7 +346,28 @@ private Drawable getFallbackDrawable() {
return fallbackDrawable;
}

private Drawable loadDrawable(int resourceId) {
private Drawable loadDrawable(@DrawableRes int resourceId) {
if (shouldCallAppCompatResources) {
return loadDrawableV7(resourceId);
} else {
return loadDrawableBase(resourceId);
}
}

/**
* Tries to load the drawable thanks to AppCompatResources.<br>
* This allows to parse VectorDrawables on legacy devices if the appcompat v7 is in the classpath.
*/
private Drawable loadDrawableV7(@DrawableRes int resourceId) {
try {
return AppCompatResources.getDrawable(glideContext, resourceId);
} catch (NoClassDefFoundError error) {
shouldCallAppCompatResources = false;
return loadDrawableBase(resourceId);
}
}

private Drawable loadDrawableBase(@DrawableRes int resourceId) {
Resources resources = glideContext.getResources();
return ResourcesCompat.getDrawable(resources, resourceId, requestOptions.getTheme());
}
Expand Down