XUtils3 添加drawable支持

由于XUtils众所周知的缺陷,项目急需将原有的依赖项目XUtils更新为XUtils3。

XUtils3的架构与之前的版本完全不同,但是依旧没有支持drawable。

XUtils添加drawable支持请戳这里

有了之前的经验,XUtils3改造起来就简单了。

下载XUtils3的源码,全局搜索"assets",找到这个位置

//org.xutils.http.request.UriRequestFactory
    public static UriRequest getUriRequest(RequestParams params, Type loadType) throws Throwable {

        // get scheme
        String scheme = null;
        String uri = params.getUri();
        int index = uri.indexOf(":");
        if (index > 0) {
            scheme = uri.substring(0, index);
        } else if (uri.startsWith("/")) {
            scheme = "file";
        }

        // get UriRequest
        if (!TextUtils.isEmpty(scheme)) {
            Class<? extends UriRequest> cls = SCHEME_CLS_MAP.get(scheme);
            if (cls != null) {
                Constructor<? extends UriRequest> constructor
                        = cls.getConstructor(RequestParams.class, Class.class);
                return constructor.newInstance(params, loadType);
            } else {
                if (scheme.startsWith("http")) {
                    return new HttpRequest(params, loadType);
                } else if (scheme.equals("assets")) {
                    return new AssetsRequest(params, loadType);
                } else if (scheme.equals("file")) {
                    return new LocalFileRequest(params, loadType);
                }  else {
                    throw new IllegalArgumentException("The url not be support: " + uri);
                }
            }
        } else {
            throw new IllegalArgumentException("The url not be support: " + uri);
        }
    }

这里可以看到,XUtils3与xUtils架构已经不同。

找到AssetsRequest的关键代码

//org.xutils.http.request.AssetsRequest
    @Override
    public InputStream getInputStream() throws IOException {
        if (inputStream == null) {
            if (callingClassLoader != null) {
                String assetsPath = "assets/" + queryUrl.substring("assets://".length());
                inputStream = callingClassLoader.getResourceAsStream(assetsPath);
                contentLength = inputStream.available();
            }
        }
        return inputStream;
    }

使用ClassLoader.getResourceAsStream()读取本地资源文件,这貌似不适用drawable中的资源,我们需要的是上下文,xUtils3控制中心已经为我们准备好了。

x.app()可以获得应用实例,老方法Context.getResources().openRawResource()读取资源

我们是代码的搬运工。复制AssetsRequest生成DrawableRequest,改造getInputStream()方法

//org.xutils.http.request.DrawableRequest
    @Override
    public InputStream getInputStream() throws IOException {
        if (inputStream == null) {
            if (callingClassLoader != null) {
                String drawableIdString = queryUrl.substring(11, queryUrl.length());
                try {
                    int drawableId = Integer.parseInt(drawableIdString);
                    inputStream = x.app().getResources().openRawResource(drawableId);
                    contentLength = inputStream.available();
                }catch (Exception e){
                    e.printStackTrace();
                }


            }
        }
        return inputStream;
    }

改造UriRequestFactory的getUriRequest方法,加入构建DrawableRequest的代码

//org.xutils.http.request.UriRequestFactory
    public static UriRequest getUriRequest(RequestParams params, Type loadType) throws Throwable {

        // get scheme
        String scheme = null;
        String uri = params.getUri();
        int index = uri.indexOf(":");
        if (index > 0) {
            scheme = uri.substring(0, index);
        } else if (uri.startsWith("/")) {
            scheme = "file";
        }

        // get UriRequest
        if (!TextUtils.isEmpty(scheme)) {
            Class<? extends UriRequest> cls = SCHEME_CLS_MAP.get(scheme);
            if (cls != null) {
                Constructor<? extends UriRequest> constructor
                        = cls.getConstructor(RequestParams.class, Class.class);
                return constructor.newInstance(params, loadType);
            } else {
                if (scheme.startsWith("http")) {
                    return new HttpRequest(params, loadType);
                } else if (scheme.equals("assets")) {
                    return new AssetsRequest(params, loadType);
                } else if (scheme.equals("file")) {
                    return new LocalFileRequest(params, loadType);
                } else if (scheme.equals("drawable")) {
                    return new DrawableRequest(params, loadType);
                } else {
                    throw new IllegalArgumentException("The url not be support: " + uri);
                }
            }
        } else {
            throw new IllegalArgumentException("The url not be support: " + uri);
        }
    }

至此,改造完成。使用方法与xutils相同。

String uri = "drawable://" +R.drawable.super_larger_logo

x.image().bind(largePic_imgV, uri);

附改造后的xUtils3源码http://download.csdn.net/detail/atm008/9477902

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值