欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android Weex加载图片

程序员文章站 2024-03-16 10:41:10
...

Weex页面写好的图片,在android上面是展示不出来的,需要我们自己去处理一下才可以展示出来,接下来的代码希望对各位大佬有用。

Android Weex加载图片

当我们想加载weex页面里面的图片的时候,官网也给出了解决办法,那就是继承

IWXImgLoaderAdapter,然后我们就会重写
setImage方法,我们就只需要在这个方法里面写实现显示图片的处理就行,就是这个样子

public class ImageAdaoterWithFresco implements IWXImgLoaderAdapter { 
@Override 
public void setImage(String url, ImageView view, WXImageQuality quality, WXImageStrategy strategy) { 
//实现你自己的图片下载,否则图片是无法显示的。 
}

这里需要注意的就是你自己写的

ImageAdapterWithFresco

类,你需要去注册一下,还是再Android Weex加载图片

这个类里面注册的,而这个类上次也说过了,是官方文档给的,你就直接复制下来用就行,

Android Weex加载图片

然后加到这个里面就行了,这个类也是需要注册的,怎么注册上次也说了,这里就不说了(官网也有)

Android Weex加载图片

我接下来把我这边写好的代码拿过来,你们需要用的时候直接就可以用,

首先加上fresco的依赖

compile 'com.facebook.fresco:fresco:0.12.0+'
compile 'com.facebook.fresco:animated-gif:0.12.0'

然后就是一坨代码

public class ImageAdapterWithFresco implements IWXImgLoaderAdapter {
    private static boolean hasInitPipeline = false;

    public ImageAdapterWithFresco() {
    }

    public void setImage(final String url, final ImageView view, WXImageQuality quality, WXImageStrategy strategy) {
        WXSDKManager.getInstance().postOnUiThread(new Runnable() {
            public void run() {
                if(view != null && view.getLayoutParams() != null) {
                    if(TextUtils.isEmpty(url)) {
                        view.setImageBitmap((Bitmap)null);
                    } else {
                        String temp = url;
                        if(url.startsWith("//")) {
                            temp = "http:" + url;
                        }

                        if(view.getLayoutParams().width > 0 && view.getLayoutParams().height > 0) {
                            Uri uri = Uri.parse(temp);
                            ImageDecodeOptions decodeOptions = ImageDecodeOptions.newBuilder().setBackgroundColor(-16711936).build();
                            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri).setImageDecodeOptions(decodeOptions).setAutoRotateEnabled(true).setLocalThumbnailPreviewsEnabled(true).setLowestPermittedRequestLevel(ImageRequest.RequestLevel.FULL_FETCH).setProgressiveRenderingEnabled(false).build();
                            if(view instanceof DraweeView) {
                                Log.d("FrescoImageAdapter", "load: " + url);
                                ControllerListener controllerListener = new BaseControllerListener<ImageInfo>() {
                                    public void onFinalImageSet(String id, @Nullable ImageInfo imageInfo, @Nullable Animatable anim) {
                                        if(imageInfo != null) {
                                            QualityInfo qualityInfo = imageInfo.getQualityInfo();
                                            FLog.d("Final image received! Size %d x %d", "Quality level %d, good enough: %s, full quality: %s", new Object[]{Integer.valueOf(imageInfo.getWidth()), Integer.valueOf(imageInfo.getHeight()), Integer.valueOf(qualityInfo.getQuality()), Boolean.valueOf(qualityInfo.isOfGoodEnoughQuality()), Boolean.valueOf(qualityInfo.isOfFullQuality())});
                                        }
                                    }

                                    public void onIntermediateImageSet(String id, @Nullable ImageInfo imageInfo) {
                                        FLog.d("", "Intermediate image received");
                                    }

                                    public void onFailure(String id, Throwable throwable) {
                                        FLog.e(this.getClass(), throwable, "Error loading %s", new Object[]{id});
                                    }
                                };
                                DraweeController controller = ((PipelineDraweeControllerBuilder)((PipelineDraweeControllerBuilder)((PipelineDraweeControllerBuilder)Fresco.newDraweeControllerBuilder().setAutoPlayAnimations(true)).setControllerListener(controllerListener)).setUri(uri).setImageRequest(request)).build();
                                ((DraweeView)view).setController(controller);
                            } else {
                                if (!hasInitPipeline) {
                                    hasInitPipeline = true;
                                    ImagePipelineFactory.initialize(WXEnvironment.getApplication());
                                }
                                ImagePipeline imagePipeline = Fresco.getImagePipeline();
                                DataSource<CloseableReference<CloseableImage>> dataSource = imagePipeline.fetchDecodedImage(request, new Object());
                                DataSubscriber dataSubscriber = new BaseDataSubscriber<CloseableReference<CloseableImage>>() {
                                    public void onNewResultImpl(DataSource<CloseableReference<CloseableImage>> dataSource) {
                                        CloseableReference<CloseableImage> imageReference = (CloseableReference)dataSource.getResult();
                                        if(imageReference != null) {
                                            try {
                                                Preconditions.checkState(CloseableReference.isValid(imageReference));
                                                CloseableImage closeableImage = (CloseableImage)imageReference.get();
                                                if(!(closeableImage instanceof CloseableStaticBitmap)) {
                                                    throw new UnsupportedOperationException("Unrecognized image class: " + closeableImage);
                                                }

                                                CloseableStaticBitmap closeableStaticBitmap = (CloseableStaticBitmap)closeableImage;
                                                view.setImageBitmap(closeableStaticBitmap.getUnderlyingBitmap());
                                            } finally {
                                                imageReference.close();
                                            }
                                        }

                                    }

                                    public void onFailureImpl(DataSource dataSource) {
                                    }
                                };
                                dataSource.subscribe(dataSubscriber, UiThreadImmediateExecutorService.getInstance());
                            }

                        }
                    }
                }
            }
        }, 0L);
    }
}

拿去用就行,

其实这个代码也不是我写的,当时本来想着自己写一个,然后呢,我公司有一个大佬弄过,所以就直接给我了,我就拿着用了!

Android Weex加载图片

然后在这里记录一下,希望不被他看到!!!

这里需要注意的是你的那个weex页面的大小,可能也会影响你的图片加载,我这边遇到过一个问题,加载图片其它都可以就只有一个页面的一张图片加载不出来,那个图片是600K,而且那个页面有1M多,块2M了,然后我们把600K的图片压缩了一下,成了200K,然后就可以正常显示了,这个原因不详,可能与页面大小有关也有可能与图片的大小有关,所以显示不出现图片的话,就先用一个小点的图片试试,可以的话在压缩你想要的图片再试试,前提是你自己写的ImageAdapter没问题!!!