我有一个 listview ,其中每个列表项都有一些imageview和一些textview。我正在使用意图打开图库,选择一个选定的图像并将其分配给imageview但我的问题是,当我滚动列表视图时图像设置为该图像视图时,所选图像< strong>消失,原始图像出现在该位置。 所以任何人都能告诉我为什么会这样,我该如何避免呢?
Intent photoPickerIntent = new Intent(
Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent,
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case SELECT_PHOTO:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
/*
* InputStream imageStream=null; try { imageStream =
* getContentResolver().openInputStream(selectedImage); } catch
* (FileNotFoundException e) { // TODO Auto-generated catch
* block e.printStackTrace(); } Bitmap image =
* BitmapFactory.decodeStream(imageStream);
*/
try {
LinearLayout ll = (LinearLayout) lv.getChildAt(position);
ImageView im = (ImageView) ll
.findViewById(R.id.image);
Bitmap b = decodeUri(selectedImage);
Bitmap circleBitmap = Bitmap.createBitmap(b.getWidth(),
b.getHeight(), Bitmap.Config.ARGB_8888);
BitmapShader shader = new BitmapShader(b, TileMode.CLAMP,
TileMode.CLAMP);
Paint paint = new Paint();
paint.setShader(shader);
Canvas c = new Canvas(circleBitmap);
c.drawCircle(b.getWidth() / 2, b.getHeight() / 2,
b.getWidth() / 2, paint);
im.setImageBitmap(circleBitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} SELECT_PHOTO);
private Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 40;
// 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 *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
getContentResolver().openInputStream(selectedImage), null, o2);
}
答案 0 :(得分:0)
您应该更新listadapter或listadapter的来源。适配器是数据的模型。当您使用新值更新适配器上的notifyDatasetChanged时,您的视图将自动更新。