Draw image view height and width before loading from url - android

I am getting image's height and width from API. I'm using Picasso to load images from url. My query is once we would get the API response how can we draw image as per it's height and width before actual image loading by Picasso. I have set image's height and width to Picasso in resize() method.
But Picasso loads images directly once image successfully downloaded. Here the issue is that I want draw image's height and width in preloading state.
/*
* set image view's height and width from API
*/
int imageViewHeight = getHeight();
int imageViewWidth = getWidth();
// picasso to laod an image from url
Picasso.with(view.getContext())
.load(url).placeholder(getGradientDrawable())
.resize(imageViewWidth, imageViewHeight)
.centerInside()
.into(imageView);
// gradient for placeholder
static GradientDrawable getGradientDrawable() {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setColor(Color.GRAY);
return gradientDrawable;
}
Any help appreciated.

I'm not sure if I understood you right but if you want a place holder for your image while Picasso doing a background download, you can use placeholder() function like below:
Picasso.with(context).load(url).placeholder(drawable).into(imageview);
you can create a drawable with your desired width and height or use a resource ID

This helped do it for me, setting the imageview max and min height and width to the image's dimensions before calling the Picasso library to load the URL. It might help someone else who is looking at this.
imageView.setMinimumWidth(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMinimumHeight(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMaxWidth(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMaxHeight(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));

Related

How to change the size of image lager than screen

Im trying to change an image lager than the screen so that the image partly(boundary parts) gets out of the screen. The purpose Im doing this is to make the whole screen filled with the image. I tried simply changing the height and width bigger than the height and width of screen which I got but it didn't work well. I used code below to get the size of the screen.
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels
val width = displayMetrics.widthPixels
How should I do for this?
In the xml match the height and width of the image to parent and change the scaleType attribute to centerCrop -> android:scaleType="centerCrop"
If you want to fill the image to entire screen like a zoomed view, I think you can try image loading libraries like Picasso or Glide to crop the image to fit it inside the layout and also it will handle the caching for large sized images.
For Picasso you can try something like this
Picasso.with(this).load(R.drawable.image).centerCrop().into(imageView);

Android Picasso load image to Bitmap variable cropping it to square

I need to load some images from URI to Bitmap variables and perform some operation with them togheter. I need the bitmaps to be squared images with fixes size, scaled down and cropped. By now I use this code:
return Picasso.with(c).load(imageUri).resize(size, size).get();
but, obviously, the image will be resized without keep its aspect ratio.
I want to resize the image with these requirements:
the smaller dimension (width or height) should be equals to size
the greater dimension should be cropped to size, keep image centered
The key is using centerInside after resize. See link
Picasso.with(c).load(imageUri).resize(size, size).centerInside().get()
set your imageview height and width fix inside xml and then set image to imageview like
Picasso.with(YourActivityName.this)
.load(imageUri)
.into(imageview_id);

android glide into(Target) OOM

I am using Glide frame to load a picture, I want to set the height and the width of it, and I know what is the width I want, but how can I set the height of it.
what I have tried is blow,and I got an error of OOM. waiting for help, thanks.
if you want change size
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
Reference
Using override method of glide you can set the dimension of image in pixels.
Glide
.with(context)
.load(url)
.override(50 , 50)
.into(imageview);

Fit listview cell to content after Downloading an image [duplicate]

I have a vertical LinearLayout where one of the items is an ImageView loaded using Picasso. I need to rise the image's width to the full device width, and to display the center part of the image cropped by a fixed height (150dp). I currently have the following code:
Picasso.with(getActivity())
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(screenWidth, imageHeight)
.centerInside()
.into(imageView);
Which values should I put into screenWidth and imageHeight (=150dp)?
You are looking for:
.fit().centerCrop()
What these mean:
fit - wait until the ImageView has been measured and resize the image to exactly match its size.
centerCrop - scale the image honoring the aspect ratio until it fills the size. Crop either the top and bottom or left and right so it matches the size exactly.
In some case the fit() is useless. Before you must wait for the width and height measurement to end. So you can use globallayoutlistener. for example;
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
Picasso.with(getActivity())
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(screenWidth, imageHeight)
.fit
.centerInside()
.into(imageView);
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});

Resize image with Picasso library

I am using Picasso to show image from url. My question is how can I resize the image to a variable heigth and width without crop the image? Currently when I resize the image without centerCrop() it stretches the image. This is what I have:
// Get current display dimensions
DisplayMetrics metrics = getResources().getDisplayMetrics();
width = metrics.widthPixels;
height = metrics.heightPixels / 2;
// Set image from URL in ImageView
Picasso.with(getApplicationContext())
.load(path)
.placeholder(R.drawable.loading).resize(width, height)
.centerCrop().into(image);
You need to use a method that will preserve the aspect ratio of your image. I would try
Picasso.with(getApplicationContext())
.load(path)
.placeholder(R.drawable.loading)
.resize(width, height)
.centerInside()
.into(image);
first, but it's not really clear to me what your exact needs are from your code. Do you want there to be padding around the image?

Categories

Resources