壁画:在gridview中显示错误的图像

时间:2017-02-02 12:30:26

标签: android baseadapter fresco

我正在使用gridview。在网格中,每个项目都包含一个simpledraweeview,当我在第一个位置添加一个新项目并在那时通知适配器第一个和最后一个具有相同图像的项目。 (只发生在棒棒糖下面)

XML SimpleDraweeView:

<com.facebook.drawee.view.SimpleDraweeView
                android:id="@+id/id_imageview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
               android:layout_gravity="center|center_horizontal|center_vertical"
                android:adjustViewBounds="true"
                android:contentDescription="@string/COM_NOTEBOOK_NOTEBOOK"
                android:scaleType="centerInside"
                app:imageUrl="@{url}" />

图片加载代码:

public void justLoadImage(final String mPath, final SimpleDraweeView mImageView, int width, int height) {
    try {

        if (TextUtils.isEmpty(mPath) || mImageView == null) {
            return;
        }

        if (!isBitmapAvailableInMemCache(mPath)) {
            GenericDraweeHierarchy hierarchy = setHierarchyForDraweeView(mImageView, 150);
            hierarchy.setPlaceholderImage(mAsyncColorDrawable);
            hierarchy.setFailureImage(mContext.getResources().getDrawable(R.drawable.broken_image_black));
        }

        ImageRequest request;
        if (mPath.endsWith(".jpg")) {//No i18n
            request = ImageRequestBuilder.newBuilderWithSource(Uri.fromFile(new File(mPath)))
                    .setLocalThumbnailPreviewsEnabled(true)
                    .setResizeOptions(new ResizeOptions(width, height))
                    .build();
        } else {
            request = ImageRequestBuilder.newBuilderWithSource(Uri.fromFile(new File(mPath)))
                    .setLocalThumbnailPreviewsEnabled(true)
                    .build();
        }

        DraweeController draweeController = Fresco.newDraweeControllerBuilder()
                .setControllerListener(new ControllerListener<ImageInfo>() {
                    @Override
                    public void onSubmit(String id, Object callerContext) {

                    }

                    @Override
                    public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable animatable) {
                    }

                    @Override
                    public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {

                    }

                    @Override
                    public void onIntermediateImageFailed(String id, Throwable throwable) {

                    }

                    @Override
                    public void onFailure(String id, Throwable throwable) {
                        clearFerscoCache(mPath);
                        mImageView.setImageURI(Uri.fromFile(new File(mPath)));
                    }

                    @Override
                    public void onRelease(String id) {

                    }
                })
                .setOldController(mImageView.getController())
                .setImageRequest(request)
                .setAutoPlayAnimations(false)
                .build();
        mImageView.setController(draweeController);
    } catch (OutOfMemoryError | Exception e) {
        e.printStackTrace();
    }
}

SetHierarchyForDraweeView函数

 private GenericDraweeHierarchy setHierarchyForDraweeView(SimpleDraweeView draweeView, int duration) {
    if (draweeView != null) {
        if (draweeView.getHierarchy() == null) {
            GenericDraweeHierarchyBuilder builder = new GenericDraweeHierarchyBuilder(mContext.getResources());
            GenericDraweeHierarchy hierarchy = builder
                    .setFadeDuration(duration)
                    .setPlaceholderImage(new AsyncColorDrawable(mContext.getResources()))
                    .setFailureImage(mContext.getResources().getDrawable(R.drawable.broken_image_black))
                    .build();
            draweeView.setHierarchy(hierarchy);
        } else {
            GenericDraweeHierarchy hierarchy = draweeView.getHierarchy();
            hierarchy.setFadeDuration(duration);
            return hierarchy;
        }
    }
    return null;
}

1 个答案:

答案 0 :(得分:0)

如果您在ViewHoler适配器中使用GridView模式,则在justLoadImage方法的下面行中的if检查可能会导致此类情况。< / p>

if (TextUtils.isEmpty(mPath) || mImageView == null) {
            return;
}

因此,如果imagePath为null,您只需尝试删除此行并让Fresco处理此情况或手动将空图像设置为ViewHolder中的SimpleDraweeView

修改

您可以查看日志。在justLoadImage方法中获取例外也可能导致这种情况。