i'm using picasso library to download and load images into imageView. now i want to know how i can get image width and height before loading them in imageViews ?
i have a listview with an adapter that contains two imageView(one of them is vertical and another is horizontal). depends on image width and height i want to load image into one of the imageviews.
Image loading using Picasso is very easy, you can do it like this way Picasso. get(). load("http://i.imgur.com/DvpvklR.png").into(imageView); and in their website you can get every details.
Add an explicit width or height to the ImageView by setting layout_width=500dp in the layout file and then be sure to call fit () during your load: Picasso.with (…).load (imageUri).fit ().into (…) Open up your static placeholder or error images and make sure their dimensions are relatively small (< 500px width).
In the first line, we get a reference to the ImageView instance in the layout file. We then load an image into the image view using the Picasso library. We first specify the context by calling with () and passing in the context. We then call the load () method and supply in it with the location of the image.
It is among the powerful image download and caching library for Android. Picasso simplifies the process of loading images from external URLs and displays them on your application. For example, downloading an image from the server is one of the most common tasks in any application.
First, calling fit () can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit () with an ImageView as the target (we'll look at other targets later). The advantage is that the image is at the lowest possible resolution, without affecting its quality.
You can get Bitmap dimensions only after downloading It - you must use synchronous method call like this:
final Bitmap image = Picasso.with(this).load("http://").get();
int width = image.getWidth();
int height = image.getHeight();
After this you can call again load with same url (It will be fetched from cache):
Picasso.with(this).load("http://").into(imageView)
Edit: Maybe better way:
Picasso.with(this).load("http://").into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
imgView.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
Work for me.
Picasso.with(context).load(imageLink).into(imageView, new Callback() {
@Override
public void onSuccess() {
Picasso.with(context).load(imageLink).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
Log.d("ComeHere ", " W : "+ width+" H : "+height);
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
}
@Override
public void onError() {
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With