Java Code Examples for com.bumptech.glide.request.target.Target#SIZE_ORIGINAL

The following examples show how to use com.bumptech.glide.request.target.Target#SIZE_ORIGINAL . 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: BitmapTransformation.java    From giffun with Apache License 2.0 6 votes vote down vote up
@Override
public final Resource<Bitmap> transform(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");
    }
    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(bitmapPool, toTransform, targetWidth, targetHeight);

    final Resource<Bitmap> result;
    if (toTransform.equals(transformed)) {
        result = resource;
    } else {
        result = BitmapResource.obtain(transformed, bitmapPool);
    }

    return result;
}
 
Example 2
Source File: Downsampler.java    From giffun with Apache License 2.0 6 votes vote down vote up
private int getRoundedSampleSize(int degreesToRotate, int inWidth, int inHeight, int outWidth, int outHeight) {
    int targetHeight = outHeight == Target.SIZE_ORIGINAL ? inHeight : outHeight;
    int targetWidth = outWidth == Target.SIZE_ORIGINAL ? inWidth : outWidth;

    final int exactSampleSize;
    if (degreesToRotate == 90 || degreesToRotate == 270) {
        // If we're rotating the image +-90 degrees, we need to downsample accordingly so the image width is
        // decreased to near our target's height and the image height is decreased to near our target width.
        //noinspection SuspiciousNameCombination
        exactSampleSize = getSampleSize(inHeight, inWidth, targetWidth, targetHeight);
    } else {
        exactSampleSize = getSampleSize(inWidth, inHeight, targetWidth, targetHeight);
    }

    // BitmapFactory only accepts powers of 2, so it will round down to the nearest power of two that is less than
    // or equal to the sample size we provide. Because we need to estimate the final image width and height to
    // re-use Bitmaps, we mirror BitmapFactory's calculation here. For bug, see issue #224. For algorithm see
    // http://stackoverflow.com/a/17379704/800716.
    final int powerOfTwoSampleSize = exactSampleSize == 0 ? 0 : Integer.highestOneBit(exactSampleSize);

    // Although functionally equivalent to 0 for BitmapFactory, 1 is a safer default for our code than 0.
    return Math.max(1, powerOfTwoSampleSize);
}
 
Example 3
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 4
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 5
Source File: ImageHelper.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static void loadImage(Context context, ImageView view,
                             @NonNull String url, @Nullable String thumbUrl,
                             @Size(2) @Px int[] size, @Nullable @Size(2) @Px int[] thumbSize,
                             @Nullable BitmapTransformation[] ts, @Nullable OnLoadImageListener l) {
    if (thumbSize == null) {
        thumbSize = new int[] {Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL};
    }
    DrawableRequestBuilder<String> thumb = TextUtils.isEmpty(thumbUrl) ? null : Glide.with(getValidContext(context))
            .load(ensureUrl(thumbUrl))
            .override(thumbSize[0], thumbSize[1])
            .diskCacheStrategy(
                    thumbSize[0] == Target.SIZE_ORIGINAL
                            ? DiskCacheStrategy.NONE
                            : DiskCacheStrategy.SOURCE
            ).listener(
                    new BaseRequestListener<>(() -> view.setTag(R.id.tag_item_image_fade_in_flag, false))
            );
    loadImage(context, view, url, thumb, size, ts, l);
}
 
Example 6
Source File: BitmapTransformation.java    From glide-transformations 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();
  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: CommonUtil.java    From Yuan-SxMusic with Apache License 2.0 5 votes vote down vote up
public static Bitmap getImgBitmap(Context context,String imgUrl){
    SimpleTarget target = new SimpleTarget<Drawable>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
        @Override
        public void onResourceReady(@Nullable Drawable resource, Transition<? super Drawable> transition) {
            Bitmap bitmap = ((BitmapDrawable) resource).getBitmap();
        }
    };
    Glide.with(context)
            .load(imgUrl)
            .apply(RequestOptions.placeholderOf(R.drawable.welcome))
            .apply(RequestOptions.errorOf(R.drawable.welcome))
            .into(target);
    return null;
}
 
Example 8
Source File: SvgDecoder.java    From SvgGlidePlugins with Apache License 2.0 5 votes vote down vote up
private static int[] getResourceSize(@NonNull SVG svg, int width, int height) {
    int[] sizes = new int[]{width, height};
    if (width == Target.SIZE_ORIGINAL && height == Target.SIZE_ORIGINAL) {
        sizes[0] = Math.round(svg.getDocumentWidth());
        sizes[1] = Math.round(svg.getDocumentHeight());

    } else if (width == Target.SIZE_ORIGINAL) {
        sizes[0] = Math.round(svg.getDocumentAspectRatio() * height);

    } else if (height == Target.SIZE_ORIGINAL) {
        sizes[1] = Math.round(width / svg.getDocumentAspectRatio());
    }
    return sizes;
}
 
Example 9
Source File: AspectTransformer.java    From nativescript-image-cache-it with Apache License 2.0 5 votes vote down vote up
@Override
protected Bitmap transform(@NonNull Context context, @NonNull BitmapPool pool,
                           @NonNull Bitmap toTransform, int outWidth, int outHeight) {


    Bitmap bitmap = toTransform;
    int sourceWidth = toTransform.getWidth();
    int sourceHeight = toTransform.getHeight();
    int reqWidth = decodeWidth > 0 ? decodeWidth : Target.SIZE_ORIGINAL;
    int reqHeight = decodeHeight > 0 ? decodeHeight : Target.SIZE_ORIGINAL;

    // scale
    if ((reqWidth != Target.SIZE_ORIGINAL && reqHeight != Target.SIZE_ORIGINAL) && (reqWidth != sourceWidth || reqHeight != sourceHeight)) {
        if (keepAspectRatio) {
            double widthCoef = (double) sourceWidth / (double) reqWidth;
            double heightCoef = (double) sourceHeight / (double) reqHeight;
            double aspectCoef = Math.min(widthCoef, heightCoef);

            reqWidth = (int) Math.floor(sourceWidth / aspectCoef);
            reqHeight = (int) Math.floor(sourceHeight / aspectCoef);
        }

        bitmap = Bitmap.createScaledBitmap(bitmap, reqWidth, reqHeight, true);
    }

    return bitmap;
}
 
Example 10
Source File: ImageHelper.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Bitmap loadBitmap(Context context, Uri uri, @Nullable @Size(2) int[] size)
        throws ExecutionException, InterruptedException {
    if (size == null) {
        size = new int[] {Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL};
    }
    return Glide.with(getValidContext(context))
            .load(uri)
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(size[0], size[1])
            .get();
}
 
Example 11
Source File: ImageHelper.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static Bitmap loadBitmap(Context context, @DrawableRes int id, @Nullable @Size(2) int[] size)
        throws ExecutionException, InterruptedException {
    if (size == null) {
        size = new int[] {Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL};
    }
    return Glide.with(getValidContext(context))
            .load(id)
            .asBitmap()
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .into(size[0], size[1])
            .get();
}
 
Example 12
Source File: Util.java    From giffun with Apache License 2.0 4 votes vote down vote up
private static boolean isValidDimension(int dimen) {
    return dimen > 0 || dimen == Target.SIZE_ORIGINAL;
}
 
Example 13
Source File: GlideLoader.java    From ImageLoader with Apache License 2.0 4 votes vote down vote up
@Override
public void requestAsBitmap(final SingleConfig config) {
    SimpleTarget target = null;
    if(config.getWidth()>0 && config.getHeight()>0){
        target = new SimpleTarget<Bitmap>(config.getWidth(),config.getHeight()) {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                // do something with the bitmap
                // for demonstration purposes, let's just set it to an ImageView
                // BitmapPool mBitmapPool = Glide.get(BigLoader.context).getBitmapPool();
                //bitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight())
                    /*if(config.isNeedBlur()){
                        bitmap = blur(bitmap,config.getBlurRadius(),false);
                        Glide.get(GlobalConfig.context).getBitmapPool().put(bitmap);
                    }
                    if(config.getShapeMode() == ShapeMode.OVAL){
                        bitmap = MyUtil.cropCirle(bitmap,false);
                        Glide.get(GlobalConfig.context).getBitmapPool().put(bitmap);
                    }else if(config.getShapeMode() == ShapeMode.RECT_ROUND){
                        bitmap = MyUtil.rectRound(bitmap,config.getRectRoundRadius(),0);
                        Glide.get(GlobalConfig.context).getBitmapPool().put(bitmap);
                    }*/

                config.getBitmapListener().onSuccess(bitmap);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                if(e !=null){
                    e.printStackTrace();
                }
                config.getBitmapListener().onFail(e);
            }
        };
    }else {
        target = new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap bitmap, GlideAnimation glideAnimation) {
                // do something with the bitmap
                // for demonstration purposes, let's just set it to an ImageView
                // BitmapPool mBitmapPool = Glide.get(BigLoader.context).getBitmapPool();
                //bitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight())
                    /*if(config.isNeedBlur()){
                        bitmap = blur(bitmap,config.getBlurRadius(),false);
                        Glide.get(GlobalConfig.context).getBitmapPool().put(bitmap);
                    }
                    if(config.getShapeMode() == ShapeMode.OVAL){
                        bitmap = MyUtil.cropCirle(bitmap,false);
                        Glide.get(GlobalConfig.context).getBitmapPool().put(bitmap);
                    }else if(config.getShapeMode() == ShapeMode.RECT_ROUND){
                        bitmap = MyUtil.rectRound(bitmap,config.getRectRoundRadius(),0);
                        Glide.get(GlobalConfig.context).getBitmapPool().put(bitmap);
                    }*/

                config.getBitmapListener().onSuccess(bitmap);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                super.onLoadFailed(e, errorDrawable);
                if(e !=null){
                    e.printStackTrace();
                }
                config.getBitmapListener().onFail(e);
            }
        };
    }

    RequestManager requestManager =  Glide.with(config.getContext());
    int width = config.getWidth();
    int height = config.getHeight();
    if(width <=0){
        width = Target.SIZE_ORIGINAL;
    }
    if(height <=0){
        height = Target.SIZE_ORIGINAL;
    }
    getDrawableTypeRequest(config, requestManager)
            .asBitmap()
            .override(width, height)
            .transform(getBitmapTransFormations(config))
            .approximate().into(target);
}
 
Example 14
Source File: CustomTarget.java    From Simple-Dilbert with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new {@link CustomTarget} that will attempt to load the resource in its original size.
 *
 * <p>This constructor can cause very memory inefficient loads if the resource is large and can
 * cause OOMs. It's provided as a convenience for when you'd like to specify dimensions with
 * {@link com.bumptech.glide.request.RequestOptions#override(int)}. In all other cases, prefer
 * {@link #CustomTarget(int, int)}.
 */
public CustomTarget() {
    this(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL);
}