如何通过网络加载多个图片到ImageView并提升性能

huangapple go评论72阅读模式
英文:

How to boost to load multiple images from web to ImageViews

问题

以下是您要求的翻译内容:

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

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 数组,这是布局中存在的 ImageView。

英文:

To load several images from a website, the following code was written.

    public void connectImgtoView(final int max) {

        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                for (int i = 0; i &lt; 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();
    }

Although this method is performed successfully, there is a problem that the performance speed is too slow.

So I want to know what is faster than this method.

Please help me.

Variable description

postImgUrl : type is ArrayList<String>, this is url that has image i want

postImg : type is ImageView Array, this is ImageView that exists in the layout.

答案1

得分: 1

尝试将for循环放在Thread之外,以便可以同时实例化和处理多个Thread

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();
    }
}
英文:

Try placing the for loop out side of Thread so that you can instantiate and work multiple Threads simultaneously.

public void connectImgtoView(final int max) {
    for (int i = 0; i &lt; 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();
    }
}

huangapple
  • 本文由 发表于 2020年5月2日 14:39:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61555441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定