2011-12-27 43 views
0

這裏是我的自定義字段的paint方法:黑莓:在自定義字段你下載過圖片減緩VerticalFieldManager

protected void paint(Graphics graphics) { 

      new downloadImage(); 
      Bitmap img = downloadImage.connectServerForImage(this.poster); 


      graphics.drawBitmap(0, 0, 100, 150, img, LEFT, TOP); 
    } 

這裏是connectServerForImage方法:

public static Bitmap connectServerForImage(String url) { 

      HttpConnection httpConnection = null; 
      DataOutputStream httpDataOutput = null; 
      InputStream httpInput = null; 
      int rc; 

      Bitmap bitmp = null; 
      try { 
      httpConnection = (HttpConnection) Connector.open(url); 
      rc = httpConnection.getResponseCode(); 
      if (rc != HttpConnection.HTTP_OK) { 
      throw new IOException("HTTP response code: " + rc); 
      } 
      httpInput = httpConnection.openInputStream(); 
      InputStream inp = httpInput; 
      byte[] b = IOUtilities.streamToBytes(inp); 
      EncodedImage hai = EncodedImage.createEncodedImage(b, 0, b.length); 
      return hai.getBitmap(); 

      } catch (Exception ex) { 
      System.out.println("URL Bitmap Error........" + ex.getMessage()); 
      } finally { 
      try { 
      if (httpInput != null) 
      httpInput.close(); 
      if (httpDataOutput != null) 
      httpDataOutput.close(); 
      if (httpConnection != null) 
      httpConnection.close(); 
      } catch (Exception e) { 
      e.printStackTrace(); 

      } 
      } 
      return bitmp; 
     } 

我把的多個實例,我自定義字段放入verticalfieldmanager,但圖像會減慢向下滾動。看起來好像每次滾動時都會再次運行paint方法,即使圖像已被下載。

我想我需要在另一個線程下載圖像?有人帶領我走向正確的方向。

回答

3
  1. 您必須在單獨的線程(無阻塞呼叫)中啓動任何HTTP交互。
  2. 爲避免多次下載同一圖像緩存已下載的圖像。您可以使用圖片下載網址作爲圖片標籤來存儲 。
  3. 每當屏幕上出現事件時,都會調用paint方法。因此,如果已下載圖像,請不要開始下載圖像。
+1

3的補充 - 如果它已經開始不開始下載。 – 2011-12-27 06:53:07

+0

謝謝我做了另一個線程下載。任何關於如何實施你所提出的建議的例子? – Adam 2012-01-01 05:34:04