基于xUtils3的在listview中加载长图

来源:互联网 发布:淘宝店铺怎么注销关闭 编辑:程序博客网 时间:2024/06/05 05:44

因项目中使用的工具是xUtils3,在listview中加载长图的时候,用的xUtils3的方法API

方法:原理是:得到图片的宽高,然后根据宽高比,计算比例,如果认为是长图,就转成bitmap,进行裁剪,然后展示。这里没有使用BitmapFactory.Options的原因是,通过xUtils3,加载到的图片result,它的方法里得不到它的存放路径,但是可以直接获取到宽高。

/**     * 处理长图     * @param iv 要展示图片的控件     * @param urlpath 要处理图片的Url     */    private void dealWithLongImage(final ImageView iv,final String urlpath){        ImageOptions imageOptions = new ImageOptions.Builder()                .setIgnoreGif(true)//                .setImageScaleType(ImageView.ScaleType.CENTER_CROP)                .setFailureDrawableId(R.mipmap.loadfailed)                .setLoadingDrawableId(R.mipmap.loading)                .setUseMemCache(true)                .build();            x.image().loadDrawable(urlpath, imageOptions, new Callback.CommonCallback<Drawable>() {                @Override                public void onSuccess(Drawable result) {                    try{                        int heigth = result.getIntrinsicHeight();                        int width = result.getIntrinsicWidth();                        LogUtils.i("width---"+width);                        LogUtils.i("heigth---"+heigth);                        //用于模拟崩溃异常//                        int x=1/0;//                        LogUtils.i("----x----"+x);                        if (heigth != 0 && width != 0) {                            if (heigth / width >= 3) {                                //高度远大于宽度                                BitmapDrawable bd = (BitmapDrawable) result;                                Bitmap bm = bd.getBitmap();                                Bitmap mBitmap = Bitmap.createBitmap(bm, 0, 0, width, (int)(width*1.5));                                iv.setImageBitmap(mBitmap);                            }else if(width / heigth >= 3){                                //宽度远大于                                BitmapDrawable bd = (BitmapDrawable) result;                                Bitmap bm = bd.getBitmap();                                Bitmap mBitmap = Bitmap.createBitmap(bm, 0, 0, (int)(heigth*1.5), heigth);                                iv.setImageBitmap(mBitmap);                            } else{                                iv.setImageDrawable(result);//                                BitmapHelper.display(iv, urlpath);                            }                        }else{                            iv.setImageDrawable(result);//                            BitmapHelper.display(iv, urlpath);                        }                    }catch (Exception e) {                        e.printStackTrace();                        BitmapHelper.display(iv, urlpath);                    }                }                @Override                public void onError(Throwable ex, boolean isOnCallback) {                }                @Override                public void onCancelled(CancelledException cex) {                }                @Override                public void onFinished() {                }            });    }

最后,在listview的getItemView中,调用方法dealWithLongImage(iv, urlpath);
注:这里访问网络是异步加载,想在工具类中创建该方法,然后返回处理好的bitmap直接用ImageView加载,是不可行的。所以,需要在使用的当前文件中创建

0 0