Java Code Examples for com.bumptech.glide.load.engine.Resource#get()

The following examples show how to use com.bumptech.glide.load.engine.Resource#get() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RoundedCornersTransformation.java    From giffun with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example 2
Source File: GifDrawableTransformation.java    From giffun with Apache License 2.0 6 votes vote down vote up
@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;
    }
}
 
Example 3
Source File: BitmapUtil.java    From Silence with GNU General Public License v3.0 6 votes vote down vote up
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();
}
 
Example 4
Source File: MaskTransformation.java    From AccountBook with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example 5
Source File: BlurTransform.java    From ImageLoader with Apache License 2.0 5 votes vote down vote up
@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 samplesize = MyUtil.calculateScaleRatio(width,height,outWidth,outHeight,subsamplingRatio);



    int scaledWidth = width / samplesize;
    int scaledHeight = height / samplesize;

    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) samplesize, 1 / (float) samplesize);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(source, 0, 0, paint);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
        try {
            bitmap = RSBlur.blur(mContext, bitmap, mRadius);
        } catch (RSRuntimeException e) {
            bitmap = FastBlur.blur(bitmap, mRadius, true);
        }
    } else {
        bitmap = FastBlur.blur(bitmap, mRadius, true);
    }

    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 6
Source File: SvgDrawableTranscoder.java    From PowerFileExplorer with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example 7
Source File: CropCircleTransformation.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  int size = Math.min(source.getWidth(), source.getHeight());

  int width = (source.getWidth() - size) / 2;
  int height = (source.getHeight() - size) / 2;

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

  Canvas canvas = new Canvas(bitmap);
  Paint paint = new Paint();
  BitmapShader shader =
      new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
  if (width != 0 || height != 0) {
    // source isn't square, move viewport to center
    Matrix matrix = new Matrix();
    matrix.setTranslate(-width, -height);
    shader.setLocalMatrix(matrix);
  }
  paint.setShader(shader);
  paint.setAntiAlias(true);

  float r = size / 2f;
  canvas.drawCircle(r, r, r, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 8
Source File: CropCircleTransformation.java    From AccountBook with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  int size = Math.min(source.getWidth(), source.getHeight());

  int width = (source.getWidth() - size) / 2;
  int height = (source.getHeight() - size) / 2;

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

  Canvas canvas = new Canvas(bitmap);
  Paint paint = new Paint();
  BitmapShader shader =
      new BitmapShader(source, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
  if (width != 0 || height != 0) {
    // source isn't square, move viewport to center
    Matrix matrix = new Matrix();
    matrix.setTranslate(-width, -height);
    shader.setLocalMatrix(matrix);
  }
  paint.setShader(shader);
  paint.setAntiAlias(true);

  float r = size / 2f;
  canvas.drawCircle(r, r, r, paint);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 9
Source File: CropTransformation.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
  Bitmap source = resource.get();
  mWidth = mWidth == 0 ? source.getWidth() : mWidth;
  mHeight = mHeight == 0 ? source.getHeight() : mHeight;

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

  float scaleX = (float) mWidth / source.getWidth();
  float scaleY = (float) mHeight / source.getHeight();
  float scale = Math.max(scaleX, scaleY);

  float scaledWidth = scale * source.getWidth();
  float scaledHeight = scale * source.getHeight();
  float left = (mWidth - scaledWidth) / 2;
  float top = getTop(scaledHeight);
  RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

  Canvas canvas = new Canvas(bitmap);
  canvas.drawBitmap(source, null, targetRect, null);

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 10
Source File: BlurTransformation.java    From giffun with Apache License 2.0 5 votes vote down vote up
@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);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    try {
      bitmap = RSBlur.blur(mContext, bitmap, mRadius);
    } catch (RSRuntimeException e) {
      bitmap = FastBlur.blur(bitmap, mRadius, true);
    }
  } else {
    bitmap = FastBlur.blur(bitmap, mRadius, true);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 11
Source File: BlurTransformation.java    From FamilyChat with Apache License 2.0 5 votes vote down vote up
@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);

  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
    try {
      bitmap = RSBlur.blur(mContext, bitmap, mRadius);
    } catch (RSRuntimeException e) {
      bitmap = FastBlur.blur(bitmap, mRadius, true);
    }
  } else {
    bitmap = FastBlur.blur(bitmap, mRadius, true);
  }

  return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 12
Source File: GifDrawableBytesTranscoder.java    From MoeGallery with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Resource<GifDrawable> transcode(Resource<byte[]> toTranscode) {
    try {
        return new GifDrawableResource(new GifDrawable(toTranscode.get()));
    } catch (IOException ex) {
        Log.e("GifDrawable", "Cannot decode bytes", ex);
        return null;
    }
}
 
Example 13
Source File: BorderRoundTransformation2.java    From ImageLoader with Apache License 2.0 5 votes vote down vote up
@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);//新建一个空白的bitmap
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(new BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));//设置要绘制的图形

    Paint borderPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//设置边框样式
    borderPaint.setColor(mBorderColor);
    borderPaint.setStyle(Paint.Style.STROKE);
    borderPaint.setStrokeWidth(mBorderWidth);

    drawRoundRect(canvas, paint, width, height, borderPaint);*/

    bitmap = getRoundBitmapByShader(bitmap,bitmap.getWidth(),bitmap.getHeight(),mRadius,mBorderColor);


    return BitmapResource.obtain(bitmap, mBitmapPool);
}
 
Example 14
Source File: GifBitmapWrapperDrawableTranscoder.java    From giffun with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 15
Source File: TestFragment.java    From glide-support with The Unlicense 5 votes vote down vote up
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());
}
 
Example 16
Source File: PaletteBitmapEncoder.java    From glide-support with The Unlicense 5 votes vote down vote up
@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;
}
 
Example 17
Source File: BitmapSizeTranscoder.java    From glide-support with The Unlicense 5 votes vote down vote up
@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);
}
 
Example 18
Source File: GifDrawableBytesTranscoder.java    From giffun with Apache License 2.0 4 votes vote down vote up
@Override
public Resource<byte[]> transcode(Resource<GifDrawable> toTranscode) {
    GifDrawable gifData = toTranscode.get();
    return new BytesResource(gifData.getData());
}
 
Example 19
Source File: BitmapPaletteTranscoder.java    From Orin with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Resource<BitmapPaletteWrapper> transcode(Resource<Bitmap> bitmapResource) {
    Bitmap bitmap = bitmapResource.get();
    BitmapPaletteWrapper bitmapPaletteWrapper = new BitmapPaletteWrapper(bitmap, PhonographColorUtil.generatePalette(bitmap));
    return new BitmapPaletteResource(bitmapPaletteWrapper, bitmapPool);
}
 
Example 20
Source File: BitmapPaletteTranscoder.java    From RetroMusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Resource<BitmapPaletteWrapper> transcode(Resource<Bitmap> bitmapResource) {
    Bitmap bitmap = bitmapResource.get();
    BitmapPaletteWrapper bitmapPaletteWrapper = new BitmapPaletteWrapper(bitmap, RetroMusicColorUtil.generatePalette(bitmap));
    return new BitmapPaletteResource(bitmapPaletteWrapper, bitmapPool);
}