com.bumptech.glide.load.engine.Resource Java Examples

The following examples show how to use com.bumptech.glide.load.engine.Resource. 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: PaletteCacheDecoder.java    From glide-support with The Unlicense 6 votes vote down vote up
@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);
	}
}
 
Example #2
Source File: BitmapTransformation.java    From AcgClub with MIT License 6 votes vote down vote up
@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;
}
 
Example #3
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 #4
Source File: PaletteBitmapDecoder.java    From glide-support with The Unlicense 6 votes vote down vote up
@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);
}
 
Example #5
Source File: GrayscaleTransformation.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.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);
}
 
Example #6
Source File: BitmapTransformation.java    From nativescript-image-cache-it with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #7
Source File: FileToStreamDecoder.java    From giffun with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #8
Source File: GrayscaleTransformation.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.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);
}
 
Example #9
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 #10
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 #11
Source File: GenericRequest.java    From giffun with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example #12
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 #13
Source File: GifBitmapWrapperResourceDecoder.java    From giffun with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #14
Source File: BlurMaskTransformation.java    From Android with MIT License 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();

    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);
}
 
Example #15
Source File: MaskTransformation.java    From Android with MIT License 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 = 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 #16
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 #17
Source File: SvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
@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);
    }
}
 
Example #18
Source File: BlurTransformation.java    From TestChat 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_MR1) {
                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 #19
Source File: BlurTransformation.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 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 #20
Source File: RawStreamDecoder.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override public Resource<Bitmap> decode(InputStream source, int width, int height) throws IOException {
	Bitmap bitmap = BitmapFactory.decodeStream(source);
	// read source stream into a Bitmap object (whatever the format)
	// make sure it's not using too much memory
	// the best is obviously if the loaded image matches the given width/height so downstream transformation will just pass it on
	return BitmapResource.obtain(bitmap, bitmapPool);
}
 
Example #21
Source File: BitmapPaletteTranscoder.java    From MusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
@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);
}
 
Example #22
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 #23
Source File: DrawableResourceDecoder.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override public Resource<Drawable> decode(Drawable source, int width, int height) throws IOException {
	return new DrawableResource<Drawable>(source) {
		@Override public int getSize() {
			return 1;
		}
		@Override public void recycle() {
		}
	};
}
 
Example #24
Source File: OOMReadyStreamBitmapDecoder.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override public Resource<Bitmap> decode(InputStream source, int width, int height) {
	try {
		return super.decode(source, width, height);
	} catch (OutOfMemoryError err) {
		throw new RuntimeException(err);
	}
}
 
Example #25
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 #26
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 #27
Source File: PaletteEncoder.java    From glide-support with The Unlicense 5 votes vote down vote up
@Override public boolean encode(Resource<Palette> data, OutputStream os) {
	Log.d("PALETTE", "Encoding a palette to cache");
	PaletteSerializer palette = new PaletteSerializer(data.get());
	try {
		ObjectOutputStream stream = new ObjectOutputStream(os);
		stream.writeObject(palette);
		stream.flush();
		return true;
	} catch (IOException e) {
		return false;
	}
}
 
Example #28
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 #29
Source File: GifBitmapWrapperResourceEncoder.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public boolean encode(Resource<GifBitmapWrapper> resource, OutputStream os) {
    final GifBitmapWrapper gifBitmap = resource.get();
    final Resource<Bitmap> bitmapResource = gifBitmap.getBitmapResource();

    if (bitmapResource != null) {
        return bitmapEncoder.encode(bitmapResource, os);
    } else {
        return gifEncoder.encode(gifBitmap.getGifResource(), os);
    }
}
 
Example #30
Source File: GifBitmapWrapperResource.java    From giffun with Apache License 2.0 5 votes vote down vote up
@Override
public void recycle() {
    Resource<Bitmap> bitmapResource = data.getBitmapResource();
    if (bitmapResource != null) {
        bitmapResource.recycle();
    }
    Resource<GifDrawable> gifDataResource = data.getGifResource();
    if (gifDataResource != null) {
        gifDataResource.recycle();
    }
}