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

来源:互联网 发布:语c kg是什么意思网络 编辑:程序博客网 时间:2024/05/29 13:03

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();                }            }        }    });
0 0