Android网络图片异步加载

部分源码如下:

dialog= ProgressDialog.show(this,"","加载数据,请稍等 …",true,true);       
         //图片资源
        String url = "http://www.kzwlg.com.cn:1080/Wlg_server2/"+bundle.getString("pic");        
         //得到可用的图片
        getHttpBitmap(url);
/**
     * 对图片进行大小缩放
     * @param bm
     * @param newWidth
     * @param newHeight
     * @return
     */
    protected Bitmap scaleImg(Bitmap bm, int newWidth, int newHeight) {
        // 图片源
        // Bitmap bm = BitmapFactory.decodeStream(getResources()
        // .openRawResource(id));
        // 获得图片的宽高
        int width = bm.getWidth();
        int height = bm.getHeight();
        // 设置想要的大小
        int newWidth1 = newWidth;
        int newHeight1 = newHeight;
        // 计算缩放比例
        float scaleWidth = ((float) newWidth1) / width;
        float scaleHeight = ((float) newHeight1) / height;
        // 取得想要缩放的matrix参数
        Matrix matrix = new Matrix();
        matrix.postScale(scaleWidth, scaleHeight);
        // 得到新的图片
        Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
          true);
        return newbm;
       }
/**
     * 处理ImageView
     */
    final Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {        
             switch (msg.what) {
            case 1:
                 image.setImageBitmap(scaleImg(bitmap, 300, 300));    
                 dialog.dismiss();  
                break;
            default:
                break;
            }
            
        }
    };
    /**
     * 从网络位置得到图片
     * 采用handler+Thread模式实现多线程异步加载
     * @param url
     * @return
     */
    private void getHttpBitmap( final String url) {
        
        Thread thread=new Thread(){
            public void run() {
                URL myFileURL;            
                
                try{        
                    myFileURL=new URL(url);
                    //获得连接
                    HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection();
                     //设置超时时间为6000毫秒,conn.setConnectionTiem(0);表示没有时间限制
                    conn.setConnectTimeout(6000);
                    //连接设置获得数据流
                    conn.setDoInput(true);
                    //不使用缓存
                    conn.setUseCaches(false);
                    //这句可有可无,没有影响
                    //conn.connect();
                    //得到数据流
                    InputStream is = conn.getInputStream();
                    //解析得到图片
                    bitmap = BitmapFactory.decodeStream(is);
                    //关闭数据流
                    is.close();
                    
                    Message message = handler.obtainMessage();
                    message.what=1;
                    handler.sendMessage(message);
                    }catch(Exception e){
                        e.printStackTrace();
                    }                

            };
        };
        thread.start();
        thread=null;
    }

 

posted @ 2012-08-05 18:07  water0504  阅读(270)  评论(0编辑  收藏  举报