/*
显示长图片,但是要小心OOM,目前我的图片为5.32MB可以正常使用.
如果出现OOM,可以尝试使用ListView
也可以使用webView,但是webView体验不如本地原生.
*/
public void setLongPicture(String assetsLocation){
try {
//获得图片的宽、高
BitmapFactory.Options tmpOptions =new BitmapFactory.Options();
tmpOptions.inJustDecodeBounds =true;
BitmapFactory.decodeStream(getAssets().open(assetsLocation), null, tmpOptions);
int width =tmpOptions.outWidth;
int height =tmpOptions.outHeight;
//设置显示图片的中心区域
BitmapRegionDecoder bitmapRegionDecoder =BitmapRegionDecoder.newInstance(getAssets().open(assetsLocation), false);
BitmapFactory.Options options =new BitmapFactory.Options();
options.inPreferredConfig =Bitmap.Config.ARGB_8888;
int beforeHeight;
int currentHeight =0;
int singleIncrease =4096;
Bitmap bitmap;
ImageView imageView;
while (true){
beforeHeight =currentHeight;
currentHeight+=singleIncrease;
if (currentHeight
bitmap =bitmapRegionDecoder.decodeRegion(new Rect(0, beforeHeight, width, currentHeight), options);
imageView =new ImageView(this);
imageView.setImageBitmap(bitmap);
mLl.addView(imageView);
}else{
bitmap =bitmapRegionDecoder.decodeRegion(new Rect(0, beforeHeight, width, height), options);
imageView =new ImageView(this);
imageView.setImageBitmap(bitmap);
mLl.addView(imageView);
return;
}
}
}catch (IOException e) {
e.printStackTrace();
}
}
网友评论