Android/通用图像加载器:不显示图像

Android/通用图像加载器:不显示图像,android,universal-image-loader,Android,Universal Image Loader,我使用通用图像加载器(UIL)在列表视图中缓存和显示图像 对于列表视图的项目,我无法使用以下布局显示图像 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width=

我使用通用图像加载器(UIL)在列表视图中缓存和显示图像

对于列表视图的项目,我无法使用以下布局显示图像

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
    >

        <ImageView
            android:id="@+id/image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        />

        <TextView
            android:id="@+id/title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
        />
</LinearLayout>

我想在图像描述上方显示图像。图像的宽度是设备的全宽,图像的高度根据图像的宽度自动缩放

使用宽度和高度的“包裹内容”,我可以在其他不使用UIL的项目中显示我的图像。我不确定我在UIL的配置中做错了什么(我在他们Github的示例中使用了相同的配置),但是图像没有使用上面的布局进行dsiplayed。实际上,它们以0高度显示。如果我提供一个固定的高度,比如

    <ImageView
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="300dp"
    />


如果您有任何解决方案,请告诉我。

我成功地根据位图的尺寸手动更新了图像视图的宽度和高度。在位图附加到图像视图后(onLoadingComplete),图像的尺寸也会重新计算,以适应设备旋转时新屏幕的大小

如果您有任何改进此解决方案的建议,如果您有任何其他解决方案,请发表评论。谢谢

private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

    static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        if (loadedImage != null) {
            ImageView imageView = (ImageView) view;
            boolean firstDisplay = !displayedImages.contains(imageUri);
            if (firstDisplay) {
                FadeInBitmapDisplayer.animate(imageView, 500);
                displayedImages.add(imageUri);
            }

            // Screen's width will be the width of the image.
            float newWidth = Resources.getSystem().getDisplayMetrics().widthPixels;

            // Get the width and the height of the image.
            int oldWidth = imageView.getDrawable().getIntrinsicWidth();
            int oldHeight = imageView.getDrawable().getIntrinsicHeight();

            // Calculate the new height of the image based on the screen's width.
            float newHeight = newWidth * oldHeight / oldWidth;

            // Apply the new width and height values to ImageView.
            imageView.getLayoutParams().width = (int) newWidth;
            imageView.getLayoutParams().height = (int) newHeight;
        }
    }
}
私有静态类AnimateFirstDisplayListener扩展了SimpleImageLoadingListener{
静态最终列表displayedImage=Collections.synchronizedList(新LinkedList());
@凌驾
public void onLoadingComplete(字符串imageUri、视图视图、位图加载图像){
如果(LoadeImage!=null){
ImageView ImageView=(ImageView)视图;
布尔值firstDisplay=!displayedImages.contains(imageUri);
如果(第一次显示){
动画(imageView,500);
displayedImages.add(imageUri);
}
//屏幕的宽度将是图像的宽度。
float newWidth=Resources.getSystem().getDisplayMetrics().widthPixels;
//获取图像的宽度和高度。
int oldWidth=imageView.getDrawable().getIntrinsicWidth();
int oldHeight=imageView.getDrawable().getIntrinsicHeight();
//根据屏幕宽度计算图像的新高度。
浮动新高度=新宽度*旧高度/旧宽度;
//将新的宽度和高度值应用于ImageView。
imageView.getLayoutParams().width=(int)newWidth;
imageView.getLayoutParams().height=(int)newHeight;
}
}
}

我已经解决了这个问题,试着这样做:

 ImageLoader.getInstance().displayImage(mImageUrl, mImageView, PhotoUtils.avatarImageOptions,new SimpleImageLoadingListener() {
        @Override
        public void onLoadingStarted(String imageUri, View view) {
            progressBar.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
            String message = null;
            switch (failReason.getType()) {
                case IO_ERROR:

                    break;
                case DECODING_ERROR:

                    break;
                case NETWORK_DENIED:

                    break;
                case OUT_OF_MEMORY:

                    break;
                case UNKNOWN:

                    break;
            }
            Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            progressBar.setVisibility(View.GONE);
            mAttacher.update();
        }
    });