I'm using this news api to pull the json data into my app and render it in my articlerecycler adapter class.
From the api there is a json value urlToImage that contains the thumbnail that I need to render. Currently I have a working list of 20 or so articles displaying the author, title, description, etc.
What is the best way to show the thumbnails as well?
Is using ImageView widget the best way to handle this?
For example,
<ImageView
android:id="#+id/thumbnailIv"
android:layout_width="94dp"
android:layout_height="93dp"
android:layout_weight="1"
android:contentDescription="#string/urlToImage"
android:scaleType="fitCenter"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/placeholder_image" />
I've found through other posts that picasso is a good way to lazy load images, specifically thumbnails.
// load the image with Picasso
Picasso.get()
.load("") // load the image
.into(thumbnailIv) // select the ImageVi
}
Yes use ImageView and use library like Glide or picasso to load image into ImageView from the url
GlideApp.with(context)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.DATA) //optional
.placeholder(R.drawable.default_image) //optional
.error(R.drawable.default_image) //optional
.into(holder.imageNews);
With ImageView you can control ScaleType another useful property is android:adjustViewBounds="true" which will maintain aspect ratio . If you use use linear/relative and set image as background then you cannot control these properties
Related
I'm using the Glide image loading library to load an image into my ImageView.
Glide.with(this).load(url).into(imageView);
I was reading the ReadMe on how to use Glide on the github page and what I noticed is that in the sample they use the method .centerCrop() to crop the image.
Glide.with(this).load(url).centerCrop().into(imageView);
Is there any benefit of using this method over just using the scaleType attribute in an ImageView?
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="centerCrop"
I tried it without the .centerCrop() method and just using the xml attribute and they essentially look the same. Does Glide have some sort of under the hood optimization that consumes less memory or something which makes that method a better choice?
centerCrop doesn't do anthing to the memory usage , it's just about scaling image
I've started an app where I'm getting images from a RESTful server in response to the user clicking on another component on the screen. I've set it up so that it gets the data back, creates a byte array and displays that on the screen - all good.
However I've learnt that this may have memory leaks and the best way to load images is with glide. I've got the simple glide working but it has problems. Specifically when I reload an image onto an image view the component shrinks to a height of zero and then resizes again. How can I say to Glide - keep the component the same size during the transition? Crossfade doesn't work and I can't see anything that can do this.
EDIT
The code with the glide is as follows:
Glide.with(this)
.load(Base64.decode(image, Base64.DEFAULT))
.into((ImageView) findViewById(R.id.cutoutImageView));
The image variable is a string containing the images retrieved from a RESTful service.
The XML defining the imageview is as follows:
<ImageView
android:id="#+id/cutoutImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/annotatedImageView"
android:layout_marginLeft="10px"
android:layout_marginTop="10px"
android:background="#color/colorPrimary"
android:padding="1dp"/>
Note that I can't hardcode the size of the image view because I don't know what size image I'll be getting from the RESTful service until I get it. I do know that once I get one image then the subsequent images will all be the same size though.
Another issue is that glide wants to scale the iamge - I don't I want to show the image at the size I get it. How do I do that?
How do I display images in a facebook like feed? So far I've been using match_parent for width and wrap_content or fixed height. This works well but I need to show proper blank space for image feeds while it's getting lazy loaded. Like how do I set the dimensions properly for the image before loading it from the internet.
I think about getting the aspect ratio or the image dimensions itself via the api. Is it the only correct way or is there some other better way to achieve it using glide or any image loading library?
If you are using glide, you can set a place holder image or drawable with the dimensions you need while the real image is being downloaded.
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.placeholder(R.mipmap.ic_launcher) // can also be a drawable
.into(imageViewPlaceholder);
I'm trying to load an image from filesystem into an imageView with the ion library.
When I'm using the following code:
Ion.with(imageView)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.load(uri);
it (sometimes) results in a kind of blurry image.
When not using ion like the following the pictures appear sharply:
imageView.setImageBitmap(BitmapFactory.decodeFile(uri.toString()));
Is there a standard compression coming with ion that I can disable?
Strangely not every picture is blurry when using ion, e.g. I have two identical images, only with another name. When I load them into the imageView with ion one is blurry, one is not.
Any help or tips are appreciated!
Ion tries to load the image to fit the bounds of the ImageView. So make sure your ImageView is sized properly in your layout. Alternatively, if you use adjustViewBounds=true to indicate the ImageView is adjusted by the image contents, it won't do that. Or alternatively smartsize(false).
Try this,
Ion.with(context)
.load(url)
.withBitmap()
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.intoImageView(imageView);
I used picasso as image library for my application. Images were loading a bit slowly so i decided to remove placeholder and it started loading faster.
How come if placeholder is not there its loading faster. But problem is if I remove placeholder. Before loading image it squeezes that area and once image loads it takes proper area filled with image.
My Imageview code below:
<com.wallie.imagview.SquareImageView
android:id="#+id/imageone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/border_image_view"
android:padding="1dp" />
Picasso code i used earlier (loads image a bit slower ):
Picasso.with(context).load(url)
.placeholder(R.drawable.image_loading)
.into(imageView);
Picasso code i used now (loads image faster):
Picasso.with(context).load(url)
.into(imageView);
But problem is it squeezes space for image without placeholder and my spinner for all images get in one place.