0

我想重新调整位图图像的大小......所以我使用下面的代码

   BitmapFactory.Options bmOptions = new BitmapFactory.Options();
   bmOptions.inJustDecodeBounds = true;
   int photoW = bmOptions.outWidth;
   int photoH = bmOptions.outHeight;
   int scaleFactor = Math.min(photoW / 100, photoH / 100);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);

但我的问题是我从这样的可绘制文件夹中获取图像

 Bitmap icon = BitmapFactory.decodeResource(getResources(),
                    Const.template[arg2]);

那么如何将这些东西转换为文件路径,以便我可以在以下行中设置

        Bitmap bitmap = BitmapFactory.decodeFile(path, bmOptions);  

并且可以获得可调整大小的图像

4

2 回答 2

1

没看懂,为什么要用decodeFile()?我很确定你可以使用

BitmapFactory.decodeResource(getResources(), Const.template[arg2]), bmOptions)

文档

于 2012-09-29T13:11:52.140 回答
0

答案如下

    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {

            BitmapFactory.Options bmOptions = new BitmapFactory.Options();

            // If set to true, the decoder will return null (no bitmap), but
            // the out... fields will still be set, allowing the caller to
            // query the bitmap without having to allocate the memory for
            // its pixels.
            bmOptions.inJustDecodeBounds = true;
            int photoW = bmOptions.outWidth;
            int photoH = bmOptions.outHeight;

            // Determine how much to scale down the image
            int scaleFactor = Math.min(photoW / 100, photoH / 100);

            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false;
            bmOptions.inSampleSize = scaleFactor;
            bmOptions.inPurgeable = true;
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), Const.template[arg2],bmOptions);

            Drawable draw = new BitmapDrawable(getResources(), bitmap);

            /* place image to textview */
            TextView txtView = (TextView) findViewById(R.id.imgChooseImage);
            txtView.setCompoundDrawablesWithIntrinsicBounds(draw, null,
                    null, null);
            position = arg2;
        }
    });
于 2012-09-29T13:20:49.527 回答