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

来源:互联网 发布:最好的php 分销系统 编辑:程序博客网 时间:2024/06/01 10:44

本文参考文章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;        }    }
0 0
原创粉丝点击