热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

基于xUtils3的在listview中加载长图

因项目中使用的工具是xUtils3,在listview中加载长图的时候,用的xUtils3的方法API方法:原理是:得到图片的宽高,然后根据宽高比,计算比例,如果认为是长图,就转成bitma

因项目中使用的工具是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() {

@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加载,是不可行的。所以,需要在使用的当前文件中创建


推荐阅读
author-avatar
水儿本人_164
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有