I am having trouble showing images correctly in my application.
I wish the picture would retain its original proportions. I display
picture in the AppBar,
I am using Picasso to download images, but cannot get the height and width of the downloaded image.
I've tried various solutions from other threads on StackOverflow, but unfortunately none of them work.
Can you help me get the height and width of the downloaded image using Picasso?
You can use bitmap target.
Load image in bitmap, get width and height and then set bitmap image in imageView. Glide library has the same feature.
Picasso.with(this).load(url).into(new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
imageView.setImageBitmap(bitmap);
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
Related
I use Glide to load my image. I need to modify my image with a SimpleTarget callback, however when the image loaded to my list, and I scroll the list shows first always an other image, and than animate the right image after 1 second to the place. Without image modification and SimpleTarget everything works just fine. Here is my code.
#BindingAdapter("imageSrc")
public static void setImage(ImageView imageView, String url) {
Glide.with(imageView.getContext()).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
//the bitmap modified here
imageView.setImageBitmap(bmp);
}
});
}
is there any solution to avoid the flickering?
I am using the Glide image loading library and I'm having issues when it comes to resizing bitmaps.
When using the following code:
Glide.with(getActivity())
.load(images.get(i))
.asBitmap().centerCrop()
.into(new SimpleTarget<Bitmap>(1200, 1200) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
}
});
Every single bitmap gets resized to the specified dimensions. So, if the image is 400x300, it gets upscaled up to 1200 x 1200 which I do not want. How do I make it so that if the image is smaller than the specified dimensions, it won't resize it?
I'm specifying dimensions because I want every image that's bigger than the specified dimensions to be resized taking into account centerCrop; and then if the image is smaller than the specified dimensions, I don't want it to be resized.
I want every image that's bigger than the specified dimensions to be
resized taking into account centerCrop; and then if the image is
smaller than the specified dimensions, I don't want it to be resized.
You can obtain this behaviour with a custom transformation:
public class CustomCenterCrop extends CenterCrop {
public CustomCenterCrop(BitmapPool bitmapPool) {
super(bitmapPool);
}
public CustomCenterCrop(Context context) {
super(context);
}
#Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
if (toTransform.getHeight() > outHeight || toTransform.getWidth() > outWidth) {
return super.transform(pool, toTransform, outWidth, outHeight);
} else {
return toTransform;
}
}
}
and then use it like this:
Glide.with(getActivity())
.load(images.get(i))
.asBitmap()
.transform(new CustomCenterCrop(getActivity()))
.into(new SimpleTarget<Bitmap>(1200, 1200) {
#Override
public void onResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
}
});
I'm using Picasso class/library to load an image from an url and display the image to a ImageView. Is it possible for me to set the imageview loaded by the picasso image loader from an url as the background image of a linearlayout programmatically?
I've already found this issue - might be useful for you:
How do i set background image with picasso in code
According to that, Use callback of Picasso
Picasso.with(getActivity()).load(R.drawable.table_background).into(new Target(){
#Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom from) {
mainLayout.setBackground(new BitmapDrawable(context.getResources(), bitmap));
}
#Override
public void onBitmapFailed(final Drawable errorDrawable) {
Log.d("TAG", "FAILED");
}
#Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
Log.d("TAG", "Prepare Load");
}
})
Read also
Set background resource using Picasso
but there would you find the same solution.
I'm switching from Picasso to Glide in my app. I have a RecyclerView, each of whose items is a CustomView. This CustomView adds TextView or ImageView dynamically based on different data sources.
A FillWidthImageView is used to display these images, it fill the screen width automatically. When I use Picasso, it works well. However, when I use Glide to load images, the image displayed in the FillWidthImageView looks like a mosaic with extremely low resolution, because it's heavily downsampled.
And if I use a normal ImageView instead, size of loaded image is the same as the placeholder, and images still look like a mosaic. If there is no placeholder, the images look tiny on the screen. And if I display the images in an empty Activity, they will be loaded with full resolution and correct size.
Code of adding ImageView in my CustomView(extends LinearLayout), called in RecyclerView.onCreateViewHolder()
public void addImageView() {
ImageView imageView = new FillWidthImageView(getContext());
imageView.setPadding(0, 0, 0, 10);
imageView.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
addView(imageView);
}
Code of FillWidthImageView
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Drawable drawable = getDrawable();
if (drawable != null) {
int width = MeasureSpec.getSize(widthMeasureSpec);
int diw = drawable.getIntrinsicWidth();
if (diw > 0) {
int height = width * drawable.getIntrinsicHeight() / diw;
setMeasuredDimension(width, height);
} else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
} else
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
Code of loading images, called in RecyclerView.onBindItemViewHolder()
Glide.with(getContext()).load(url).into(imageView);
Thanks #Hitesh Kr Sahu, but I have solved of the bug by myself. It's because the Glide didn't get the right dimensions of my CustomView wrapper. I solved this bug by applying the dimensions by calling .override(int,int) and .fitCenter(). I passed the width and height of screen to it, so it will be scaled fitting the screen size.
Image loaded by Glide has the worse quality compared to Picasso because Glide's default Bitmap Format is set to RGB_565 & it consumed just 50% memory footprint compared to ARGB_8888.
That make Glide faster than Picasso as you can see in this video.
Good news is you can switch Bitmap Format to ARGB_8888 by creating a new class which extended from GlideModule like this :-
public class GlideConfiguration implements GlideModule {
#Override
public void applyOptions(Context context, GlideBuilder builder) {
// Apply options to the builder here.
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
#Override
public void registerComponents(Context context, Glide glide) {
// register ModelLoaders here.
}
}
and adding metadata field in manifest
<meta-data android:name="com.inthecheesefactory.lab.glidepicasso.GlideConfiguration"
android:value="GlideModule"/>
You can read more about it here glide Configuration page of Glide's wiki and as you have switched from Picasso you will find this an interesting read
Task
1) Download the image from repository using picasso library
2) Add text and Image on specific coordinates on downloaded image. I don't want to use RelativeLayout. Can we do it using bitmap , canwas, drawable?
3) After adding Text and Image on specific coordinates load the Image on ImageView specified in Picasso.load() section [Please have a look at the code below].
How can I do that?
Example (Look at the heart and amount)
I am using picasso to download images:
private Target loadtarget;
public void loadBitmap(String url) {
if (loadtarget == null) loadtarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
// do something with the Bitmap
handleLoadedBitmap(bitmap);
}
#Override
public void onBitmapFailed() {
}
}
Picasso.with(this).load(url).into(loadtarget);
}
public void handleLoadedBitmap(Bitmap b) {
// do something here
}
my ImageView.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"/>
Now I want to add text or image on above image (loaded by picasso) using bitmap and canvas. How can I do that?