2

为了从网站加载多个图像,编写了以下代码。

    public void connectImgtoView(final int max) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                for (int i = 0; i < max; i++) {
                    try {
                        url = new URL(postImgUrl.get(i));
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }

                    try {
                        final BufferedInputStream bufferedInputStream
                                = new BufferedInputStream(url.openStream());
                        Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
                        bufferedInputStream.close();

                        final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
                                bitmap,
                                (int) (992),
                                (int) (1403),
                                true
                        );

                        final int finalI = i;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                postImg[finalI].setImageBitmap(scaledBitmap);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                progressOFF();
            }
        }).start();
    }

这种方法虽然执行成功,但存在执行速度太慢的问题。

所以我想知道什么比这种方法更快。

请帮我。

变量描述

postImgUrl :类型是 ArrayList,这是包含我想要的图像的 url

postImg :类型是 ImageView Array,这是布局中存在的 ImageView。

4

1 回答 1

1

尝试将 for 循环放在一边,Thread以便您可以Threads同时实例化和工作多个。

public void connectImgtoView(final int max) {
    for (int i = 0; i < max; i++) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                    try {
                        url = new URL(postImgUrl.get(i));
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    }

                    try {
                        final BufferedInputStream bufferedInputStream
                                = new BufferedInputStream(url.openStream());
                        Bitmap bitmap = BitmapFactory.decodeStream(bufferedInputStream);
                        bufferedInputStream.close();

                        final Bitmap scaledBitmap = Bitmap.createScaledBitmap(
                                bitmap,
                                (int) (992),
                                (int) (1403),
                                true
                        );

                        final int finalI = i;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                postImg[finalI].setImageBitmap(scaledBitmap);
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                progressOFF();
            }
        }).start();
    }
}
于 2020-05-02T06:52:41.983 回答