Loading Image into ImageView with Glide - android

I have an app which loads images from api into my imageView which is in my RecyclerView.
Here is my ImageView :
<ImageView
android:id="#+id/Group_ImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:layout_alignParentTop="true"
android:background="#color/_Opacity"
android:scaleType="centerInside"
android:src="#mipmap/ic_launcher" />
and here is the image loading part in my adapter with glide :
Glide.with(mContext).load(goodGroups.getImageUrl()).asBitmap().dontTransform().listener(new RequestListener<String, Bitmap>() {
#Override
public boolean onException(Exception e, String s, Target<Bitmap> target, boolean b) {
return false;
}
#Override
public boolean onResourceReady(Bitmap bitmap, String s, Target<Bitmap> target, boolean b, boolean b1) {
bitmap.setDensity(mContext.getResources().getDisplayMetrics().densityDpi);
return false;
}
}).placeholder(R.mipmap.ic_launcher).error(R.drawable.ic_menu_gallery).into(holder.imageView);
here is the screen shot of my problem below :
my problem is with the part shown by a red line in the picture. the blue line shows my Imageview. the red part isn't supposed to be shown.
I've tried changing the ScaleType to "FitXY" or "CenterCrop" but it messes up with the picture size. I want my picture shown without any cropping, with its original size, and without these padding-like spaces in ImageView.
What should I do?!

set android:adjustViewBounds="true" for your ImageView in the code

Try this set Image url.
Glide.with(getBaseContext())
.load("Your image url parse this place")
.thumbnail(0.1f)
.into(img_glide);

try following you have to user centerCrop or fitCenter methods.
Glide.with(context).load(url).centerCrop().placeholder(R.drawable.default_image).into(img);
or
Glide.with(context).load(url).fitCenter().placeholder(R.drawable.default_image).into(img);
or
Glide.with(getActivity().getApplicationContext())
.load(url).fitCenter()
.override(500, 758)
.into(mMovieBackgroundImageView);

Related

Different scale type for place holder and image

I want fitXY scaleType for image and fitCenter for place holder. I am fetching data from api using volley and loading image using glide.
How am I supposed to know that image is null ?
XML and code
<ImageView
android:id="#+id/NewsImage"
android:layout_width="120dp"
android:layout_height="120dp"
android:scaleType="fitCenter" />
Glide.with(context).load(news.getUrlToImage()).placeholder(R.drawable.place_holder).dontAnimate().into(holder.NewsImage);
1.USING LISTENER
Using GLIDE you can add a listener for image loading. Once the image is loaded change the SCALE type of the ImageView.
Glide.with(getActivity())
.load(args.getString(IMAGE_TO_SHOW))
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
imgview.setScaleType(ImageView.ScaleType.FIT_XY);
return false;
}
})
.into(imgview)
2.USING GLIDE
You can use one of the GLIDE transformations for the image or create your custom transformation
https://github.com/bumptech/glide/wiki/Transformations
You can use Best Image Loading Library Picasso for this.
If their is no image it will show place holders image into imageview
Picasso.get()
.load("http://i.imgur.com/DvpvklR.png") //loading url image
.placeholder(R.drawable.custom_image) // during loading this image will be set imageview
.error(R.drawable.error) //if image is failed to load - this image is set to imageview
.networkPolicy(NetworkPolicy.OFFLINE) //stores images for offline view
.resize(50, 50) //resize
.centerCrop() // apply scaling OR
.fit() //apply scaling OR
.centerInside() //scaling
.into(imageView, new Callback() {
#Override
public void onSuccess() {
//called when image is loaded successfully.. \n \n
}
#Override
public void onError(Exception e) {
//called when image is failed to be loaded into.
}
});

How to use Glide's SimpleTarget with databinding?

I use Glide to load my image. I need to modify my image with a SimpleTarget callback, however when the image loaded to my list, and I scroll the list shows first always an other image, and than animate the right image after 1 second to the place. Without image modification and SimpleTarget everything works just fine. Here is my code.
#BindingAdapter("imageSrc")
public static void setImage(ImageView imageView, String url) {
Glide.with(imageView.getContext()).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
//the bitmap modified here
imageView.setImageBitmap(bmp);
}
});
}
is there any solution to avoid the flickering?

Android: Glide not showing large image from URL

I'm trying to load a large hi-res (3225x4800) image from URL into glide for a newspaper company. The image I wanted to load is this High Res Image.
String url = "http://www.businessweekmindanao.com/content/wp-content/uploads/2015/10/fitness-and-wellness-poster-final.jpg";
Glide.with(getActivity()).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageView.setImageBitmap(resource);
}
});
I'm using Mike Ortiz' TouchImageView from the solution offered here: https://github.com/MikeOrtiz/TouchImageView/issues/135 when loading resource from url. But it seems to only work on small resolution image and it does not load high quality image.
There is an alternative solution I tried from stackoverflow: Android: not displayed ImageView with UIL and TouchImageView which tries to modify the onMeasure() method of Mike's library. It works with:
Glide.with(getActivity()).load(url)
.into(imageViewPreview);
The problem is it loads the image in very low resolution.
Is there a way to load high resolution image like: 7480x3740 for instance using Glide and TouchImageView? How do I do that?
I have found a solution that worked for my requirement. The override() method does the trick. Setting higher numbers for the target resolution seems to be the final workaround, but the bigger the numbers, the longer the time it would take to display the image(s), so it would be wise to implement a preloader/progressbar.
Glide.with(getContext())
.asBitMap() //[for new glide versions]
.load(url)
//.asBitmap()[for older glide versions]
//.placeholder(R.drawable.default_placeholder)
.override(1600, 1600) // Can be 2000, 2000
.into(new BitmapImageViewTarget(imageViewPreview) {
#Override
public void onResourceReady(Bitmap drawable, GlideAnimation anim) {
super.onResourceReady(drawable, anim);
progressBar.setVisibility(View.GONE);
}
});
Custom centered preloader/progressbar placeholder indicator on relativeLayout xml:
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
This is neat because another issue related about loading image asBitmap() in Glide will not show placeholders for error and preloader/progress indicator:
Glide.with(getActivity())
.load(url).asBitmap()
.placeholder() //<== will simply not work:
.error() // <== is also useless
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
imageView.setImageBitmap(resource);
}
});
In my research, I have also tried a Picasso solution to handle high quality image:
Picasso.with(getContext())
.load(url)
.resize(1500, 0)
.placeholder(R.drawable.default_placeholder)
.error(R.drawable.download_error)
.into(imageViewPreview);
In the end, I was happy with the Glide version.
I hope this will be helpful to anyone that might be facing the same challenge.
I had issue while loading image from url into TouchImageView using Glide. It would show a black screen.
I found a fix which works like this. Pass width and height in Glide override method and after setting bitmap call setZoom of TouchImageView with value 1.
Here is the complete code:
Glide.with('context')
.asBitmap()
.load('url')
.apply(new RequestOptions().override(1600, 1600)) //This is important
.into(new BitmapImageViewTarget('imageview') {
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
super.onResourceReady(resource, transition);
'imageview'.setImageBitmap(resource);
'imageview'.setZoom(1); //This is important
}
});
Add android:usesCleartextTraffic="true"
Glide will start loading images in android 10
I have same issue and it is due to large image size. resolved it by following way:
Glide
.with(imageView.context)
.load(url)
.placeholder(R.drawable.placeholder)
.apply(RequestOptions().transform(CenterCrop(), RoundedCorners(if (applyCorner) 32 else 1)))
.into(object : CustomTarget<Drawable>(200, 200) {
override fun onLoadCleared(placeholder: Drawable?) {
// called when imageView is cleared. If you are referencing the bitmap
// somewhere else too other than this imageView clear it here
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
imageView.setImageDrawable(resource)
}

Loading images using Glide in Imageview

I am using Glide library for loading images in imageview and using the below code.
Glide.with(mActivity)
.load(img.getmGridViewImageUrl())
.placeholder(R.color.grey_light)
.into(imageHolder.imageView);
The placeholder with grey color gets visible until the image does not load but after the image get loads in imageview, the placeholder still take place and show some blank space after that image.
How can I resolve this issue. Please help me if you have any idea here.
Try this code.
When I give fix height to the ImageView it works for me.
Here's the layout
<ImageView
android:id="#+id/imgPoster"
android:layout_width="fill_parent"
android:layout_height="#dimen/height_of_getInspired_list"
android:layout_centerVertical="true"
android:scaleType="fitXY"
android:src="#drawable/default_image"/>
Here's my code.
Glide.with(this.context).load(inspiredList.get(position).image)
.error(R.drawable.default_image)
.centerCrop()
.crossFade()
.placeholder(R.drawable.default_image).into(holder.imgPoster);
Try this code.. This will help you..
Glide.with(this).load(photo_url)
.crossFade()
.thumbnail(0.5f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.placeholder(R.drawable.plaeholder_image)
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
imageView.setImageDrawable(resource);
return false;
}
})
.into(imageView);
Please use placeholder image instead of color. you will able to use plain grey color image from drawable for placeholder image.

How to add textview on Imageview using bitmap drawable in android?

Task
1) Download the image from repository using picasso library
2) Add text and Image on specific coordinates on downloaded image. I don't want to use RelativeLayout. Can we do it using bitmap , canwas, drawable?
3) After adding Text and Image on specific coordinates load the Image on ImageView specified in Picasso.load() section [Please have a look at the code below].
How can I do that?
Example (Look at the heart and amount)
I am using picasso to download images:
private Target loadtarget;
public void loadBitmap(String url) {
if (loadtarget == null) loadtarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// do something with the Bitmap
handleLoadedBitmap(bitmap);
}
#Override
public void onBitmapFailed() {
}
}
Picasso.with(this).load(url).into(loadtarget);
}
public void handleLoadedBitmap(Bitmap b) {
// do something here
}
my ImageView.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"/>
Now I want to add text or image on above image (loaded by picasso) using bitmap and canvas. How can I do that?

Categories

Resources