下面列出了怎么用com.bumptech.glide.load.resource.SimpleResource的API类实例代码及写法,或者点击链接到github查看源代码。
@Override public Resource<Palette> decode(InputStream source, int width, int height) throws IOException {
if (!source.markSupported()) {
source = new BufferedInputStream(source);
}
Log.d("PALETTE", "Decoding from cache");
if (isBitmap(source)) {
Log.d("PALETTE", "It's a cached bitmap");
Resource<Bitmap> bitmap = bitmapDecoder.decode(source, width, height);
try {
Palette palette = new Palette.Builder(bitmap.get())
.resizeBitmapArea(-1)
.generate();
Log.d("PALETTE", "Palette generated");
return new SimpleResource<>(palette);
} finally {
bitmap.recycle();
}
} else {
Log.d("PALETTE", "It's a cached palette");
if (PaletteCacheEncoder.PALETTE_MAGIC_BYTE != source.read()) {
throw new IOException("Cannot read palette magic.");
}
return paletteDecoder.decode(source, width, height);
}
}
@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));
}
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
try {
SVG svg = SVG.getFromInputStream(source);
return new SimpleResource<SVG>(svg);
} catch (SVGParseException ex) {
throw new IOException("Cannot load SVG from stream", ex);
}
}
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
SVG svg = toTranscode.get();
Picture picture = svg.renderToPicture();
PictureDrawable drawable = new PictureDrawable(picture);
return new SimpleResource<PictureDrawable>(drawable);
}
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);
}
@Override public Resource<Size> transcode(Resource<Bitmap> toTranscode) {
Bitmap bitmap = toTranscode.get();
Size size = new Size();
size.width = bitmap.getWidth();
size.height = bitmap.getHeight();
return new SimpleResource<>(size);
}
@Override public Resource<Size> transcode(Resource<Options> toTranscode) {
Options options = toTranscode.get();
Size size = new Size();
size.width = options.outWidth;
size.height = options.outHeight;
return new SimpleResource<>(size);
}
@Override public Resource<Palette> decode(InputStream source, int width, int height) throws IOException {
try {
ObjectInputStream stream = new ObjectInputStream(source);
PaletteSerializer palette = (PaletteSerializer)stream.readObject();
Log.d("PALETTE", "Palette loaded");
return new SimpleResource<>(palette.getPalette());
} catch (Exception e) {
Log.w("PALETTE", "Cannot deserialize", e);
throw new IOException("Cannot read palette", e);
}
}
@Override public boolean encode(Resource<PaletteBitmap> data, OutputStream os) {
PaletteBitmap bitmap = data.get();
// Resource objects are only created to satisfy the contract, they don't need to be recycled.
boolean paletteOK = paletteEncoder.encode(new SimpleResource<>(bitmap.palette), os);
boolean bitmapOK = bitmapEncoder.encode(new BitmapResource(bitmap.bitmap, FAKE_POOL), os);
return bitmapOK && paletteOK;
}
public Resource<SVG> decode(InputStream source, int width, int height) throws IOException {
try {
SVG svg = SVG.getFromInputStream(source);
return new SimpleResource<SVG>(svg);
} catch (SVGParseException ex) {
throw new IOException("Cannot load SVG from stream", ex);
}
}
@Override
public Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
SVG svg = toTranscode.get();
Picture picture = svg.renderToPicture();
PictureDrawable drawable = new PictureDrawable(picture);
return new SimpleResource<PictureDrawable>(drawable);
}
@Override public Resource<Options> decode(File source, int width, int height) throws IOException {
Options options = new Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(source.getAbsolutePath(), options);
return new SimpleResource<>(options);
}
@Override public Resource<T> decode(T source, int width, int height) throws IOException {
return new SimpleResource<>(source);
}