Android 加载listview图像在滚动时会混淆
当我从SD卡加载图像并将其显示在listview中时出现问题。它们最初加载得很好,但当我开始上下滚动时,图像会被放大,而不是与相应列表项一起显示的图像 这是我的适配器,我在这里做任何事情Android 加载listview图像在滚动时会混淆,android,bitmap,Android,Bitmap,当我从SD卡加载图像并将其显示在listview中时出现问题。它们最初加载得很好,但当我开始上下滚动时,图像会被放大,而不是与相应列表项一起显示的图像 这是我的适配器,我在这里做任何事情 @Override public void bindView(final View view,final Context context,final Cursor cursor){ final int id = cursor.getInt(0);; final Quick
@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);
}
}
public类LoadImage扩展异步任务{
@凌驾
受保护对象[]doInBackground(对象…参数){
字符串u=(字符串)参数[0];
InputStream输入=null;
试一试{
input=getActivity().getContentResolver().openInputStream(Uri.parse(u));
BitmapFactory.Options=new-BitmapFactory.Options();
options.inJustDecodeBounds=false;
位图og=BitmapFactory.decodeStream(输入,空,选项);
int height=options.outHeight;
int width=options.outWidth;
解码资源(getResources(),R.drawable.ic\u contact\u picture,选项);
int imageHeight=options.outHeight;
int imageWidth=options.outWidth;
试一试{
input.close();
}捕获(IOE异常){
e、 printStackTrace();
}
浮动缩放高度=((浮动)图像高度)/高度;
浮动缩放宽度=((浮动)图像宽度)/宽度;
矩阵=新矩阵();
矩阵。后缩放(缩放宽度、缩放高度);
Bitmap resize=Bitmap.createBitmap(og,0,0,宽度,高度,矩阵,真);
试一试{
input.close();
}捕获(IOE异常){
e、 printStackTrace();
}
返回新对象[]{resize,params[1]};
}catch(filenotfounde异常){
e、 printStackTrace();
返回null;
}
}
@凌驾
public void onPostExecute(对象[]参数){
位图大小图像=(位图)参数[0];
QuickContactBadge图像=(QuickContactBadge)参数[1];
image.setImageBitmap(resizedImage);
}
}
我猜这与处理
异步任务中的图像有关,但如何修复它?我认为ListView回收器存在问题。使用ListView滚动时,ListView适配器始终仅加载显示的行(n),向下移动时,第一个位置将循环为n+1位置。所以问题是,您开始加载第一个位置的图像,然后向下滚动,第一个位置现在是滚动列表视图中的第二个位置。
解决方法是检查加载的图像是否应该使用某种行ID放置在行中。您也可以使用像Aquery这样的ImageLoader库来完成这项工作。我忘了设置默认图像
image.setImageResource(R.drawable.ic_contact_picture);
downvote是不必要的,不难理解imageview上的默认图像是什么意思,它将被替换请阅读常见问题解答。扭转了向下的投票,并向上投票,因为它帮助我解决了我的问题。谢谢