I have a listivew that display a bunch of images. am using Universal Image Loader to load this images from files to imageviews.
This images have different dimensions and i want all of them to have same width but different height in respect to each image aspect ratio.
To achieve this, i have tried setting the following to my imageview
<ImageView
android:layout_width = "400dp"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
The issue with this method is that there is a lot of flickering when one scrolls the listview since imageview height is not known in advance and images have to be scaled first using my width to calculate each image height in respect to it's aspect ratio.
How can i calculate each image height in advance instead of letting imageview handle it?
if i have an image which is 400 X 700, and i want the imageview to be 300px wide, how can i calculate imageview's height using my image dimension and maintain image aspect ratio? this can help avoid flickering wnen one scroll the listview.
The reason for this flicker is that, in listview list items are reused. When re-used, the imageviews in the list item retains the old image reference which is displayed first. Later on once new image is downloaded, it starts to show. this causes the flickering behavior. To avoid this flickering issue, always clear the old image reference from the imageview when it is getting reused.
In your case, add holder.image.setImageBitmap(null); after holder = (ViewHolder) convertView.getTag();
So, your getView() method will look like:
my suggestion is to use grid view to avoid flickering of images it will load at first time if it is same url , it will load from cache
How I solved it was by creating a
Bitmap[]
array variable to store images, then in adapter'sgetView()
, I used position to check if image inBitmap[]
array isnull
or has value. If it is has value, then I use the value instead of calling thenew DownloadImageTask()
construct again.For example:
YourCustomArrayAdapter.java
Then, in your image downloader construct, after downloading the image, make an insertion into the
Bitmap[]
image array for that position. For example:YourImageDownloaderClass.java
After hours of research, i was able to know the method that i can use to calculate new imageview height while maintaining image aspect ratio.
You can do something like this :