有些APP可能為了省事和界面美觀會(huì)把很多內(nèi)容做成一張高清圖片交給移動(dòng)端去加載(我們的項(xiàng)目就是...),如果圖片較小那還OK衍菱,但是如果圖片過(guò)大(我們的有2M還多,MMP)要么不顯示爷狈,要么非常模糊拯辙。那我們應(yīng)該怎么辦呢?用BitmapRegionDecoder將圖片進(jìn)行切分然后再進(jìn)行拼接就可以了企垦。
大圖展示.gif
以本文圖片為例环壤,高是6543,以3000為單位進(jìn)行切分钞诡,那么要生成2個(gè)3000的加上1個(gè)543的bitmap
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)
//用于創(chuàng)建BitmapRegionDecoder郑现,isBm表示輸入流,只有jpeg和png圖片才支持這種方式荧降,
// isShareable如果為true接箫,那BitmapRegionDecoder會(huì)對(duì)輸入流保持一個(gè)表面的引用,
// 如果為false朵诫,那么它將會(huì)創(chuàng)建一個(gè)輸入流的復(fù)制辛友,并且一直使用它。即使為true剪返,程序也有可能會(huì)創(chuàng)建一個(gè)輸入流的深度復(fù)制废累。
// 如果圖片是逐步解碼的,那么為true會(huì)降低圖片的解碼速度脱盲。如果路徑下的圖片不是支持的格式邑滨,那就會(huì)拋出異常
BitmapRegionDecoder decoder = BitmapRegionDecoder.newInstance(isBm, true);
final int imgWidth = decoder.getWidth();
final int imgHeight = decoder.getHeight();
BitmapFactory.Options opts = new BitmapFactory.Options();
//計(jì)算圖片要被切分成幾個(gè)整塊,
// 如果sum=0 說(shuō)明圖片的長(zhǎng)度不足3000px钱反,不進(jìn)行切分 直接添加
// 如果sum>0 先添加整圖驼修,再添加多余的部分殿遂,否則多余的部分不足3000時(shí)底部會(huì)有空白
int sum = imgHeight/3000;
int redundant = imgHeight%3000;
List<Bitmap> bitmapList = new ArrayList<>();
//說(shuō)明圖片的長(zhǎng)度 < 3000
if (sum == 0){
//直接加載
bitmapList.add(resource);
}else {
//說(shuō)明需要切分圖片
for (int i = 0; i < sum; i++) {
//需要注意:mRect.set(left, top, right, bottom)的第四個(gè)參數(shù),
//也就是圖片的高不能大于這里的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取出來(lái)拼接成一個(gè)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)的第四個(gè)參數(shù)乙各, 不能大于4096墨礁,最后盡量把圖片壓縮下