我的应用包含一个列表视图,可以选择包含来自用户SD卡的图像。它工作正常但是当加载任何图像时会有明显的滞后。我在getView中有这个:
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image, o);
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
Bitmap myBitmap = BitmapFactory.decodeFile(image, o2);
image_main.setImageBitmap(myBitmap);
有什么建议吗?
编辑:用此替换上面的内容但没有加载图片......
class Thumbnailer extends AsyncTask<String, Void, Bitmap> {
String image;
@Override
protected void onPostExecute(Bitmap result) {
image_main.setImageBitmap(result);
}
@Override
protected void onProgressUpdate(Void... progress) {
}
@Override
protected Bitmap doInBackground(String... params) {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image, o);
final int REQUIRED_SIZE=70;
//Find the correct scale value. It should be the power of 2.
int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}
//Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeFile(image, o2);
}
}
new Thumbnailer().execute(image);
答案 0 :(得分:2)
显示带占位符图像的行。让BitmapFactory
在[{1}}中工作,完成后更新AsyncTask
(如果ImageView
尚未回收,那么仍需要此特定缩略图)。缓存您的工作,这样您就不必再次重新进行所有缩放,至少对于ImageView
的这个版本,如果图像不会改变,可能会更长。