如何避免列表视图中的图像闪烁 [英] How to avoid image flickering in a listview
问题描述
我有一个listivew,可以显示一堆图像.正在使用通用图像加载器将此图像从文件加载到图像视图.
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.
为此,我尝试将以下内容设置为我的imageview
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"/>
此方法的问题是,滚动列表视图时会有很多闪烁,因为图像视图的高度事先未知,因此必须首先使用我的宽度缩放图像以计算相对于图像的每个图像高度这是长宽比.
我如何预先计算每个图像的高度,而不是让imageview处理呢?
How can i calculate each image height in advance instead of letting imageview handle it?
如果我的图像尺寸为400 X 700,并且我希望imageview的宽度为300px,如何使用图像尺寸计算imageview的高度并保持图像的长宽比?这样可以避免在滚动列表视图时闪烁.
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.
推荐答案
经过数小时的研究,我能够知道可用于在保持图像纵横比的同时计算新的imageview高度的方法.
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.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//Returns null, sizes are in the options variable
BitmapFactory.decodeFile("/sdcard/image.png", options);
int width = options.outWidth;
int height = options.outHeight;
//calculating image aspect ratio
float ratio =(float) height/(float) width;
//calculating my image height since i want it to be 360px wide
int newHeight = Math.round(ratio*360);
//setting the new dimentions
imageview.getLayoutParams().width = 360;
imageview.getLayoutParams().height = newHeight;
//i'm using universal image loader to display image
imaheview.post(new Runnable(){
ImageLoader.getInstance().displayImage(imageuri,imageview,displayoptions);
});
这篇关于如何避免列表视图中的图像闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!