下面列出了怎么用com.bumptech.glide.load.resource.transcode.ResourceTranscoder的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);
}
DecodeJob(EngineKey resultKey, int width, int height, DataFetcher<A> fetcher,
DataLoadProvider<A, T> loadProvider, Transformation<T> transformation, ResourceTranscoder<T, Z> transcoder,
DiskCacheProvider diskCacheProvider, DiskCacheStrategy diskCacheStrategy, Priority priority, FileOpener
fileOpener) {
this.resultKey = resultKey;
this.width = width;
this.height = height;
this.fetcher = fetcher;
this.loadProvider = loadProvider;
this.transformation = transformation;
this.transcoder = transcoder;
this.diskCacheProvider = diskCacheProvider;
this.diskCacheStrategy = diskCacheStrategy;
this.priority = priority;
this.fileOpener = fileOpener;
}
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));
}
}
/**
* Adds a transcoder to this request to transcode from the resource type to the given transcode type.
*
* @param transcoder The transcoder to use.
* @param transcodeClass The class of the resource type that will be transcoded to.
* @param <TranscodeType> The type of the resource that will be transcoded to.
* @return A new request builder to set options for the transcoded load.
*/
public <TranscodeType> GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType> transcode(
ResourceTranscoder<ResourceType, TranscodeType> transcoder, Class<TranscodeType> transcodeClass) {
LoadProvider<ModelType, DataType, ResourceType, TranscodeType> loadProvider = build(glide, modelLoader,
dataClass, resourceClass, transcoder);
return optionsApplier.apply(new GenericRequestBuilder<ModelType, DataType, ResourceType, TranscodeType>(
loadProvider, transcodeClass, this));
}
private GenericRequestBuilder<ModelType, DataType, File, File> getDownloadOnlyRequest() {
ResourceTranscoder<File, File> transcoder = UnitTranscoder.get();
DataLoadProvider<DataType, File> dataLoadProvider = glide.buildDataProvider(dataClass, File.class);
FixedLoadProvider<ModelType, DataType, File, File> fixedLoadProvider =
new FixedLoadProvider<ModelType, DataType, File, File>(modelLoader, transcoder, dataLoadProvider);
return optionsApplier.apply(new GenericRequestBuilder<ModelType, DataType, File, File>(fixedLoadProvider,
File.class, this))
.priority(Priority.LOW)
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.skipMemoryCache(true);
}
public EngineKey(String id, Key signature, int width, int height, ResourceDecoder cacheDecoder,
ResourceDecoder decoder, Transformation transformation, ResourceEncoder encoder,
ResourceTranscoder transcoder, Encoder sourceEncoder) {
this.id = id;
this.signature = signature;
this.width = width;
this.height = height;
this.cacheDecoder = cacheDecoder;
this.decoder = decoder;
this.transformation = transformation;
this.encoder = encoder;
this.transcoder = transcoder;
this.sourceEncoder = sourceEncoder;
}
@SuppressWarnings("rawtypes")
public EngineKey buildKey(String id, Key signature, int width, int height, ResourceDecoder cacheDecoder,
ResourceDecoder sourceDecoder, Transformation transformation, ResourceEncoder encoder,
ResourceTranscoder transcoder, Encoder sourceEncoder) {
return new EngineKey(id, signature, width, height, cacheDecoder, sourceDecoder, transformation, encoder,
transcoder, sourceEncoder);
}
/**
* {@inheritDoc}
*/
@Override
public ResourceTranscoder<Z, R> getTranscoder() {
if (transcoder != null) {
return transcoder;
} else {
return parent.getTranscoder();
}
}
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);
}
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);
}
<Z, R> ResourceTranscoder<Z, R> buildTranscoder(Class<Z> decodedClass, Class<R> transcodedClass) {
return transcoderRegistry.get(decodedClass, transcodedClass);
}
public DecodeJob(EngineKey resultKey, int width, int height, DataFetcher<A> fetcher,
DataLoadProvider<A, T> loadProvider, Transformation<T> transformation, ResourceTranscoder<T, Z> transcoder,
DiskCacheProvider diskCacheProvider, DiskCacheStrategy diskCacheStrategy, Priority priority) {
this(resultKey, width, height, fetcher, loadProvider, transformation, transcoder, diskCacheProvider,
diskCacheStrategy, priority, DEFAULT_FILE_OPENER);
}
/**
* Starts a load for the given arguments. Must be called on the main thread.
*
* <p>
* The flow for any request is as follows:
* <ul>
* <li>Check the memory cache and provide the cached resource if present</li>
* <li>Check the current set of actively used resources and return the active resource if present</li>
* <li>Check the current set of in progress loads and add the cb to the in progress load if present</li>
* <li>Start a new load</li>
* </ul>
* </p>
*
* <p>
* Active resources are those that have been provided to at least one request and have not yet been released.
* Once all consumers of a resource have released that resource, the resource then goes to cache. If the
* resource is ever returned to a new consumer from cache, it is re-added to the active resources. If the
* resource is evicted from the cache, its resources are recycled and re-used if possible and the resource is
* discarded. There is no strict requirement that consumers release their resources so active resources are
* held weakly.
* </p>
*
* @param signature A non-null unique key to be mixed into the cache key that identifies the version of the data to
* be loaded.
* @param width The target width in pixels of the desired resource.
* @param height The target height in pixels of the desired resource.
* @param fetcher The fetcher to use to retrieve data not in the disk cache.
* @param loadProvider The load provider containing various encoders and decoders use to decode and encode data.
* @param transformation The transformation to use to transform the decoded resource.
* @param transcoder The transcoder to use to transcode the decoded and transformed resource.
* @param priority The priority with which the request should run.
* @param isMemoryCacheable True if the transcoded resource can be cached in memory.
* @param diskCacheStrategy The strategy to use that determines what type of data, if any,
* will be cached in the local disk cache.
* @param cb The callback that will be called when the load completes.
*
* @param <T> The type of data the resource will be decoded from.
* @param <Z> The type of the resource that will be decoded.
* @param <R> The type of the resource that will be transcoded from the decoded resource.
*/
public <T, Z, R> LoadStatus load(Key signature, int width, int height, DataFetcher<T> fetcher,
DataLoadProvider<T, Z> loadProvider, Transformation<Z> transformation, ResourceTranscoder<Z, R> transcoder,
Priority priority, boolean isMemoryCacheable, DiskCacheStrategy diskCacheStrategy, ResourceCallback cb) {
Util.assertMainThread();
long startTime = LogTime.getLogTime();
final String id = fetcher.getId();
EngineKey key = keyFactory.buildKey(id, signature, width, height, loadProvider.getCacheDecoder(),
loadProvider.getSourceDecoder(), transformation, loadProvider.getEncoder(),
transcoder, loadProvider.getSourceEncoder());
EngineResource<?> cached = loadFromCache(key, isMemoryCacheable);
if (cached != null) {
cb.onResourceReady(cached);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logWithTimeAndKey("Loaded resource from cache", startTime, key);
}
return null;
}
EngineResource<?> active = loadFromActiveResources(key, isMemoryCacheable);
if (active != null) {
cb.onResourceReady(active);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logWithTimeAndKey("Loaded resource from active resources", startTime, key);
}
return null;
}
EngineJob current = jobs.get(key);
if (current != null) {
current.addCallback(cb);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logWithTimeAndKey("Added to existing load", startTime, key);
}
return new LoadStatus(cb, current);
}
EngineJob engineJob = engineJobFactory.build(key, isMemoryCacheable);
DecodeJob<T, Z, R> decodeJob = new DecodeJob<T, Z, R>(key, width, height, fetcher, loadProvider, transformation,
transcoder, diskCacheProvider, diskCacheStrategy, priority);
EngineRunnable runnable = new EngineRunnable(engineJob, decodeJob, priority);
jobs.put(key, engineJob);
engineJob.addCallback(cb);
engineJob.start(runnable);
if (Log.isLoggable(TAG, Log.VERBOSE)) {
logWithTimeAndKey("Started new load", startTime, key);
}
return new LoadStatus(cb, engineJob);
}
/**
* {@inheritDoc}
*/
@Override
public ResourceTranscoder<Z, R> getTranscoder() {
return transcoder;
}
public GeneralizingTranscoder(@NonNull ResourceTranscoder<ResourceType, Specific> transcoder) {
this.transcoder = transcoder;
}
/**
* Sets a transcoder to transcode the decoded and transformed {@link Bitmap} into another resource type.
*
* @param transcoder The transoder to use.
* @param transcodeClass The {@link Class} of the resource the {@link Bitmap} will be transcoded to.
* @param <R> The type of the resource the {@link Bitmap} will be transcoded to.
* @return This request builder.
*/
public <R> BitmapRequestBuilder<ModelType, R> transcode(ResourceTranscoder<Bitmap, R> transcoder,
Class<R> transcodeClass) {
return optionsApplier.apply(new BitmapRequestBuilder<ModelType, R>(
buildProvider(glide, streamModelLoader, fileDescriptorModelLoader, transcodeClass, transcoder),
transcodeClass, this));
}
/**
* Sets a transcoder to transcode the decoded {@link GifDrawable} into another
* resource type.
*
* @param transcoder The transcoder to use.
* @param transcodeClass The {@link Class} of the resource the
* {@link GifDrawable} will be transcoded to.
*
* @param <R> The type of the resource the {@link GifDrawable} will be
* trasncoded to.
* @return This request builder.
*/
public <R> GenericRequestBuilder<ModelType, InputStream, GifDrawable, R> transcode(
ResourceTranscoder<GifDrawable, R> transcoder, Class<R> transcodeClass) {
FixedLoadProvider<ModelType, InputStream, GifDrawable, R> provider = buildProvider(glide, streamModelLoader,
transcodeClass, transcoder);
return optionsApplier.apply(new GenericRequestBuilder<ModelType, InputStream, GifDrawable, R>(provider,
transcodeClass, this));
}
/**
* Returns the {@link ResourceTranscoder} to convert from the decoded
* and transformed resource into the transcoded resource.
*/
ResourceTranscoder<Z, R> getTranscoder();
/**
* Sets the {@link ResourceTranscoder} to use to transcode the decoded
* resource.
*
* @param transcoder The transcoder to use.
*/
public void setTranscoder(ResourceTranscoder<Z, R> transcoder) {
this.transcoder = transcoder;
}