Loading images using Glide in Imageview - android

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.

Related

Android adding image to view programatically getting this error java.lang.StackOverflowError: stack size 8MB

Getting below error while adding an image to view dynamically.
Note: I am using .svg to set an image.
java.lang.StackOverflowError: stack size 8MB
at libcore.util.NativeAllocationRegistry.registerNativeAllocation(NativeAllocationRegistry.java:219)
at libcore.util.NativeAllocationRegistry.registerNativeAllocation(NativeAllocationRegistry.java:122)
at android.graphics.Bitmap.<init>(Bitmap.java:137)
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:1026)
at android.graphics.Bitmap.createBitmap(Bitmap.java:980)
at android.graphics.Bitmap.createBitmap(Bitmap.java:930)
at android.graphics.Bitmap.createBitmap(Bitmap.java:891)
at com.rpoli.localwire.libs.circleimageview.CircularImageView.a(CircularImageView.java:411)
at com.rpoli.localwire.libs.circleimageview.CircularImageView.a(CircularImageView.java:401)
at com.rpoli.localwire.libs.circleimageview.CircularImageView.invalidate(CircularImageView.java:337)
at android.widget.ImageView.invalidateDrawable(ImageView.java:281)
at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:450)
at android.graphics.drawable.Drawable.setBounds(Drawable.java:229)
I was having the same problem but I found it in my case, with the circleImageView as I changed it from center_crop and tried to adjustViewBounds which is not supported as this requires an unsupported ScaleType and also disabled the fade animations.
and I found the picture that I'm trying to use is going to be large to be cropped and actually it's of no use if you'll have a high resolution picture in a small circle.
Send me more details about problem with. I also share an example that I think glide might be more useful than picasso
build.grandle
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
//MainActivity
final RequestOptions options = new RequestOptions()
.placeholder(R.drawable.null_image_profile)
.error(R.drawable.null_image_profile);
Glide.with(context).load(s.getImage())
.apply(options)
.listener(new RequestListener<Drawable>() {
#Override
public boolean onLoadFailed(#Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
#Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.into(profileImage);

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

Loading Image into ImageView with Glide

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

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

How to load gif image in placeholder of Glide/Picasso/Ion etc

Not able to find perfect solution for loading a gif image in placeholder
Glide
.with(context)
.load("imageUrl")
.asGif()
.placeholder(R.drawable.gifImage)
.crossFade()
.into(imageView)
Tried asGif() property of Glide version 3.7.0 too. But no Luck!
Here is The Best Way..
Glide.with(getContext()).load(item[position])
.thumbnail(Glide.with(getContext()).load(R.drawable.preloader))
.fitCenter()
.crossFade()
.into(imageView);
I do it like mentioned below:
The idea is to create the gif using transition drawables & set the scale type to as required by the place holder initially & attach listener to change the scale type again to as required by the downloaded image after the image is downloaded. (last step can be skipped if you don't require it)
//ivBuilderLogo = Target ImageView
//Set the scale type to as required by your place holder
//ScaleType.CENTER_INSIDE will maintain aspect ration and fit the placeholder inside the image view
holder.ivBuilderLogo.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
//AnimationDrawable is required when you are using transition drawables
//You can directly send resource id to glide if your placeholder is static
//However if you are using GIFs, it is better to create a transition drawable in xml
//& use it as shown in this example
AnimationDrawable animationDrawable;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
animationDrawable=(AnimationDrawable)context.getDrawable(R.drawable.anim_image_placeholder);
else
animationDrawable=(AnimationDrawable)context.getResources().getDrawable(R.drawable.anim_image_placeholder);
animationDrawable.start();
Glide.with(context).load(logo_url)
.placeholder(animationDrawable)
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource)
{
return false;
}
//This is invoked when your image is downloaded and is ready
//to be loaded to the image view
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource)
{
//This is used to remove the placeholder image from your ImageView
//and load the downloaded image with desired scale-type(FIT_XY in this case)
//Changing the scale type from 'CENTER_INSIDE' to 'FIT_XY'
//will stretch the placeholder for a (very) short duration,
//till the downloaded image is loaded
//setImageResource(0) removes the placeholder from the image-view
//before setting the scale type to FIT_XY and ensures that the UX
//is not spoiled, even for a (very) short duration
holder.ivBuilderLogo.setImageResource(0);
holder.ivBuilderLogo.setScaleType(ImageView.ScaleType.FIT_XY);
return false;
}
})
.into( holder.ivBuilderLogo);
My transition drawable (R.drawable.anim_image_placeholder) :
(not required if using a static image)
<?xml version="1.0" encoding="utf-8"?>
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="#drawable/loading_frame1" android:duration="100" />
<!--<item android:drawable="#drawable/loading_frame2" android:duration="100" />-->
<item android:drawable="#drawable/loading_frame3" android:duration="100" />
<!--<item android:drawable="#drawable/loading_frame4" android:duration="100" />-->
<item android:drawable="#drawable/loading_frame5" android:duration="100" />
<!--<item android:drawable="#drawable/loading_frame6" android:duration="100" />-->
<item android:drawable="#drawable/loading_frame7" android:duration="100" />
<!--<item android:drawable="#drawable/loading_frame8" android:duration="100" />-->
<item android:drawable="#drawable/loading_frame9" android:duration="100" />
<!--<item android:drawable="#drawable/loading_frame10" android:duration="100" />-->
</animation-list>
Use ProgressBar as loading gif:
Glide.with(context).
load(url)
.listener(new RequestListener<String, GlideDrawable>() {
#Override
public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
#Override
public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
progressBar.setVisibility(View.GONE);
return false;
}
})
.crossFade(1000)
.into(imageView);
(static_placeholder is, say, the first frame of the GIF)
Glide...
.load("http://...")
.placeholder(R.drawable.static_placeholder)
.thumbnail(Glide.with(...).load(R.raw.gif_placeholder))
.dontAnimate() //so there's no weird crossfade
Placeholder is set much earlier than thumbnail, so it prevents "long" empty white ImageViews. You can skip the placeholder to simplify though.
or another option is to use AnimationDrawable(you can convert your GIF to AnimationDrawable from here):
AnimationDrawable animPlaceholder = (AnimationDrawable) ContextCompat.getDrawable(activity, R.drawable.animatedDrawable);
animPlaceholder.start(); // probably needed
Glide...
.load("http://...")
.placeholder(animPlaceholder)
Ref: link

Categories

Resources