Android 一个Imageview加载长图

有些APP可能为了省事和界面美观会把不少内容作成一张高清图片交给移动端去加载(咱们的项目就是...),若是图片较小那还OK,可是若是图片过大(咱们的有2M还多,MMP)要么不显示,要么很是模糊。那咱们应该怎么办呢?用BitmapRegionDecoder将图片进行切分而后再进行拼接就能够了。android

 

大图展现.gifcanvas

以本文图片为例,高是6543,以3000为单位进行切分,那么要生成2个3000的加上1个543的bitmapide

String url = "http://bmob-cdn-15177.b0.upaiyun.com/2018/08/23/8fa7f1c2404bafbd808bde10ff072ceb.jpg";
Glide.with(this).load(url)
                .asBitmap()
                .into(new SimpleTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                        setBitmapToImg(resource);
                    }
                });

private void setBitmapToImg(Bitmap resource) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            resource.compress(Bitmap.CompressFormat.PNG, 100, baos);

            InputStream isBm = new ByteArrayInputStream(baos.toByteArray());

            //BitmapRegionDecoder newInstance(InputStream is, boolean isShareable)
            //用于建立BitmapRegionDecoder,isBm表示输入流,只有jpeg和png图片才支持这种方式,
            // isShareable若是为true,那BitmapRegionDecoder会对输入流保持一个表面的引用,
            // 若是为false,那么它将会建立一个输入流的复制,而且一直使用它。即便为true,程序也有可能会建立一个输入流的深度复制。
            // 若是图片是逐步解码的,那么为true会下降图片的解码速度。若是路径下的图片不是支持的格式,那就会抛出异常
            BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(isBm, true);

            final int imgWidth = decoder.getWidth();
            final int imgHeight = decoder.getHeight();

            BitmapFactory.Options opts = new BitmapFactory.Options();

            //计算图片要被切分红几个整块,
            // 若是sum=0 说明图片的长度不足3000px,不进行切分 直接添加
            // 若是sum>0 先添加整图,再添加多余的部分,不然多余的部分不足3000时底部会有空白
            int sum = imgHeight/3000;

            int redundant = imgHeight%3000;

            List<Bitmap> bitmapList = new ArrayList<>();

            //说明图片的长度 < 3000
            if (sum == 0){
                //直接加载
                bitmapList.add(resource);
            }else {
                //说明须要切分图片
                for (int i = 0; i < sum; i++) {
                    //须要注意:mRect.set(left, top, right, bottom)的第四个参数,
                    //也就是图片的高不能大于这里的4096
                    mRect.set(0, i*3000, imgWidth, (i+1) * 3000);
                    Bitmap bm = decoder.decodeRegion(mRect, opts);
                    bitmapList.add(bm);
                }

                //将多余的不足3000的部分做为尾部拼接
                if (redundant > 0){
                    mRect.set(0, sum*3000, imgWidth, imgHeight);
                    Bitmap bm = decoder.decodeRegion(mRect, opts);
                    bitmapList.add(bm);
                }

            }

            Bitmap bigbitmap = Bitmap.createBitmap(imgWidth, imgHeight, Bitmap.Config.ARGB_8888);
            Canvas bigcanvas = new Canvas(bigbitmap);

            Paint paint = new Paint();
            int iHeight = 0;

            //将以前的bitmap取出来拼接成一个bitmap
            for (int i = 0; i < bitmapList.size(); i++) {
                Bitmap bmp = bitmapList.get(i);
                bigcanvas.drawBitmap(bmp, 0, iHeight, paint);
                iHeight += bmp.getHeight();

                bmp.recycle();
                bmp = null;
            }

            mImageView1.setImageBitmap(bigbitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

布局文件布局

<ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">
            <ImageView
                android:id="@+id/iv_big"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:scaleType="fitXY"/>
</ScrollView>

须要注意:mRect.set(left, top, right, bottom)的第四个参数, 不能大于4096,最后尽可能把图片压缩下this



做者:iot_xc
连接:https://www.jianshu.com/p/ef44310cfe72
来源:简书
著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。url

相关文章
相关标签/搜索