英文:
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
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 < 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 < 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();
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论