2011-07-02 32 views
0

你好這裏是代碼:drawBitmap時間過長

 URL uri = new URL(photoUrl); 
      URLConnection connection = uri.openConnection(); 
      Log.i(TAG, "connecting..."); 
      connection.connect(); 
      Log.i(TAG, "connected"); 
      Log.i(TAG, "building Bitmap..."); 

      InputStream is = connection.getInputStream(); 
      //BufferedInputStream bis = new BufferedInputStream(is, 8 * 1024); 

      File myfile = new File(getApplicationContext().getCacheDir(), "wallpaper.tmp"); 
      myfile.createNewFile(); 
      BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(myfile)); 

      byte buf[]=new byte[1024]; 
      int len; 
      while((len=is.read(buf))>0) 
      out.write(buf,0,len); 
      out.close(); 
      is.close(); 

      Bitmap bmp = BitmapFactory.decodeFile(myfile.getPath()); 
      //Bitmap bmp = BitmapFactory.decodeStream(bis); 

      Log.i(TAG, "builded Bitmap");    
      Log.i(TAG, "showing bitmap..."); 


      //int scale; 
      Matrix matrix = new Matrix(); 
      matrix.setScale(0.1F, 0.1F); 
      //if (bmp.getWidth() < bmp.getHeight()){ 
      // scale = canvas.getWidth()/bmp.getWidth(); 
      //}else{ 
      // scale = canvas.getHeight()/bmp.getHeight(); 
      //} 
      //matrix.postScale(scale, scale, bmp.getWidth(), bmp.getHeight()); 
      //matrix.postScale(0.5F, canvas.getWidth()/bmp.getWidth()); 

      //Bitmap bmp2 = Bitmap.createScaledBitmap(bmp, canvas.getWidth(), canvas.getHeight(), true); 

      //Paint p = new Paint(); 
      //p.setFilterBitmap(true); 


      //try{ 
      canvas.drawBitmap(bmp, matrix, null); 
      //}catch(NullPointerException exc){ 
      // //why do we get this?? 
      // Log.d(TAG, "NullPointerException drawing canvas. why?"); 
      // return; 
      //} 

現在發生的事情是drawBitmap是因爲5分鐘... 任何想法阻止?

回答

1

您可能會首先創建一個縮放的位圖以獲得更好的性能。

Bitmap bmp = BitmapFactory.decodeFile(myfile.getPath()); 
bmp = bmp.createScaledBitmap(bmp, width, height, true); 

然後,當您將其繪製到屏幕上時,您不必使用矩陣。

+0

謝謝,我用'canvas.drawBitmap(bmp,0,0,null)繪製位圖;'但我只是不顯示... – nomoral

+0

你想要做什麼?繪製位圖並將其顯示在屏幕上?如果是這樣,請查看android sdk附帶的lunarlander示例應用程序。 – Emiam