3

グリッドビューに画像を表示する単純な Android アプリケーションを作成しました。いくつかの画像を選択して送信できます。これは私の getView メソッドです。

  public View getView(int position, View convertView, ViewGroup parent)
    {
        View v;
        if (convertView == null)
        {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.imageview, null);
            final CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
            cb.setOnCheckedChangeListener(new AddPhotos());

            ImageView iv = (ImageView) v.findViewById(R.id.image);

            //make image clickable so that it is checked when clicked on the image
            iv.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    cb.setChecked((cb.isChecked() ? false : true));

                }
            });

            imagecursor.moveToPosition(position);

            uriList.add(imagecursor.getString(image_column_index));

            File image = new File(imagecursor.getString(image_column_index));

            Bitmap actualBitmap = resizeBitMapImage(image.getPath(), image_width, image_height);
            iv.setImageBitmap(actualBitmap);

            iv.setLayoutParams(new FrameLayout.LayoutParams(95, 95));
            System.gc();

        }
        else
        {
            v = convertView;
            System.gc();

        }
        return v;
    }
}

これで問題になったのは、画像が少ない場合、つまりすべての画像がスクロールする必要なく表示された場合、正常に機能していたことです。しかし、表示されていない画像があり、下にスクロールする必要がある場合、以前の画像を繰り返し、下または上にスクロールすると画像のチェック状態が変わるという奇妙な動作を示しました。getView メソッドを次のように変更しました

public View getView(int 位置、View convertView、ViewGroup 親)

    {

        View v;
        if (convertView == null)
        {
            LayoutInflater li = getLayoutInflater();
            v = li.inflate(R.layout.imageview, null);

        }
        else
        {
            v = convertView;
            System.gc();

        }

        final CheckBox cb = (CheckBox) v.findViewById(R.id.checkbox);
        cb.setOnCheckedChangeListener(new AddPhotos());

        ImageView iv = (ImageView) v.findViewById(R.id.image);

        // make image clickable so that it is checked when clicked on the image
        iv.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                cb.setChecked((cb.isChecked() ? false : true));

            }
        });

        imagecursor.moveToPosition(position);

        uriList.add(imagecursor.getString(image_column_index));

        File image = new File(imagecursor.getString(image_column_index));

        Bitmap actualBitmap = resizeBitMapImage(image.getPath(), image_width, image_height);
        iv.setImageBitmap(actualBitmap);

        iv.setLayoutParams(new FrameLayout.LayoutParams(95, 95));
        System.gc();

        return v;
    }
}

現在、画像の順序は変更されていませんが、上下にスクロールするとANRがスローされ、チェックされた状態も変更されます(ランダムだと思います)。traces.txt は、 bitMapImage = BitmapFactory.decodeFile(filePath, options);からのものであることを示しています。以下の resizeBitMapImage メソッドの行

public static Bitmap resizeBitMapImage(String filePath, int targetWidth, int targetHeight)

{
    Bitmap bitMapImage = null;
    // First, get the dimensions of the image
    Options options = new Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    double sampleSize = 0;
    // Only scale if we need to
    // (16384 buffer for img processing)
    Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth - targetWidth);

    if (options.outHeight * options.outWidth * 2 >= 1638)
    {
        // Load, scaling to smallest power of 2 that'll get it <= desired
        // dimensions
        sampleSize = scaleByHeight ? options.outHeight / targetHeight : options.outWidth / targetWidth;
        sampleSize = (int) Math.pow(2d, Math.floor(Math.log(sampleSize) / Math.log(2d)));
    }

    // Do the actual decoding
    options.inJustDecodeBounds = false;
    options.inTempStorage = new byte[128];
    while (true)
    {
        try
        {
            options.inSampleSize = (int) sampleSize;
            bitMapImage = BitmapFactory.decodeFile(filePath, options);

            break;
        }
        catch (Exception ex)
        {
            try
            {
                sampleSize = sampleSize * 2;
            }
            catch (Exception ex1)
            {

            }
        }
    }

    return bitMapImage;
}

今、私はAndroid開発に非常に慣れていないため、非常に混乱しています。どんな助けでも大歓迎です

編集: 画像は gridview に追加されており、getView メソッドはこの gridview の imageAdapter 用です。

4

0 に答える 0