2012-08-02 39 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

相关问题