当我从 SD 卡加载图像并将它们显示在我的列表视图中时,我遇到了问题。它们最初加载得很好,但是当我开始上下滚动时,图像会全部变大,而不是与相应列表项一起使用的图像
这是我做所有事情的适配器
@Override
public void bindView(final View view,final Context context,final Cursor cursor){
final int id = cursor.getInt(0);;
final QuickContactBadge image = (QuickContactBadge)view.findViewById(R.id.quickContactBadge1);
image.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(!fromGame){
bowlerID = id;
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i,1);
}
}
});
String uri = cursor.getString(3);
if(uri != null){
image.setImageResource(R.drawable.ic_contact_picture);
new LoadImage().execute(new Object[]{uri,image});
}else{
}
TextView first = (TextView)view.findViewById(R.id.bListTextView);
TextView last = (TextView)view.findViewById(R.id.bListTextView2);
first.setText(cursor.getString(1));
last.setText(cursor.getString(2));
}
和我的AsyncTask
,我给出了图像将显示的视图和图像的 uri
public class LoadImage extends AsyncTask<Object,Void,Object[]>{
@Override
protected Object[] doInBackground(Object... params) {
String u = (String) params[0];
InputStream input=null;
try {
input = getActivity().getContentResolver().openInputStream(Uri.parse(u));
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
Bitmap og = BitmapFactory.decodeStream(input,null,options);
int height = options.outHeight;
int width = options.outWidth;
BitmapFactory.decodeResource(getResources(), R.drawable.ic_contact_picture, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
float scaledHeight = ((float)imageHeight)/height;
float scaledWidth = ((float)imageWidth)/width;
Matrix matrix = new Matrix();
matrix.postScale(scaledWidth, scaledHeight);
Bitmap resize = Bitmap.createBitmap(og, 0, 0, width, height, matrix, true);
try {
input.close();
} catch (IOException e) {
e.printStackTrace();
}
return new Object[] {resize,params[1]};
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
}
@Override
public void onPostExecute(Object[] params){
Bitmap resizedImage = (Bitmap)params[0];
QuickContactBadge image = (QuickContactBadge) params[1];
image.setImageBitmap(resizedImage);
}
}
我猜这与处理图像有关,AsyncTask
但我该如何解决呢?