2012-08-02 41 views
0

對於我目前的代碼,它會首先下載圖像,然後只顯示數據並導致設備滯後。如何在顯示列表數據後下載在線圖像?

public Custom_ListField(Vector content, boolean islatest) { 
    this.content = content; 
    this.islatest = islatest; 

    newsid = new int[content.size()]; 
    title = new String[content.size()]; 
    category = new String[content.size()]; 
    date = new String[content.size()]; 
    imagepath = new String[content.size()]; 
    catsid = new int[content.size()]; 
    imagebitmap = new Bitmap[content.size()]; 
    ischeck = new boolean[content.size()]; 

    for (int i = 0; i < content.size(); i++) { 
     newslist = (List_News) content.elementAt(i); 
     newsid[i] = newslist.getID(); 
     title[i] = newslist.getNtitle(); 
     category[i] = newslist.getNewCatName(); 
     date[i] = newslist.getNArticalD(); 
     imagepath[i] = newslist.getImagePath(); 
     catsid[i] = newslist.getCatID(); 
     ischeck[i] = false; 

     if (!imagepath[i].toString().equals("no picture")) { 
      if (Config_GlobalFunction.isConnected()) 
       imagebitmap[i] = Util_ImageLoader.loadImage(imagepath[i]); 
      else 
       imagebitmap[i] = localimage; 
     } 
    } 
    initCallbackListening(); 
} 

private void initCallbackListening() { 
    callback = new ListCallback(); 
    this.setCallback(callback); 
    this.setRowHeight(-2); 
} 

private class ListCallback implements ListFieldCallback { 
    public ListCallback() { 

    } 

    public void drawListRow(ListField listField, Graphics graphics, 
      final int index, int y, int width) { 
     currentPosition = index; 

     if (!imagepath[index].toString().equals("no picture")) { 
      float ratio = (float) ((float) localimage.getHeight()/(float) imagebitmap[index] 
        .getHeight()); 
      Bitmap temp = new Bitmap(
        (int) (imagebitmap[index].getWidth() * ratio), 
        (int) (imagebitmap[index].getHeight() * ratio)); 
      imagebitmap[index].scaleInto(temp, Bitmap.FILTER_BILINEAR, 
        Bitmap.SCALE_TO_FIT); 
      imagebitmap[index] = temp; 

      graphics.drawBitmap(
        Display.getWidth() 
          - localimage.getWidth() 
          - 5 
          + ((localimage.getWidth() - imagebitmap[index] 
            .getWidth())/2), 
        y 
          + (listField.getRowHeight(index) - imagebitmap[index] 
            .getHeight())/2, 
        imagebitmap[index].getWidth(), 
        imagebitmap[index].getHeight(), imagebitmap[index], 0, 
        0); 

      graphics.setColor(Color.BLACK); 
      text = Config_GlobalFunction 
        .wrap(title[index], Display.getWidth() 
          - imagebitmap[index].getWidth() - 15); 

      for (int i = 0; i < text.size(); i++) { 
       int liney = y + (i * Font.getDefault().getHeight()); 
       graphics.drawText(
         (String) text.elementAt(i), 
         5, 
         liney + 3, 
         DrawStyle.TOP | DrawStyle.LEFT | DrawStyle.ELLIPSIS, 
         Display.getWidth() - imagebitmap[index].getWidth() 
           - 10); 
      } 
     } else { 
      graphics.setColor(Color.BLACK); 
      text = Config_GlobalFunction.wrap(title[index], 
        Display.getWidth() - 10); 
      for (int i = 0; i < text.size(); i++) { 
       int liney = y + (i * Font.getDefault().getHeight()); 
       graphics.drawText(
         (String) text.elementAt(i), 
         5, 
         liney + 3, 
         DrawStyle.TOP | DrawStyle.LEFT | DrawStyle.ELLIPSIS, 
         Display.getWidth() - 10); 
      } 
     } 

     if (text.size() == 2) { 
      graphics.setColor(Color.GRAY); 
      graphics.drawText(date[index], 5, y 
        + Font.getDefault().getHeight() + 3); 

      if (islatest) { 
       graphics.setColor(Color.RED); 
       graphics.drawText(category[index], Font.getDefault() 
         .getAdvance(date[index]) + 15, y 
         + Font.getDefault().getHeight() + 3); 
      } 
     } else if (text.size() == 3) { 
      graphics.setColor(Color.GRAY); 
      graphics.drawText(date[index], 5, y 
        + Font.getDefault().getHeight() * 2 + 3); 

      if (islatest) { 
       graphics.setColor(Color.RED); 
       graphics.drawText(category[index], Font.getDefault() 
         .getAdvance(date[index]) + 15, y 
         + Font.getDefault().getHeight() * 2 + 3); 
      } 
     } 

     if (!imagepath[index].toString().equals("no picture")) 
      setRowHeight(index, imagebitmap[index].getHeight() + 10); 
     else { 
      if (text.size() == 2) 
       setRowHeight(index, getRowHeight() + 9); 
      else if (text.size() == 3) { 
       setRowHeight(index, getRowHeight() * 15/10 + 9); 
      } 
     } 

     graphics.setColor(Color.WHITE); 
     graphics.drawRect(0, y, width, listField.getRowHeight(index)); 

     ischeck[index] = true; 
    } 
} 

我想顯示數據後,該imagebitmap[i] = Util_ImageLoader.loadImage(imagepath[i]);運行,這樣無需卡在那裏。然而,我試圖把drawListRow,它的工作,但很慢,因爲最初顯示它將運行0-8次,然後當我滾動列表字段,它再次運行。它被再次下載並下載。

更新

public class Util_LazyLoader implements Runnable { 
String url = null; 
BitmapDowloadListener listener = null; 

public Util_LazyLoader(String url, BitmapDowloadListener listener) { 
    this.url = url; 
    this.listener = listener; 
} 

public void run() { 
    Bitmap bmpImage = getImageFromWeb(url); 
    listener.ImageDownloadCompleted(bmpImage); 
} 

private Bitmap getImageFromWeb(String url) { 
    HttpConnection connection = null; 
    InputStream inputStream = null; 
    EncodedImage bitmap; 
    byte[] dataArray = null; 

    try { 
     connection = (HttpConnection) (new ConnectionFactory()) 
       .getConnection(url + Database_Webservice.ht_params) 
       .getConnection(); 

     int responseCode = connection.getResponseCode(); 
     if (responseCode == HttpConnection.HTTP_OK) { 
      inputStream = connection.openDataInputStream(); 
      dataArray = IOUtilities.streamToBytes(inputStream); 
     } 
    } catch (Exception ex) { 
    } finally { 
     try { 
      inputStream.close(); 
      connection.close(); 
     } catch (Exception e) { 
     } 
    } 

    if (dataArray != null) { 
     bitmap = EncodedImage.createEncodedImage(dataArray, 0, 
       dataArray.length); 
     return bitmap.getBitmap(); 
    } else { 
     return null; 
    } 
} 
} 

我創建了一個新的類,但我不知道如何使用它。

+0

你還在工作在這個問題上艾倫? – Nate 2012-08-03 05:45:01

+0

沒有更多,我得到它的工作,但又得到了另一個問題,請幫助我,謝謝 – 2012-08-03 05:59:24

回答

0

你應該在線程中創建方法,將數據從Url傳遞到向量。這可能會在您的連接類中擴展爲線程。 這樣>>>>

getimagemethod(image[i]); 

後,你聲明你的方法獲得圖像的URL字符串的方法。像這樣>>

private void getimagemethod(String image2) 
{ 
this.imageforlist = image2; 

//你應該聲明imageforlist字符串作爲全局字符串..

newBitmap1 = Util_ImageLoader.getImageFromUrl(imageforlist); 

// newBitmap1也是全球位圖.. ** }

此後,將您的位圖是newBitmap1到Vector像這樣>>

imagevct.addElement(newBitmap1); 

這裏imagevct是向量,這也是全球** ** 序創建全局矢量使用這個....

private Vector imagevct = new Vector(); 

現在你準備繪製您的名單上的位圖

因爲這樣做是這樣的...

public void drawListRow(ListField list, Graphics g, int index, int y, int w) { 
Bitmap imagee = (Bitmap) imagevct.elementAt(index); 
g.drawBitmap(HPADDING, 15 + y, 60, 60, imagee , 0, 0); 
} 

這裏HPADDING是>>>> 私有靜態最終詮釋HPADDING = Display.getWidth()< = 320? 6:8;

這只是樣品教程一步一步.. 如果有任何疑問,那麼你可以在這裏發佈...

1

你需要在這裏使用延遲加載的概念。 對於前:

http://supportforums.blackberry.com/t5/Java-Development/How-to-load-images-quickly-like-android/m-p/1487995#M187253

http://supportforums.blackberry.com/t5/Java-Development/Lazy-loading-issue-in-blackberry/m-p/1835127

您需要下載的圖像在一個單獨的線程(不是UI線程)。實際上,當你渲染列表行時會發生什麼,它會查找位圖圖像。 所以,一旦你創建你的列表視圖,你可以做什麼。提供一個默認的加載位圖圖像,啓動一個線程來下載圖像,

+0

我創建了新的類,但似乎不能使用它,因爲它適用於bitmapfield和我的一個是位圖只有 – 2012-08-02 05:48:56

+0

我認爲這是不適合我的,因爲我無法在循環內運行此線程。這適用於循環所有行而不是特定行的圖像 – 2012-08-02 09:14:11

相關問題