我有一张能显示很多图片的图片。我正在使用通用图像加载器将这些图像从文件加载到图像视图。
这张图片有不同的尺寸,我希望它们都有相同的宽度,但是对于每个图像的纵横比,有不同的高度。
为了实现这一点,我尝试将以下设置设置为我的图像视图
<ImageView
android:layout_width = "400dp"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"/>
这种方法的问题是,当一个人滚动列表视图时,会有很多闪烁,因为图像视图的高度是事先不知道的,并且必须首先使用我的宽度来计算每幅图像相对于其高宽比的图像高度。
如何预先计算每个图像的高度,而不是让图像视图处理它呢?
如果我有一个400 x 700的图像,并且我希望图像视图宽300 be,我如何用我的图像维数来计算图像视图的高度,并保持图像的纵横比?这可以帮助避免闪烁一个滚动列表视图。
发布于 2016-03-15 23:38:04
经过几个小时的研究,我知道了在保持图像高宽比的同时,我可以用来计算新的图像视图高度的方法。
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);
});
发布于 2016-03-15 06:13:41
此闪烁的原因是,在列表视图中,列表项被重用。当重复使用时,列表项中的图像视图保留第一次显示的旧图像引用。稍后,一旦下载新的图像,它将开始显示。这会导致闪烁行为。为了避免这个闪烁的问题,总是清除旧的图像引用从图像视图时,它被重用。
在您的示例中,将holder.image.setImageBitmap(null);添加到holder = (ViewHolder) convertView.getTag();之后
因此,您的getView()方法如下所示:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
...
if (convertView == null) {
LayoutInflater inflater = getLayoutInflater();
convertView = inflater.inflate(viewResourceId, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.image.setImageBitmap(null)
}
...
return convertView;
}
发布于 2016-03-15 07:11:02
你可以这样做:
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;
//If you want, the MIME type will also be decoded (if possible)
String type = options.outMimeType;
https://stackoverflow.com/questions/36013709
复制