Glide Image showing Blur in fragment - android

I use glide in fragment using recycler view but images are not showing properly.
please suggest me to slove this issue
Glide.with(context)
.load( image_url)
.placeholder(R.drawable.pic)
.error(R.drawable.pic)
.fitCenter()
.into(Viewholder.imageView);

By default Glide uses low resolution images to save data and improve performance.
boolean isHighResolution = true;
BitmapRequestBuilder<String, Bitmap> builder = Glide
.with(this)
.load(url)
.asBitmap()
.format(isHighResolution
? DecodeFormat.PREFER_ARGB_8888
: DecodeFormat.PREFER_RGB_565)
builder.into(imageView);
You can try this code, and see if this helps.

The image to be loaded size should be greater and doesn't loaded properly and it is blurred.
use Bitmap and compress the image like below
Glide.with(mContext)
.load(album.getBannerUrl())
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
holder3.headerimage.setImageBitmap(bitmap);
Glide.with(mContext)
.load(album.getBannerUrl().get(0))
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
// Do something with bitmap here.
holder3.headerimage.setImageBitmap(bitmap);
}
});
}
});

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.
}
});

Hide and show a image with Glide

I load ad image in a map of Google with Glide. I want hide/ the image on same condition. How can I do? there is a something as Visible and Invisible?
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.error(null)
.into(new SimpleTarget<Bitmap>() {
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { //code }
}
i dont find anything useful but myself do something like load empty.png that just have 24 * 24 emplty transparent for marker this make your marker seems invisible
You can hold the view reference and change the visibility of it.
SimpleTarget imageView = new SimpleTarget<Bitmap>() {
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) { //code }
};
Glide.with(getApplicationContext())
.asBitmap()
.load(url)
.error(null)
.into(imageView);
Then you can hide/show imageView using setVisibility(View.GONE) or setVisibility(View.VISIBLE) method

Gif image is not showing its animation in android application

In my android application, I am using universal image loader to show images from url. During loading of image I want a gif image to be shown. But the gif image added is not showing any animation.
Here is my code to display image.
private static final DisplayImageOptions.Builder DEFAULT_DISPLAY_IMAGE_OPTIONS_BUIDLER = new DisplayImageOptions.Builder()
.imageScaleType(ImageScaleType.IN_SAMPLE_POWER_OF_2)
.displayer(new FadeInBitmapDisplayer(300, true, false, false))
.showImageForEmptyUri(R.drawable.default_image)
.showImageOnLoading(R.drawable.loadingx)
.showImageOnFail(sampleapp.sample.com.sampleapp.R.drawable.default_image).cacheOnDisk(true)
.cacheInMemory(true).bitmapConfig(Config.ARGB_8888);
what changes should I make to show an animated gif image during loading time image
Try to use glide for gif image loading...
Glide
.with( context )
.load( gifUrl )
.asGif()
.error( R.drawable.full_cake )
.diskCacheStrategy(DiskCacheStrategy.SOURCE)
.into( imageViewGif );
To understand follow the link -
here
If you want to use ProgressBar then create a method like below -
public void loadImage(Context context, String url, ImageView img, final ProgressBar eProgressBar) {
eProgressBar.setVisibility(View.VISIBLE);
Glide.with(context)
.load(url)
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
eProgressBar.setVisibility(View.GONE);
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
eProgressBar.setVisibility(View.GONE);
return false;
}
})
.crossFade()
.error(R.drawable.placeholder_image)
.into(img);
}
Use this method and pass your parameter value through it... :)
Universal Image Loader doesnot Support Gif see this Try using other libraries like Glide which supports gif
Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.yourgif)
.crossFade()
.into(imageView);
I got answer by using glide library. But glide is slower than universal image loader.
Glide.with(getActivity().getApplication())
.load(item.imageUrl)
.thumbnail(Glide.with(getActivity().getApplication()).load(R.drawable.loadingx))
.fitCenter()
.crossFade()
.into(holder.image);

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)
}

Return Shared Element Transition after ImageView changes

I have a fully working shared element transition between 2 fragments, lets say A to B, and they both share an ImageView. Everything works out except when I replace the shared ImageView on B. Currently I'm changing it using the following code:
Glide.with(getActivity())
.load(getArguments().getString(Constants.APPVIEW_FEATURE_IMAGE_URL)).asBitmap()
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
if (getView() != null) {
((ImageView) getView().findViewWithTag(Constants.TAG_FGRAPHIC)).setImageBitmap(resource);
}
}
});
I replace the ImageView that I received from A on B with a higher definition one, since this is a header Image that will extend from a listView to a full width header.
The problem is that when returning from B to A, the animation goes wrong because when I press Back, the image changes size right away to it's size on A instead of using the animation scaling down from it's B size to A size. It still moves from the header to the A location correctly, it's just the size problem.
If I don't replace the image, no problem happens and everything goes normal.
Any idea? Thanks.
I am not sure how to do it with Glide, but this is how I did it with Picasso.
Picasso.with(context)
.load(AppController.getInstance().getThumbor().buildImage(url)
.resize(widthPlaceholder, heightPlaceholder)
.fitIn()
.toUrl())
.placeholder(R.color.light_gray)
.centerCrop()
.noFade()
.config(Bitmap.Config.RGB_565)
.into(target);
Target target = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable d = new BitmapDrawable(context.getResources(), bitmap);
Picasso.with(context)
.load(AppController.getInstance().getThumbor().buildImage(url)
.resize(width, height)
.fitIn()
.toUrl())
.placeholder(d)
.centerCrop()
.noFade()
.config(Bitmap.Config.RGB_565)
.into(imageView);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
Picasso.with(context)
.load(AppController.getInstance().getThumbor().buildImage(url)
.resize(width, height)
.fitIn()
.toUrl())
.placeholder(R.color.light_gray)
.config(Bitmap.Config.RGB_565)
.centerCrop()
.noFade()
.into(imageView);
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Picasso.with(context)
.load(AppController.getInstance().getThumbor().buildImage(url)
.resize(width, height)
.fitIn()
.toUrl())
.placeholder(placeHolderDrawable)
.config(Bitmap.Config.RGB_565)
.centerCrop()
.noFade()
.into(imageView);
}
};
I am loading lower resolution picture and I am putting it as placeholder (background image), and than triggering higher res picture loading.

Categories

Resources