下面列出了怎么用com.bumptech.glide.load.Options的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("EmptyCatchBlock")
@Override
public boolean encode(@NonNull Resource<Bitmap> data, @NonNull File file, @NonNull Options options) {
Log.i(TAG, "Encrypted resource encoder running: " + file.toString());
Bitmap bitmap = data.get();
Bitmap.CompressFormat format = getFormat(bitmap, options);
int quality = options.get(BitmapEncoder.COMPRESSION_QUALITY);
try (OutputStream os = createEncryptedOutputStream(secret, file)) {
bitmap.compress(format, quality, os);
os.close();
return true;
} catch (IOException e) {
Log.w(TAG, e);
return false;
}
}
@SuppressWarnings("EmptyCatchBlock")
@Override
public boolean encode(@NonNull InputStream data, @NonNull File file, @NonNull Options options) {
Log.i(TAG, "Encrypted cache encoder running: " + file.toString());
byte[] buffer = byteArrayPool.get(ArrayPool.STANDARD_BUFFER_SIZE_BYTES, byte[].class);
try (OutputStream outputStream = createEncryptedOutputStream(secret, file)) {
int read;
while ((read = data.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
return true;
} catch (IOException e) {
if (e instanceof SocketException) {
Log.d(TAG, "Socket exception. Likely a cancellation.");
} else {
Log.w(TAG, e);
}
return false;
} finally {
byteArrayPool.put(buffer);
}
}
@NonNull
private Bitmap.Config getDecodeFormat(@Nullable Options options) {
DecodeFormat decodeFormat = options == null ? null : options.get(GifOptions.DECODE_FORMAT);
if (decodeFormat == null) {
return Bitmap.Config.ARGB_8888;
}
switch (decodeFormat) {
case PREFER_RGB_565:
return Bitmap.Config.RGB_565;
case PREFER_ARGB_8888:
default:
return Bitmap.Config.ARGB_8888;
}
}
private void prepareSvg(@NonNull Resource<SVG> toTranscode, @Nullable Options options) {
if (!(toTranscode instanceof SvgResource)) {
return;
}
DownsampleStrategy strategy =
options == null ? null : options.get(DownsampleStrategy.OPTION);
if (strategy != null) {
float scaleFactor = strategy.getScaleFactor(
Math.round(toTranscode.get().getDocumentWidth()),
Math.round(toTranscode.get().getDocumentHeight()),
((SvgResource) toTranscode).getWidth(),
((SvgResource) toTranscode).getHeight()
);
SvgUtils.scaleDocumentSize(toTranscode.get(), scaleFactor);
}
}
private Bitmap.CompressFormat getFormat(Bitmap bitmap, Options options) {
Bitmap.CompressFormat format = options.get(BitmapEncoder.COMPRESSION_FORMAT);
if (format != null) {
return format;
} else if (bitmap.hasAlpha()) {
return Bitmap.CompressFormat.PNG;
} else {
return Bitmap.CompressFormat.JPEG;
}
}
@Override
public boolean handles(@NonNull File source, @NonNull Options options)
throws IOException
{
Log.i(TAG, "Checking item for encrypted Bitmap cache decoder: " + source.toString());
try (InputStream inputStream = createEncryptedInputStream(secret, source)) {
return streamBitmapDecoder.handles(inputStream, options);
} catch (IOException e) {
Log.w(TAG, e);
return false;
}
}
@Nullable
@Override
public Resource<Bitmap> decode(@NonNull File source, int width, int height, @NonNull Options options)
throws IOException
{
Log.i(TAG, "Encrypted Bitmap cache decoder running: " + source.toString());
try (InputStream inputStream = createEncryptedInputStream(secret, source)) {
return streamBitmapDecoder.decode(inputStream, width, height, options);
}
}
@Override
public boolean handles(@NonNull File source, @NonNull Options options) {
Log.i(TAG, "Checking item for encrypted GIF cache decoder: " + source.toString());
try (InputStream inputStream = createEncryptedInputStream(secret, source)) {
return gifDecoder.handles(inputStream, options);
} catch (IOException e) {
Log.w(TAG, e);
return false;
}
}
@Nullable
@Override
public Resource<GifDrawable> decode(@NonNull File source, int width, int height, @NonNull Options options) throws IOException {
Log.i(TAG, "Encrypted GIF cache decoder running...");
try (InputStream inputStream = createEncryptedInputStream(secret, source)) {
return gifDecoder.decode(inputStream, width, height, options);
}
}
@Override
public boolean encode(@NonNull Resource<GifDrawable> data, @NonNull File file, @NonNull Options options) {
GifDrawable drawable = data.get();
try (OutputStream outputStream = createEncryptedOutputStream(secret, file)) {
ByteBufferUtil.toStream(drawable.getBuffer(), outputStream);
return true;
} catch (IOException e) {
Log.w(TAG, e);
return false;
}
}
@Override
public LoadData<BlurHash> buildLoadData(@NonNull BlurHash blurHash,
int width,
int height,
@NonNull Options options)
{
return new LoadData<>(new ObjectKey(blurHash.getHash()), new BlurDataFetcher(blurHash));
}
@Override
public @Nullable Resource<Bitmap> decode(@NonNull BlurHash source, int width, int height, @NonNull Options options) throws IOException {
final int finalWidth;
final int finalHeight;
if (width > height) {
finalWidth = Math.min(width, MAX_DIMEN);
finalHeight = (int) (finalWidth * height / (float) width);
} else {
finalHeight = Math.min(height, MAX_DIMEN);
finalWidth = (int) (finalHeight * width / (float) height);
}
return new SimpleResource<>(BlurHashDecoder.decode(source.getHash(), finalWidth, finalHeight));
}
@Nullable
@Override
public Resource<Drawable> decode(@NonNull final ByteBuffer source, int width, int height, @NonNull Options options) throws IOException {
Loader loader = new ByteBufferLoader() {
@Override
public ByteBuffer getByteBuffer() {
source.position(0);
return source;
}
};
FrameAnimationDrawable drawable;
if (WebPParser.isAWebP(new ByteBufferReader(source))) {
drawable = new WebPDrawable(loader);
} else if (APNGParser.isAPNG(new ByteBufferReader(source))) {
drawable = new APNGDrawable(loader);
} else if (GifParser.isGif(new ByteBufferReader(source))) {
drawable = new GifDrawable(loader);
} else {
return null;
}
return new DrawableResource<Drawable>(drawable) {
@NonNull
@Override
public Class<Drawable> getResourceClass() {
return Drawable.class;
}
@Override
public int getSize() {
return source.limit();
}
@Override
public void recycle() {
((FrameAnimationDrawable) drawable).stop();
}
};
}
@Nullable
@Override
public Resource<Drawable> decode(@NonNull final InputStream source, int width, int height, @NonNull Options options) throws IOException {
byte[] data = inputStreamToBytes(source);
if (data == null) {
return null;
}
ByteBuffer byteBuffer = ByteBuffer.wrap(data);
return byteBufferDecoder.decode(byteBuffer, width, height, options);
}
public Resource<SVG> decode(@NonNull InputStream source, int width, int height,
@NonNull Options options)
throws IOException {
try {
SVG svg = SVG.getFromInputStream(source);
return new SimpleResource<>(svg);
} catch (SVGParseException ex) {
throw new IOException("Cannot load SVG from stream", ex);
}
}
@Nullable
@Override
public Resource<PictureDrawable> transcode(@NonNull Resource<SVG> toTranscode,
@NonNull Options options) {
SVG svg = toTranscode.get();
Picture picture = svg.renderToPicture();
PictureDrawable drawable = new PictureDrawable(picture);
return new SimpleResource<>(drawable);
}
@Nullable
@Override
public Resource<BitmapPaletteWrapper> transcode(@NonNull Resource<Bitmap> bitmapResource, @NonNull Options options) {
Bitmap bitmap = bitmapResource.get();
BitmapPaletteWrapper bitmapPaletteWrapper = new BitmapPaletteWrapper(bitmap,PhonographColorUtil.generatePalette(bitmap));
return new BitmapPaletteResource(bitmapPaletteWrapper);
}
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;
}
@Nullable
@Override
public LoadData<InputStream> buildLoadData(@NonNull ArtistImage artistImage, int width, int height, @NonNull Options options) {
return new LoadData<>( ArtistSignatureUtil.getInstance(App.getInstance()).getArtistSignature(artistImage.mArtistName, artistImage.mLoadOriginal,artistImage.mImageNumber),new ArtistImageFetcher(lastFMClient,artistImage,urlLoader,width,height,options));
// return new LoadData<>(new ObjectKey(String.valueOf(artistImage.getArtistName())),new ArtistImageFetcher(lastFMClient,artistImage,urlLoader,width,height,options));
// return new LoadData<>( ArtistSignatureUtil.getInstance(App.getInstance()).getArtistSignature(artistImage.getArtistName()), new ArtistImageFetcher(lastFMClient,artistImage,urlLoader,width,height, options));
}
@Override
SVG loadSvg(@NonNull ByteBuffer source, int width, int height, @NonNull Options options) throws SvgParseException {
try (InputStream is = ByteBufferUtil.toStream(source)) {
return SVG.getFromInputStream(is);
} catch (IOException | SVGParseException e) {
throw new SvgParseException(e);
}
}
@Nullable
@Override
public Resource<SVG> decode(@NonNull T source, int width, int height, @NonNull Options options)
throws IOException {
try {
int sourceSize = getSize(source);
SVG svg = loadSvg(source, width, height, options);
SvgUtils.fix(svg);
int[] sizes = getResourceSize(svg, width, height);
return new SvgResource(svg, sizes[0], sizes[1], sourceSize);
} catch (SvgParseException e) {
throw new IOException("Cannot load SVG", e);
}
}
@Override
SVG loadSvg(@NonNull InputStream source, int width, int height, @NonNull Options options) throws SvgParseException {
try {
return SVG.getFromInputStream(source);
} catch (SVGParseException e) {
throw new SvgParseException(e);
}
}
@Override
SVG loadSvg(@NonNull File source, int width, int height, @NonNull Options options) throws SvgParseException {
try {
return SvgUtils.getSvg(source);
} catch (IOException | SVGParseException e) {
throw new SvgParseException(e);
}
}
@Override
SVG loadSvg(@NonNull Uri source, int width, int height, @NonNull Options options) throws SvgParseException {
try {
return SVG.getFromResource(mResources, ResourceUtils.getRawResourceId(mResources, source));
} catch (SVGParseException e) {
throw new SvgParseException(e);
}
}
@Override
SVG loadSvg(@NonNull String source, int width, int height, @NonNull Options options) throws SvgParseException {
try {
return SVG.getFromString(source);
} catch (SVGParseException e) {
throw new SvgParseException(e);
}
}
@Override
SVG loadSvg(
@NonNull ParcelFileDescriptor source,
int width,
int height,
@NonNull Options options
) throws SvgParseException {
return mDecoder.loadSvg(source.getFileDescriptor(), width, height, options);
}
@Override
SVG loadSvg(
@NonNull FileDescriptor source,
int width,
int height,
@NonNull Options options
) throws SvgParseException {
try {
return SvgUtils.getSvg(source);
} catch (IOException | SVGParseException e) {
throw new SvgParseException(e);
}
}
@Override
public Resource<BitmapDrawable> transcode(
@NonNull Resource<SVG> toTranscode, @Nullable Options options) {
prepareSvg(toTranscode, options);
Bitmap bitmap = SvgUtils.toBitmap(toTranscode.get(), mBitmapProvider, getDecodeFormat(options));
return LazyBitmapDrawableResource.obtain(mResources, new BitmapResource(bitmap, mBitmapPool));
}
@Nullable
@Override
public LoadData<InputStream> buildLoadData(@NonNull StorageReference reference,
int height,
int width,
@NonNull Options options) {
return new LoadData<>(
new FirebaseStorageKey(reference),
new FirebaseStorageFetcher(reference));
}
@Override
public @NonNull LoadData<InputStream> buildLoadData(@NonNull StickerRemoteUri sticker, int width, int height, @NonNull Options options) {
return new LoadData<>(sticker, new StickerRemoteUriFetcher(receiver, sticker));
}