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もグローバルビットマップです..** }

この後、newBitmap1であるビットマップを次のようにベクターに配置します>>

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 に答える