Resize images with Glide in a ImageView Android

百般思念 提交于 2019-11-29 06:46:48

Override must be accessed via RequestOptions in the most recent version of Glide 4.x. You can add RequestOptions through apply()

Glide
    .with(context)
    .load(path)
    .apply(new RequestOptions().override(600, 200))
    .into(imageViewResizeCenterCrop);

in glide 4.x 'override' is in 'apply' :

Glide.with(getBaseContext())
 .load(path)
 .apply(RequestOptions.placeholderOf(R.mipmap.no_wifi)
 .error(R.mipmap.no_wifi)
 .override(500,500))
 .into(mImageView);
Glide
    .with(context)
    .load(path)
    .override(600, 200) 
    .centerCrop() 
    .into(imageViewResizeCenterCrop);

@Raghunandan is right.you should try transformation your own way.

If you want to load image without using glide then you can use scaleType "fitCenter" or "centerInside" as follows -

<ImageView
        android:id="@+id/img"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="false"
        app:layout_collapseMode="parallax"
        android:scaleType="centerInside"
        app:layout_scrollFlags="scroll|enterAlways" />

With Glide you can use override(w,h). When you use override(w,h), glide generates a new bitmap with width and height mentioned in override(w,h) and then load the image into ImageView. You can use fitCenter() to align the image. You can also use diskCacheStrategy(). If you don't use it, Glide will catch only newly generated bitmap. If you want to catch original image also then use diskCacheStrategy(DiskCacheStrategy.ALL).

Glide.with(context)
            .load(image_path)
            .override(800, 400)
            .fitCenter()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imageView)

I hope this helps.

you have to do the following with your imageview :

        <ImageView
            android:id="@+id/img"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:scaleType="fitCenter" />

and this with your glide :

    Glide.with(getBaseContext())
            .load(path)
            //.placeholder(R.drawable.drawable)
            .error(R.drawable.noimg)
            .animate(R.anim.animation_fade_in)
            .into(mImageView);

This works perfectly with me .

Glide 4.x - If you want get bitmap, try:

Glide.with(mContext).asBitmap().
                        load(pictureUri)
                        .apply(new RequestOptions().override(50, 50))
                        .listener(new RequestListener<Bitmap>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                                return false;
                            }
                        @Override
                        public boolean onResourceReady(Bitmap resource, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
                            // resource is your loaded Bitmap
                            imgView.setImageBitmap(resource);
                            return true;
                        }
                    }).submit();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!