I'm trying to show the user photo in BottomNavigationView but I don't know how to do it correctly.
This solution is working fine but other items will lose their colors
Drawable drawable = activityMainBinding.activityMainBottomNavigationViewBottomNavigation.getMenu().findItem(R.id.menu_bottom_navigation_user).getIcon();
activityMainBinding.activityMainBottomNavigationViewBottomNavigation.setItemIconTintList(null);
The Code Result
I tried to solve the problem by applying the setItemIconTintList method on the user-item only
Drawable drawable = activityMainBinding.activityMainBottomNavigationViewBottomNavigation.getMenu().findItem(R.id.menu_bottom_navigation_user).getIcon();
drawable.setState(null);
drawable.setTintList(null);
drawable.clearColorFilter();
activityMainBinding.activityMainBottomNavigationViewBottomNavigation.getMenu().findItem(R.id.menu_bottom_navigation_user).setIcon(drawable);
The Code Result
How can I solve the problem?
I hope someone shows the attached images directly because I cannot do it.
You Can Use This Code:
Menu navMenu = activityMainBinding.activityMainBottomNavigationViewBottomNavigation.getMenu();
MenuItem menuItem = navMenu.findItem(R.id.menu_bottom_navigation_user);
Glide.with(context)
.asBitmap()
.load("your image")
.apply(RequestOptions
.circleCropTransform()
.placeholder("Your PlaceHolder"))
.into(new SimpleTarget<Bitmap>() {
public void onResourceReady(Bitmap resource, Transition<Bitmap> transition) {
menuItem.setIcon(new BitmapDrawable(context.getResource(), resource));
}
})
I have this XML:
<de.hdodenhof.circleimageview.CircleImageView
android:src="#drawable/ic_kitty_superhero"
android:id="#+id/userAvatar"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:layout_marginBottom="16dp"
app:civ_border_color="#android:color/white" // just to show you wrong result
app:civ_border_width="1dp" />
and this extension function to load image:
fun ImageView.loadImage(context: Context, url: String) {
val resourceID = context.resources.getIdentifier(url, "drawable", context.packageName)
if (resourceID != 0) { // if it's an image from drawable folder
Glide.with(context)
.load(resourceID)
.fitCenter()
.into(this)
} else {
Picasso.with(context)
.load(url)
.fit()
.centerInside()
.into(this)
}
}
Since Picasso doesn't know how to work with resourceID, I need to use Glide or something similar like this.setImageResource(). But it's cannot execute transformation that I want.
In the first photo, everything looks the way I want, but it’s just used ImageView container.
In the second photo I use CircleImageView container and it no longer looks the way I would like - the ears are cropped in the photo.
Please, can you help me, how can I get the correct result?
NOTE: I can't use .load(R.drawable.icon) and I can't use the ImageView container (I can probably if make it circle somehow)
I solved my problem by using ImageView container and Circle Transformation for Picasso
I've been using CircleImageView till now. But now I need to add background as well as src. Both these features are available in native ImageView. But when I add background color to CircleImageView, the background becomes square instead of circle. I tried many custom circle imageview but to no avail. Is there any way to achieve this?
Use this RoundedImageView code from github
RoundedImageView
Check my code using glide with normal imageview:
Glide.with(getActivity()).load(dashBoardCompanyInfoModel.getLogo()).asBitmap().into(new BitmapImageViewTarget(ivProfile) {
#Override
protected void setResource(Bitmap resource) {
final RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(getResources(), resource);
circularBitmapDrawable.setCircular(true);
ivProfile.setImageDrawable(circularBitmapDrawable);
}
});
I try to show a GIF image as loading placeholder in image view - with Glide Library:
Glide.with(context)
.load(ImageUrl())
.placeholder(R.drawable.loading2)
.asGif()
.crossFade()
.into(image);
I try to show this file
loading2.gif
but get this error :
Error:(54, 86) error: cannot find symbol method asGif()
How can I show GIF file with Glide in a imageView?
The above answer didn't work for me. The below code works.
ImageView imageView = (ImageView) findViewById(R.id.imageView);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(R.raw.sample_gif).into(imageViewTarget);
For Glide 4.+
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(this).asGif().load(R.raw.image_gif).into(imageView);
For Glide 3.0 you need to set asGif() earlier:
Glide.with(context)
.load(imageUrl)
.asGif()
.placeholder(R.drawable.loading2)
.crossFade()
.into(imageView);
Keep in mind that just using load() will load either a GIF or a Bitmap depending on the type of the data. Unless you want your load to fail if the given url is not a gif, you don't need to specify asGif()
I had this same problem. You can resolve it by moving where the asGif() is located. It must come directly before .load().
So change:
Glide.with(context)
.load(ImageUrl())
.placeholder(R.drawable.loading2)
.asGif()
....
to
Glide.with(context)
.asGif()
.load(ImageUrl())
.placeholder(R.drawable.loading2)
....
Here is another example. I have observed that Glide doesn't play Gif automatically sometimes. This solution worked for me:
In the example, imageView is one of the members in the RecyclerView's ViewHolder class.
Glide.with(itemView.getContext())
.asGif()
.load(thumbPath)
.placeholder(ResourcesCompat.getDrawable(context.getResources(),
R.drawable.image_loading_placeholder, null))
.centerCrop()
.into(new ImageViewTarget<GifDrawable>(imageView) {
#Override
protected void setResource(#Nullable GifDrawable resource) {
imageView.setImageDrawable(resource);
}
});
The method .asGif() needs to be used earlier or it will not be recognized
Use GlideDrawableImageViewTarget. This code works for me.
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(R.drawable.logo_gif).into(imageViewTarget);
Glide automatically plays animated gif files, no need of extra method
Use below code
Glide.with(this)
.load(R.drawable.logo)
.into(imageView);
put your gif in raw folder and the use this below code also add the below
dependency in your gradle
//for glide
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
ImageView layoutAnim;
//load gif file to imageView where layoutanim is my imageView
Glide.with(this).asGif().load(R.raw.backloader).into(layoutAnim);
From Glide version v4 :
Dependency:
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
Visit this link To create GlideApp class:
You can directly give drawable reference on GlideApp
GlideApp.with(this).load(R.drawable.ic_loader).into(mBinding.progressBar);
If you get any error, please let me know.
I'm using Glide version 4.11 and for me it did work setting .asGif() right after Glide.with(context).
But I had another problem, I've also set .dontAnimate() when loading gif into ImageView. Apparently this doesn't work, so I had to remove .dontAnimate() from my setup, and everything worked after that change.
Here is issue for more info https://github.com/bumptech/glide/issues/3395
Here is The Best Way
Glide.with(a).load(item[position])
.thumbnail(Glide.with(a).load(R.drawable.preloader))
.fitCenter()
.crossFade()
.into(imageView);
Try this. It worked for me and will work for your code too.It works for both images as well as gifs too.
ImageView imageView = (ImageView) findViewById(R.id.test_image);
GlideDrawableImageViewTarget imagePreview = new GlideDrawableImageViewTarget(imageView);
Glide
.with(this)
.load(url)
.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) {
return false;
}
})
.into(imagePreview);
}
This solution is kind of work around
<RelativeLayout
android:layout_width="wrap_content"
android:id="#+id/ImageContainer"
android:background="#fff"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:id="#+id/loadingImageView"
android:layout_centerInParent="true"
android:layout_height="wrap_content"/>
<ImageView
android:id="#+id/mainImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:layout_centerInParent="true"
/>
</RelativeLayout>
instead of using one, use 2 ImageViews
Put loading gif in first ImageView using the solution given by #Preetam
ImageView loadingImageView = (ImageView) findViewById(R.id.loadingImageView);
GlideDrawableImageViewTarget loadingImageViewTarget = new GlideDrawableImageViewTarget(loadingImageView);
Glide.with(this).load(R.raw.spinner).into(loadingImageViewTarget);
Load Image from network in the second ImageView
(Order is important if you don't want to hide the loading gif once
the image from network is loaded)
ImageView mainImageView = (ImageView) findViewById(R.id.mainImageView);
GlideDrawableImageViewTarget mainImageViewTarget = new GlideDrawableImageViewTarget(mainImageView);
Glide.with(this).load(imageUrl).into(mainImageViewTarget);
Instead of GlideDrawableImageViewTarget use DrawableImageViewTarget
ImageView imageView =findViewById(R.id.imageView);
DrawableImageViewTarget imageViewTarget = new DrawableImageViewTarget (imageView);
Glide.with(this).load(R.raw.image_loader).into(imageViewTarget);
library: implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
I am trying to insert a generic loading circle as placeholder while an image is being loaded with an image loader library like Glide or Picasso.
I cannot for the life of me find out how you are supposed to create a rotating drawable from xml.
I tried using an AnimationDrawable in XML by creating an animation-list, but it doesn't even show up (not even static).
I just want to have a simple circle that spins all by itself, all in an xml drawable, so I can pass the id as placeholder to my image loader. If that is not possible please tell me now, so I can save a lot of research :)
EDIT: Some code to make it more clear, I need a spinning drawable for this Glide command:
Glide.with(context)
.load(imageUrl)
.into(imageView)
.placeHolder(R.drawable.spinning_loading_placeholder);
While the image is being loaded a placeholder drawable will be shown where the image will later be. I need a drawable that spins itself.
You can add a ProgressBar element to your layout XML, and then display/hide this element as needed. The ProgressBar View is built in and already animated, no custom AnimationDrawable required.
You can use something like this:
Glide.with(mContext).load(imageUrl).asBitmap().centerCrop().into(new BitmapImageViewTarget(vh.avatarImageView) {
#Override
public void onLoadStarted(Drawable placeholder) {
vh.avatarImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_placeholder));
}
#Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
vh.avatarImageView.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ic_failed_download));
}
#Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable circularBitmapDrawable =
RoundedBitmapDrawableFactory.create(mContext.getResources(), resource);
circularBitmapDrawable.setCircular(true);
vh.avatarImageView.setImageDrawable(circularBitmapDrawable);
}
});