0

我有一个列表视图,其中每个列表项都有一些图像视图和一些文本视图。我正在使用意图打开画廊,选择一个选定的图像并将其分配给 imageview 但我的问题是,当我滚动我的列表视图时,当图像设置为该图像视图时,选定的图像消失并且原始图像出现在那地方。那么谁能告诉我为什么会这样,我该如何避免呢?

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);

    }
4

1 回答 1

0

您应该更新您的 listadapter,或您的 listadapter 的来源。适配器是您的数据的模型。当您使用新值更新适配器后调用适配器上的 notifyDatasetChanged 时,您的视图将自动更新。

于 2013-07-12T11:13:11.370 回答