下面列出了怎么用com.bumptech.glide.load.model.ModelLoader的API类实例代码及写法,或者点击链接到github查看源代码。
private static <A, Z, R> FixedLoadProvider<A, ImageVideoWrapper, Z, R> buildProvider(Glide glide,
ModelLoader<A, InputStream> streamModelLoader,
ModelLoader<A, ParcelFileDescriptor> fileDescriptorModelLoader, Class<Z> resourceClass,
Class<R> transcodedClass,
ResourceTranscoder<Z, R> transcoder) {
if (streamModelLoader == null && fileDescriptorModelLoader == null) {
return null;
}
if (transcoder == null) {
transcoder = glide.buildTranscoder(resourceClass, transcodedClass);
}
DataLoadProvider<ImageVideoWrapper, Z> dataLoadProvider = glide.buildDataProvider(ImageVideoWrapper.class,
resourceClass);
ImageVideoModelLoader<A> modelLoader = new ImageVideoModelLoader<A>(streamModelLoader,
fileDescriptorModelLoader);
return new FixedLoadProvider<A, ImageVideoWrapper, Z, R>(modelLoader, transcoder, dataLoadProvider);
}
private static <A, R> FixedLoadProvider<A, ImageVideoWrapper, Bitmap, R> buildProvider(Glide glide,
ModelLoader<A, InputStream> streamModelLoader,
ModelLoader<A, ParcelFileDescriptor> fileDescriptorModelLoader,
Class<R> transcodedClass, ResourceTranscoder<Bitmap, R> transcoder) {
if (streamModelLoader == null && fileDescriptorModelLoader == null) {
return null;
}
if (transcoder == null) {
transcoder = glide.buildTranscoder(Bitmap.class, transcodedClass);
}
DataLoadProvider<ImageVideoWrapper, Bitmap> loadProvider = glide.buildDataProvider(ImageVideoWrapper.class,
Bitmap.class);
ImageVideoModelLoader<A> modelLoader = new ImageVideoModelLoader<A>(streamModelLoader,
fileDescriptorModelLoader);
return new FixedLoadProvider<A, ImageVideoWrapper, Bitmap, R>(modelLoader, transcoder, loadProvider);
}
public FixedLoadProvider(ModelLoader<A, T> modelLoader, ResourceTranscoder<Z, R> transcoder,
DataLoadProvider<T, Z> dataLoadProvider) {
if (modelLoader == null) {
throw new NullPointerException("ModelLoader must not be null");
}
this.modelLoader = modelLoader;
if (transcoder == null) {
throw new NullPointerException("Transcoder must not be null");
}
this.transcoder = transcoder;
if (dataLoadProvider == null) {
throw new NullPointerException("DataLoadProvider must not be null");
}
this.dataLoadProvider = dataLoadProvider;
}
/**
* A callback method that should never be invoked directly.
*/
@Override
public void onSizeReady(int width, int height) {
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logV("Got onSizeReady in " + LogTime.getElapsedMillis(startTime));
}
if (status != Status.WAITING_FOR_SIZE) {
return;
}
status = Status.RUNNING;
width = Math.round(sizeMultiplier * width);
height = Math.round(sizeMultiplier * height);
ModelLoader<A, T> modelLoader = loadProvider.getModelLoader();
final DataFetcher<T> dataFetcher = modelLoader.getResourceFetcher(model, width, height);
if (dataFetcher == null) {
onException(new Exception("Failed to load model: \'" + model + "\'"));
return;
}
ResourceTranscoder<Z, R> transcoder = loadProvider.getTranscoder();
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logV("finished setup for calling load in " + LogTime.getElapsedMillis(startTime));
}
loadedFromMemoryCache = true;
loadStatus = engine.load(signature, width, height, dataFetcher, loadProvider, transformation, transcoder,
priority, isMemoryCacheable, diskCacheStrategy, this);
loadedFromMemoryCache = resource != null;
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logV("finished onSizeReady in " + LogTime.getElapsedMillis(startTime));
}
}
private <T> DrawableTypeRequest<T> loadGeneric(Class<T> modelClass) {
ModelLoader<T, InputStream> streamModelLoader = Glide.buildStreamModelLoader(modelClass, context);
ModelLoader<T, ParcelFileDescriptor> fileDescriptorModelLoader =
Glide.buildFileDescriptorModelLoader(modelClass, context);
if (modelClass != null && streamModelLoader == null && fileDescriptorModelLoader == null) {
throw new IllegalArgumentException("Unknown type " + modelClass + ". You must provide a Model of a type for"
+ " which there is a registered ModelLoader, if you are using a custom model, you must first call"
+ " Glide#register with a ModelLoaderFactory for your custom model class");
}
return optionsApplier.apply(
new DrawableTypeRequest<T>(modelClass, streamModelLoader, fileDescriptorModelLoader, context,
glide, requestTracker, lifecycle, optionsApplier));
}
GenericTranscodeRequest(
Class<ResourceType> transcodeClass, GenericRequestBuilder<ModelType, ?, ?, ?> other,
ModelLoader<ModelType, DataType> modelLoader, Class<DataType> dataClass, Class<ResourceType> resourceClass,
RequestManager.OptionsApplier optionsApplier) {
super(build(other.glide, modelLoader, dataClass, resourceClass, UnitTranscoder.<ResourceType>get()),
transcodeClass, other);
this.modelLoader = modelLoader;
this.dataClass = dataClass;
this.resourceClass = resourceClass;
this.optionsApplier = optionsApplier;
}
DrawableTypeRequest(Class<ModelType> modelClass, ModelLoader<ModelType, InputStream> streamModelLoader,
ModelLoader<ModelType, ParcelFileDescriptor> fileDescriptorModelLoader, Context context, Glide glide,
RequestTracker requestTracker, Lifecycle lifecycle, RequestManager.OptionsApplier optionsApplier) {
super(context, modelClass,
buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, GifBitmapWrapper.class,
GlideDrawable.class, null),
glide, requestTracker, lifecycle);
this.streamModelLoader = streamModelLoader;
this.fileDescriptorModelLoader = fileDescriptorModelLoader;
this.optionsApplier = optionsApplier;
}
public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader<GlideUrl, InputStream> urlLoader, int width, int height) {
this.context = context;
this.lastFMRestClient = lastFMRestClient;
this.model = model;
this.urlLoader = urlLoader;
this.width = width;
this.height = height;
}
BitmapTypeRequest(GenericRequestBuilder<ModelType, ?, ?, ?> other,
ModelLoader<ModelType, InputStream> streamModelLoader,
ModelLoader<ModelType, ParcelFileDescriptor> fileDescriptorModelLoader,
RequestManager.OptionsApplier optionsApplier) {
super(buildProvider(other.glide, streamModelLoader, fileDescriptorModelLoader, Bitmap.class, null),
Bitmap.class, other);
this.streamModelLoader = streamModelLoader;
this.fileDescriptorModelLoader = fileDescriptorModelLoader;
this.glide = other.glide;
this.optionsApplier = optionsApplier;
}
private static <A, R> FixedLoadProvider<A, InputStream, GifDrawable, R> buildProvider(Glide glide,
ModelLoader<A, InputStream> streamModelLoader, Class<R> transcodeClass,
ResourceTranscoder<GifDrawable, R> transcoder) {
if (streamModelLoader == null) {
return null;
}
if (transcoder == null) {
transcoder = glide.buildTranscoder(GifDrawable.class, transcodeClass);
}
DataLoadProvider<InputStream, GifDrawable> dataLoadProvider = glide.buildDataProvider(InputStream.class,
GifDrawable.class);
return new FixedLoadProvider<A, InputStream, GifDrawable, R>(streamModelLoader, transcoder, dataLoadProvider);
}
GifTypeRequest(GenericRequestBuilder<ModelType, ?, ?, ?> other,
ModelLoader<ModelType, InputStream> streamModelLoader, RequestManager.OptionsApplier optionsApplier) {
super(buildProvider(other.glide, streamModelLoader, GifDrawable.class, null), GifDrawable.class, other);
this.streamModelLoader = streamModelLoader;
this.optionsApplier = optionsApplier;
// Default to animating.
crossFade();
}
public ArtistImageFetcher(LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader<GlideUrl, InputStream> urlLoader, int width, int height, Options options) {
this.lastFMRestClient = lastFMRestClient;
this.model = model;
this.urlLoader = urlLoader;
this.width = width;
this.height = height;
mOption = options;
mLoadOriginal = model.mLoadOriginal;
mImageNumber = model.mImageNumber;
}
public ArtistImageFetcher(Context context, LastFMRestClient lastFMRestClient, ArtistImage model, ModelLoader<GlideUrl, InputStream> urlLoader, int width, int height) {
this.context = context;
this.lastFMRestClient = lastFMRestClient;
this.model = model;
this.urlLoader = urlLoader;
this.width = width;
this.height = height;
}
@Override
public @NonNull ModelLoader<StickerRemoteUri, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
return new StickerRemoteUriLoader(ApplicationDependencies.getSignalServiceMessageReceiver());
}
@Override
public @NonNull ModelLoader<GlideUrl, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
return new OkHttpUrlLoader(client);
}
@Override
public @NonNull ModelLoader<ContactPhoto, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
return new ContactPhotoLoader(context);
}
@Override
public @NonNull ModelLoader<ChunkedImageUrl, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
return new ChunkedImageUrlLoader(client);
}
@Override
public @NonNull ModelLoader<DecryptableUri, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
return new DecryptableStreamUriLoader(context);
}
@Override
public @NonNull ModelLoader<AttachmentModel, InputStream> build(@NonNull MultiModelLoaderFactory multiFactory) {
return new AttachmentStreamUriLoader();
}
@Override
public @NonNull ModelLoader<BlurHash, BlurHash> build(@NonNull MultiModelLoaderFactory multiFactory) {
return new BlurHashModelLoader();
}
@Override
public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
return new OkHttpUrlLoader(client);
}
VideoModelRequest(ModelLoader<T, ParcelFileDescriptor> loader) {
this.loader = loader;
}
ImageModelRequest(ModelLoader<T, InputStream> loader) {
this.loader = loader;
}
GenericModelRequest(ModelLoader<A, T> modelLoader, Class<T> dataClass) {
this.modelLoader = modelLoader;
this.dataClass = dataClass;
}
private static <A, T, Z, R> LoadProvider<A, T, Z, R> build(Glide glide, ModelLoader<A, T> modelLoader,
Class<T> dataClass, Class<Z> resourceClass, ResourceTranscoder<Z, R> transcoder) {
DataLoadProvider<T, Z> dataLoadProvider = glide.buildDataProvider(dataClass, resourceClass);
return new FixedLoadProvider<A, T, Z, R>(modelLoader, transcoder, dataLoadProvider);
}
public ArtistImageLoader(Context context, LastFMRestClient lastFMRestClient, ModelLoader<GlideUrl, InputStream> urlLoader) {
this.context = context;
this.lastFMClient = lastFMRestClient;
this.urlLoader = urlLoader;
}
@Override
public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
return new OkHttpUrlLoader(client);
}
@Override
public ModelLoader<String, InputStream> build(Context context, GenericLoaderFactory factories) {
return new ProgressModelLoader(mModelCache);
}
@Override
public ModelLoader<GlideUrl, InputStream> build(Context context, GenericLoaderFactory factories) {
return new HttpUrlGlideUrlLoader(modelCache);
}
@Override
public ModelLoader<String, InputStream> build(Context context, GenericLoaderFactory factories) {
return new StreamStringLoader(factories.buildModelLoader(Uri.class, InputStream.class));
}