类com.bumptech.glide.load.engine.Resource源码实例Demo

下面列出了怎么用com.bumptech.glide.load.engine.Resource的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: AccountBook   文件: MaskTransformation.java
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
  if (result == null) {
    result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  }

  Drawable mask = Utils.getMaskDrawable(mContext, mMaskId);

  Canvas canvas = new Canvas(result);
  mask.setBounds(0, 0, width, height);
  mask.draw(canvas);
  canvas.drawBitmap(source, 0, 0, sMaskingPaint);

  return BitmapResource.obtain(result, mBitmapPool);
}
 
源代码2 项目: giffun   文件: GenericRequest.java
/**
 * Internal {@link #onResourceReady(Resource)} where arguments are known to be safe.
 *
 * @param resource original {@link Resource}, never <code>null</code>
 * @param result object returned by {@link Resource#get()}, checked for type and never <code>null</code>
 */
private void onResourceReady(Resource<?> resource, R result) {
    // We must call isFirstReadyResource before setting status.
    boolean isFirstResource = isFirstReadyResource();
    status = Status.COMPLETE;
    this.resource = resource;

    if (requestListener == null || !requestListener.onResourceReady(result, model, target, loadedFromMemoryCache,
            isFirstResource)) {
        GlideAnimation<R> animation = animationFactory.build(loadedFromMemoryCache, isFirstResource);
        target.onResourceReady(result, animation);
    }

    notifyLoadSuccess();

    if (Log.isLoggable(TAG, Log.VERBOSE)) {
        logV("Resource ready in " + LogTime.getElapsedMillis(startTime) + " size: "
                + (resource.getSize() * TO_MEGABYTE) + " fromCache: " + loadedFromMemoryCache);
    }
}
 
源代码3 项目: giffun   文件: GifDrawableTransformation.java
@Override
public Resource<GifDrawable> transform(Resource<GifDrawable> resource, int outWidth, int outHeight) {
    GifDrawable drawable = resource.get();

    // The drawable needs to be initialized with the correct width and height in order for a view displaying it
    // to end up with the right dimensions. Since our transformations may arbitrarily modify the dimensions of
    // our gif, here we create a stand in for a frame and pass it to the transformation to see what the final
    // transformed dimensions will be so that our drawable can report the correct intrinsic width and height.
    Bitmap firstFrame = resource.get().getFirstFrame();
    Resource<Bitmap> bitmapResource = new BitmapResource(firstFrame, bitmapPool);
    Resource<Bitmap> transformed = wrapped.transform(bitmapResource, outWidth, outHeight);
    Bitmap transformedFrame = transformed.get();
    if (!transformedFrame.equals(firstFrame)) {
        return new GifDrawableResource(new GifDrawable(drawable, transformedFrame, wrapped));
    } else {
        return resource;
    }
}
 
源代码4 项目: AccountBook   文件: GrayscaleTransformation.java
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(width, height, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, config);
  }

  Canvas canvas = new Canvas(bitmap);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(source, 0, 0, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
源代码5 项目: glide-support   文件: PaletteBitmapDecoder.java
@Override public Resource<PaletteBitmap> decode(InputStream source, int width, int height) throws IOException {
	if (!source.markSupported()) {
		source = new BufferedInputStream(source);
	}
	source.mark(1024); // 1k allowance for palette to figure out if it works or not
	// in practice it's most likely just 2 shorts (see ObjectInputStream.readStreamHeader)
	Palette palette = null;
	try {
		palette = paletteDecoder.decode(source, width, height).get();
	} catch (Exception ex) {
		// go back to the beginning as the palette was invalid, let's hope it's a bitmap
		source.reset();
	}
	Bitmap bitmap = bitmapDecoder.decode(source, width, height).get();
	if (palette == null) {
		Log.i("PALETTE", "Palette was not included in cache, calculate from Bitmap");
		palette = new Palette.Builder(bitmap).generate();
	}
	return new PaletteBitmapResource(new PaletteBitmap(bitmap, palette), bitmapPool);
}
 
@NonNull
@Override
public final Resource<Bitmap> transform(@NonNull Context context, @NonNull Resource<Bitmap> resource,
                                        int outWidth, int outHeight) {
    if (!Util.isValidDimensions(outWidth, outHeight)) {
        throw new IllegalArgumentException(
                "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight
                        + " less than or equal to zero and not Target.SIZE_ORIGINAL");
    }
    BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
    Bitmap toTransform = resource.get();
    System.out.println("target " + Target.SIZE_ORIGINAL + " outWidth " + outWidth  + " transform " +toTransform.getWidth()  );
    int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth;
    int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight;
    Bitmap transformed = transform(context.getApplicationContext(), bitmapPool, toTransform, targetWidth, targetHeight);

    final Resource<Bitmap> result;
    if (toTransform.equals(transformed)) {
        result = resource;
    } else {
        result = BitmapResource.obtain(transformed, bitmapPool);
    }
    return result;
}
 
源代码7 项目: giffun   文件: FileToStreamDecoder.java
@Override
public Resource<T> decode(File source, int width, int height) throws IOException {
    InputStream is = null;
    Resource<T> result = null;
    try {
        is = fileOpener.open(source);
        result = streamDecoder.decode(is, width, height);
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
                // Do nothing.
            }
        }
    }
    return result;
}
 
源代码8 项目: giffun   文件: GifBitmapWrapperResourceDecoder.java
private GifBitmapWrapper decodeGifWrapper(InputStream bis, int width, int height) throws IOException {
    GifBitmapWrapper result = null;
    Resource<GifDrawable> gifResource = gifDecoder.decode(bis, width, height);
    if (gifResource != null) {
        GifDrawable drawable = gifResource.get();
        // We can more efficiently hold Bitmaps in memory, so for static GIFs, try to return Bitmaps
        // instead. Returning a Bitmap incurs the cost of allocating the GifDrawable as well as the normal
        // Bitmap allocation, but since we can encode the Bitmap out as a JPEG, future decodes will be
        // efficient.
        if (drawable.getFrameCount() > 1) {
            result = new GifBitmapWrapper(null /*bitmapResource*/, gifResource);
        } else {
            Resource<Bitmap> bitmapResource = new BitmapResource(drawable.getFirstFrame(), bitmapPool);
            result = new GifBitmapWrapper(bitmapResource, null /*gifResource*/);
        }
    }
    return result;
}
 
源代码9 项目: Silence   文件: BitmapUtil.java
private static <T> Bitmap createScaledBitmapInto(Context context, T model, int width, int height)
    throws BitmapDecodingException
{
  final Bitmap rough = Downsampler.AT_LEAST.decode(getInputStreamForModel(context, model),
                                                   Glide.get(context).getBitmapPool(),
                                                   width, height,
                                                   DecodeFormat.PREFER_RGB_565);

  final Resource<Bitmap> resource = BitmapResource.obtain(rough, Glide.get(context).getBitmapPool());
  final Resource<Bitmap> result   = new FitCenter(context).transform(resource, width, height);

  if (result == null) {
    throw new BitmapDecodingException("unable to transform Bitmap");
  }
  return result.get();
}
 
源代码10 项目: giffun   文件: GrayscaleTransformation.java
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap.Config config =
      source.getConfig() != null ? source.getConfig() : Bitmap.Config.ARGB_8888;
  Bitmap bitmap = mBitmapPool.get(width, height, config);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, config);
  }

  Canvas canvas = new Canvas(bitmap);
  ColorMatrix saturation = new ColorMatrix();
  saturation.setSaturation(0f);
  Paint paint = new Paint();
  paint.setColorFilter(new ColorMatrixColorFilter(saturation));
  canvas.drawBitmap(source, 0, 0, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
源代码11 项目: giffun   文件: RoundedCornersTransformation.java
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();

  int width = source.getWidth();
  int height = source.getHeight();

  Bitmap bitmap = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
  if (bitmap == null) {
    bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
  }

  Canvas canvas = new Canvas(bitmap);
  Paint paint = new Paint();
  paint.setAntiAlias(true);
  paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
  drawRoundRect(canvas, paint, width, height);
  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
源代码12 项目: AcgClub   文件: BitmapTransformation.java
@Override
public final Resource<Bitmap> transform(Context context, Resource<Bitmap> resource, int outWidth,
    int outHeight) {
  if (!Util.isValidDimensions(outWidth, outHeight)) {
    throw new IllegalArgumentException(
        "Cannot apply transformation on width: " + outWidth + " or height: " + outHeight
            + " less than or equal to zero and not Target.SIZE_ORIGINAL");
  }
  BitmapPool bitmapPool = Glide.get(context).getBitmapPool();
  Bitmap toTransform = resource.get();
  int targetWidth = outWidth == Target.SIZE_ORIGINAL ? toTransform.getWidth() : outWidth;
  int targetHeight = outHeight == Target.SIZE_ORIGINAL ? toTransform.getHeight() : outHeight;
  Bitmap transformed = transform(context.getApplicationContext(), bitmapPool, toTransform,
      targetWidth, targetHeight);

  final Resource<Bitmap> result;
  if (toTransform.equals(transformed)) {
    result = resource;
  } else {
    result = BitmapResource.obtain(transformed, bitmapPool);
  }
  return result;
}
 
源代码13 项目: glide-support   文件: PaletteCacheDecoder.java
@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);
	}
}
 
源代码14 项目: Android   文件: BlurMaskTransformation.java
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();
    int width = source.getWidth();
    int height = source.getHeight();

    if (radius != 0) {
        source = blur(source);
    }
    Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }

    Drawable mask = getMaskDrawable(mContext, mMaskId);

    Canvas canvas = new Canvas(result);
    mask.setBounds(0, 0, width, height);
    mask.draw(canvas);
    canvas.drawBitmap(source, 0, 0, sMaskingPaint);
    return BitmapResource.obtain(result, mBitmapPool);
}
 
源代码15 项目: Android   文件: MaskTransformation.java
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();
    int width = source.getWidth();
    int height = source.getHeight();

    Bitmap result = mBitmapPool.get(width, height, Bitmap.Config.ARGB_8888);
    if (result == null) {
        result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    }

    Drawable mask = getMaskDrawable(mContext, mMaskId);

    Canvas canvas = new Canvas(result);
    mask.setBounds(0, 0, width, height);
    mask.draw(canvas);
    canvas.drawBitmap(source, 0, 0, sMaskingPaint);
    return BitmapResource.obtain(result, mBitmapPool);
}
 
源代码16 项目: glide-support   文件: TestFragment.java
private BitmapDrawable transformDrawable(Drawable drawable, Transformation<Bitmap> transform, int size) {
	// render original
	Bitmap bitmap = Bitmap.createBitmap(size, size, Config.ARGB_8888);
	Canvas canvas = new Canvas(bitmap);
	drawable.setBounds(0, 0, size, size);
	drawable.draw(canvas);
	// make rounded
	Resource<Bitmap> original = BitmapResource.obtain(bitmap, Glide.get(getContext()).getBitmapPool());
	Resource<Bitmap> rounded = transform.transform(original, size, size);
	if (!original.equals(rounded)) {
		original.recycle();
	}
	return new BitmapDrawable(getResources(), rounded.get());
}
 
源代码17 项目: glide-support   文件: PaletteBitmapEncoder.java
@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;
}
 
源代码18 项目: mollyim-android   文件: EncryptedGifCacheDecoder.java
@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;
  }
}
 
源代码20 项目: mollyim-android   文件: BlurHashResourceDecoder.java
@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));
}
 
源代码21 项目: Dota2Helper   文件: BlurTransformation.java
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
    Bitmap source = resource.get();

    int width = source.getWidth();
    int height = source.getHeight();
    int scaledWidth = width / mSampling;
    int scaledHeight = height / mSampling;

    Bitmap bitmap = mBitmapPool.get(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(scaledWidth, scaledHeight, Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    canvas.scale(1 / (float) mSampling, 1 / (float) mSampling);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(source, 0, 0, paint);

    RenderScript rs = RenderScript.create(mContext);
    Allocation input = Allocation.createFromBitmap(rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE,
            Allocation.USAGE_SCRIPT);
    Allocation output = Allocation.createTyped(rs, input.getType());
    ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));

    blur.setInput(input);
    blur.setRadius(mRadius);
    blur.forEach(output);
    output.copyTo(bitmap);

    rs.destroy();

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
@Override
public Resource<byte[]> decode(InputStream in, int width, int height) throws
        IOException {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int count;
    while ((count = in.read(buffer)) != -1) {
        bytes.write(buffer, 0, count);
    }
    return new BytesResource(bytes.toByteArray());
}
 
@SuppressWarnings("unchecked")
@Override
public Resource<GlideDrawable> transcode(Resource<GifBitmapWrapper> toTranscode) {
    GifBitmapWrapper gifBitmap = toTranscode.get();
    Resource<Bitmap> bitmapResource = gifBitmap.getBitmapResource();

    final Resource<? extends GlideDrawable> result;
    if (bitmapResource != null) {
        result = bitmapDrawableResourceTranscoder.transcode(bitmapResource);
    } else {
        result = gifBitmap.getGifResource();
    }
    // This is unchecked but always safe, anything that extends a Drawable can be safely cast to a Drawable.
    return (Resource<GlideDrawable>) result;
}
 
@Override public Resource<Bitmap> decode(Drawable drawable, int width, int height) throws IOException {
	Config config = PixelFormat.formatHasAlpha(drawable.getOpacity())? Config.ARGB_8888 : Config.RGB_565;
	Bitmap bitmap = pool.get(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config);
	if (bitmap == null) {
		bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), config);
	}
	Canvas canvas = new Canvas(bitmap);
	drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
	drawable.draw(canvas);
	return new BitmapResource(bitmap, pool);
}
 
源代码25 项目: glide-support   文件: ApplicationIconDecoder.java
@Override public Resource<Drawable> decode(ApplicationInfo source, int width, int height) throws IOException {
	Drawable icon = context.getPackageManager().getApplicationIcon(source);
	return new DrawableResource<Drawable>(icon) {
		@Override public int getSize() { // best effort
			if (drawable instanceof BitmapDrawable) {
				return Util.getBitmapByteSize(((BitmapDrawable)drawable).getBitmap());
			} else {
				return 1;
			}
		}
		@Override public void recycle() { /* not from our pool */ }
	};
}
 
源代码26 项目: giffun   文件: GifBitmapWrapperResourceDecoder.java
@SuppressWarnings("resource")
// @see ResourceDecoder.decode
@Override
public Resource<GifBitmapWrapper> decode(ImageVideoWrapper source, int width, int height) throws IOException {
    ByteArrayPool pool = ByteArrayPool.get();
    byte[] tempBytes = pool.getBytes();

    GifBitmapWrapper wrapper = null;
    try {
        wrapper = decode(source, width, height, tempBytes);
    } finally {
        pool.releaseBytes(tempBytes);
    }
    return wrapper != null ? new GifBitmapWrapperResource(wrapper) : null;
}
 
源代码27 项目: giffun   文件: GifBitmapWrapperResourceDecoder.java
private GifBitmapWrapper decodeBitmapWrapper(ImageVideoWrapper toDecode, int width, int height) throws IOException {
    GifBitmapWrapper result = null;

    Resource<Bitmap> bitmapResource = bitmapDecoder.decode(toDecode, width, height);
    if (bitmapResource != null) {
        result = new GifBitmapWrapper(bitmapResource, null);
    }

    return result;
}
 
源代码28 项目: glide-support   文件: PaletteDecoder.java
@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 Resource<PictureDrawable> transcode(Resource<SVG> toTranscode) {
    SVG svg = toTranscode.get();
    Picture picture = svg.renderToPicture();
    PictureDrawable drawable = new PictureDrawable(picture);
    return new SimpleResource<PictureDrawable>(drawable);
}
 
源代码30 项目: giffun   文件: GifBitmapWrapperResource.java
@Override
public void recycle() {
    Resource<Bitmap> bitmapResource = data.getBitmapResource();
    if (bitmapResource != null) {
        bitmapResource.recycle();
    }
    Resource<GifDrawable> gifDataResource = data.getGifResource();
    if (gifDataResource != null) {
        gifDataResource.recycle();
    }
}
 
 类所在包
 类方法
 同包方法