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;
    }
}
}

我创建了一个新类,但我不知道如何使用它。

4

2 回答 2

1

您需要在这里使用延迟加载概念。例如:

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

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

您需要在单独的线程中下载图像(而不是在 UI 线程中)。实际上,当您渲染列表行时会发生什么,它会查找位图图像。因此,一旦您创建了列表视图,您可以做什么。提供默认加载位图图片,启动线程下载图片,

于 2012-08-02T04:12:41.117 回答
0

您应该在将数据从 Url 放入向量的线程上创建方法。这可能在您已扩展为线程的连接类中。像这样>>>>

getimagemethod(image[i]);

在您声明您的方法后,获取该方法的图像字符串 url。像这样>>

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

// 你应该将 imageforlist 字符串声明为全局字符串..

newBitmap1 = Util_ImageLoader.getImageFromUrl(imageforlist);

//newBitmap1 也是全局 Bitmap..** }

在此之后将您的位图(即 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 是>>>> private static final int HPADDING = Display.getWidth() <= 320 ?6:8;

这只是一步一步的教程示例。如果有任何疑问,您可以在此处发布...

于 2012-09-02T11:42:21.563 回答