您的位置:首页 > 移动开发 > Android开发

Android中加载长图的策略(微博的那种)

2016-10-28 16:44 1296 查看
Android中加载上图的方法

对于一些需要加载超长图需求时,可能一开始使用使用bitmap或者inputstream转bitmap(或类似加载库)会发现出现图片太大加载不出来的问题。

解决问题的思路可以参考Coding_the_world封装自己的库(然而我太菜了,就在网上找了一些开源库),比较流行的是Subsampling Scale Image View,而且里边封装了缩放功能。

实现思路是:使用glide把图片下载到本地(由于我使用的就是glide图片加载库),然后赋值。具体的操作如下:

1.在gradle文件中添加依赖

compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.5.0'
compile 'com.github.bumptech.glide:glide:3.7.0'


2.在具体代码中的配置

imageView = (SubsamplingScaleImageView) findViewById(R.id.imageView);

imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM);

imageView.setMinScale(1.0F);

finalString testUrl ="http://cache.attach.yuanobao.com/image/2016/10/24/332d6f3e63784695a50b782a38234bb7/da0f06f8358a4c95921c00acfd675b60.jpg";

finalFile downDir = Environment.getExternalStorageDirectory();

//下载图片保存到本地
Glide.with(this)
.load(testUrl)
.asBitmap()
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource,
GlideAnimation<? super Bitmap> glideAnimation) {
File file = new File(downDir, "/da0f06f8358a4c95921c00acfd675b60.jpg");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
FileOutputStream fout = null;
try {
//保存图片
fout = new FileOutputStream(file);
resource.compress(Bitmap.CompressFormat.JPEG, 100, fout);
// 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
imageView.setImage(ImageSource.uri(file.getAbsolutePath()), new ImageViewState(0.5F, new PointF(0, 0), 0));
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
try {
if (fout != null) fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android 图片 长图 加载