您的位置:首页 > 其它

ImageView在开启硬件加速时加载长图

2015-09-16 19:27 260 查看
本文参考文章http://tieba.baidu.com/p/3064414697

Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
HashMap<Integer , Bitmap> mMap = (HashMap<Integer , Bitmap>)msg.obj;
if(mMap !=null){
try{
mOtherRouteContainer.removeAllViews(); //先移除容器内的全部子view
for(int i = 0 ; i < mMap.size() ; i ++){
ScaleImageView imageView = new ScaleImageView(GroupProductDetailActivity.this);
float width = mMap.get(i).getWidth();

scale = (AppConfig.getScreenWidth() - ExtendUtils.dip2px(GroupProductDetailActivity.this, 20)) / width;
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT,(int)(mMap.get(i).getHeight()* scale));
imageView.zoomTo(scale);
imageView.setLayoutParams(params);
imageView.setImageBitmap(mMap.get(i));
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) { //imageview点击事件
if (mjourney != null) {
Intent route_img_intent = new Intent(GroupProductDetailActivity.this, RouteBrowsePictureActivity.class);
route_img_intent.putExtra(GlobalConstant.IntentConstant.ROUTE_IMG_URL, mjourney.image);
route_img_intent.putExtra(GlobalConstant.IntentConstant.ROUTE_IMG_DOWNLOAD_URL, mjourney.pdfPath);
startActivity(route_img_intent);
}
}
});
mOtherRouteContainer.addView(imageView);
}
}catch(Exception e){
}
}

}
};
/**
* 新线程切割bitmap运算
*/
public class calThread extends Thread{
private Bitmap bitmap;
public calThread(Bitmap bitmap){
this.bitmap = bitmap;
}
@Override
public void run() {
super.run();
deviderBitmapToShow(bitmap);
}
}

/**
* bitmap 转换成 InputStream
* @param bm
* @return
*/
private InputStream  Bitmap2IS(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
InputStream sbs = new ByteArrayInputStream(baos.toByteArray());
return sbs;
}

/**
* 将原bitmap切割显示
* @param bitmap
*/
private void deviderBitmapToShow(Bitmap bitmap){
if(bitmap == null || bitmap.getHeight()<=0 || bitmap.getWidth()<=0){
return;
}
int num = 0 ; //要分割的份数
HashMap<Integer , Bitmap> mMap = new HashMap<>(); //保存bitmap的 hashmap
Bitmap mbitmap = null;   //切割后的bitmap
Rect rect = null;
BitmapRegionDecoder bitmapRegionDecoder = null;
int height = bitmap.getHeight();       //bitmap的高度和寬度
num = height / 4000;
if(height % 4000 > 0){   //如果height对4000取余未除尽,则n加1
num++;
}
try{
bitmapRegionDecoder = BitmapRegionDecoder.newInstance(Bitmap2IS(bitmap), false);
for(int i = 0 ; i < num ; i ++){
if( num - i == 1 ){
rect = new Rect(0,4000 * i,bitmap.getWidth() ,height);
}else{
rect = new Rect(0,4000 * i,bitmap.getWidth() ,4000 * (i+1));
}
mbitmap = bitmapRegionDecoder.decodeRegion(rect,null);
mMap.put(i, mbitmap);
}
Message msg = Message.obtain();
msg.obj = mMap;
handler.sendMessage(msg);
}catch (Exception e){
return;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: