how to load xml in imageview using glide - android

I have created an gradient xml in drawable folder of my project. I want to load that xml in an ImageView using Glide. How can I do that ?
Simple loading of image in ImageView using Glide:
Glide.with(LoginActivity.this).load(R.drawable.a270).asBitmap().diskCacheStrategy(DiskCacheStrategy.RESULT).into(backgroundImage);
But if I use the same code above replacing R.drawable.a270 with R.drawable.gradient_background with a drawable xml its not working.

Please try this -
Glide.with(LoginActivity.this).load("").error(R.drawable.gradient_background).diskCacheStrategy(DiskCacheStrategy.RESULT).into(backgroundImage);
OR
Glide.with(LoginActivity.this).load(null).fallback(R.drawable.gradient_background).diskCacheStrategy(DiskCacheStrategy.RESULT).into(backgroundImage);
I doubt you are using VectorDrawable or animated drawable. Please refer this and this for more information on this.

Related

How to put a svg file in your app?

I would like to insert a svg picture in my app so I looked for in Youtube ans I follow this tutorial :
https://www.youtube.com/watch?v=S65WtImJOvI
I create a directory by the name of raw, I download svg-android.jar and I added the following lines in my MainActivity.java :
ImageView imageView = new ImageView(this);
imageView.setBackgroundColor(Color.WHITE);
SVG svg = SVGParser.getSVGFromResource(getResources(), R.raw.android);
imageView.setImageDrawable(svg.createPictureDrawable());
setContentView(imageView);
The svg picture is android.svg. But when I tried to compile and execute with an emulator it crashes ! The app does not open !
So, May I have to do something in the activity_main.xml ?
What do you think about this ?
Thank you very much !
Easy,
Add it like this, and use inside of ImageView
app:srcCompat="#drawable/ic_person_outline_black_24dp"
Default Android ImageView does not support SVG files. You can include a custom library to perform this operation.
This is a wonderfull library I've used so far : https://github.com/Pixplicity/sharp
The easier way is to transform your SVG into a XML vector image (using this website for example).
Then you can use it almost like a standard resource drawable.
For example in your xml layout, on an ImageView, you'll have to use app:srcCompat instead of android:src.
You may have to add vectorDrawables.useSupportLibrary = true in your gradle file too.

Load dyanmic resource drawable using Picasso

I want to load multiple drawable-resources in RecyclerView and i am using Picasso in RecyclerView.ViewHolder for this task.
Picasso.with(context)
.load(imageList.get(position))
.into(imageView);
Picasso loads the drawable-resources which are in drawable folder but it doesn't load resources from other drawable.
For example if i put the drawables in all drawable folders(mdpi,hdpi,xhdpi etc) except "drawable" folder, It doesn't work.
So is there something I am missing? is it possible to dynamic reference to the drawables using Picasso?
Picasso should load mipmaps. Try forcing just one to show, like
Picasso.with(context).load(R.mipmap.ic_launcher).into(imageView);
and see if is loading. If it is, there is something with your id's from array.

How to show dynamic drawable using Picasso

I have some drawable generated dynamically from material design icons
Drawable qq = new IconDrawable(context, FontAwesomeIcons.fa_qq).
colorRes(R.color.qq).sizeRes(R.dimen.button_height_normal);
I want to show this Drawable using Picasso but when try to load this Drawable get an error because Picasso load accept only url for the Drawable and I can't get it or get it's Resource Id.
there any way to make that ?
I've made a workaround as I've not seen the way to set directly a dynamically generated drawable. Picasso has the "error" and "placeholder" options that they both allow inputing a drawable so if you make the load to fail the Picasso will load the drawable (with you desired transformations, for instance):
Picasso.with(context).load("nothing").error(placeholder).placeholder(placeholder).into(this)
where placeholder is the drawable you want to load

Android: How to use TextView in placeholder of Glide or Picasso

i am using Glide or Picasso for loading the image like
Glide.with(context).load(POST_IMAGE).placeholder(R.drawable.loading_img).error(R.drawable.bg_480_800).into(image);
Picasso.with(context).load("image url").error(R.drawable.bg_480_800).placeholder(R.drawable.loading_img).into(holder.imageURL);
In these two, i'm using image as place holder from the drawable folder. Now i want to use text inside the place holder instead of image from drawable folder.
So, please help me to load text instead of loading image from the drawable folder
Placeholder is a Drawable, so what you'll need is some kind of Drawable that is able to display text. This library does exactly that, and does it pretty well.

How to load GIF image from drawable folder into ImageView using glide:3.6.0

I am using glide in my project for the purpose of loading images from the url. But I have another requirement to load GIF image background for my view from drawable folder using glide. I am completely stuck up with the methodology and solutions for loading GIF into my view. I have been struggling with for a couple of days. Kindly please help me with youor solutions to perform loading GIF from drawable using Glid 3.6.0. Thanks in advance. I am posting the piece of code that I am using in my project as follows,
Glide.with(LoginActivity.this)
.load(getResources ().getDrawable (R.drawable.bg_gif));
.placeholder(R.drawable.login_bg)
.crossFade()
.into(relativeLayout);
Note : I have tried using .asGif() method to load, but unfortunately it is indicating the error cannot resolve method asGif()
it may be late now but I load my gif from resources this way:
Glide.with(fragment).load(id).asGif().placeholder(R.mipmap.ic_launcher).into(img);
id is an (int) with the resource file, example:
R.drawable.YOURGIFNAME.gif
In my case I call glide giving a FragmentActivity, but glide can be called different way check docs https://github.com/bumptech/glide
I found out that the gif wont load if you dont add the:
.placeholder(R.mipmap.ic_launcher)
Hope it helps.
Use the following snippet:-
GlideDrawableImageViewTarget imageViewTarget = new
GlideDrawableImageViewTarget(imageView);
Glide.with(this).load(getResources ().getDrawable (R.drawable.bg_gif)).
into(relativeLayout);

Categories

Resources