如何提升以将多个图像从网络加载到 ImageViews
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 Array,这是布局中存在的ImageView。
尝试将 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();
}
}
为了从网站加载多张图片,编写了以下代码。
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。
尝试将 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();
}
}