【发布时间】:2010-11-26 04:09:06
【问题描述】:
我的应用程序包含一个列表视图,可以选择包含来自用户 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);
【问题讨论】: