开发者

Displaying images and managing memory in android

I wrote a program that at any time displays 8 user selected images on the screen. Each image is taken from its original form and scaled down to a uniform size. In order to do this I am using the code below:

Bitmap bitmapOrg = BitmapFactory.decodeFile(File Location Here);

        int width = bitmapOrg.getWidth();
        int height = bitmapOrg.getHeight();
        int newWidth = 100;
        int newHeight = 100;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        // createa matrix for the manipulation
        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);

        // recreate the new Bitmap
        Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
                height, matrix, true);
        // make a Drawable from Bitmap to allow to set the BitMap
        // to the ImageView, ImageButton or what ever
        BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

        //ImageView imageView开发者_如何转开发 = new ImageView(this);
        ImageView iv = (ImageView) findViewById(R.id.imageView1);

        // set the Drawable on the ImageView
        iv.setImageDrawable(bmd);

        // center the Image
        iv.setScaleType(ScaleType.CENTER);

Even though my code works its not perfect. It seems like I'm using up a lot of memory especially calling this code possibly 8 times at once. Where in the code would I "recycle" the memory and how could I make this code possibly run better?

EDIT:

So I implemented the code in my project and it was working perfect and then I tried to add it to other sections and it just stopped working all together. My code looks like this: any idea what am I doing wrong?

    BitmapFactory.Options options = new BitmapFactory.Options(); 
 options.outWidth = 50; 
 options.outHeight = 50; Bitmap bitmap = BitmapFactory.decodeFile(path, options);
 ImageView iv = (ImageView) findViewById(R.id.imageView7); 
 iv.setImageBitmap(bitmap);


You can use BitmapFactory.Options to scale the image as you decode it rather than reading in a full image and then scaling it.

BitmapFactory.Options options = new BitmapFactory.Options();
// You can play with this setting depending on how large your images are
// For example, to scale ~400x400 images to ~100x100, you can use 4.
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(path, options);

Edit - George is correct. You should use inSampleSize to create a smaller image of the general size you need and then have it resized to the exact size you want using your ImageView. I've corrected my answer above to reflect this.

In any case, you should be much better off memory-wise if you are scaling the bitmaps during decode.


@matthew-willis I do not think you can use outWidth and outHeight to scale a bitmap. I believe they are output parameters only: they report the size of the bitmap created after the fact--setting them prior to decoding has no effect. You should use inSampleSize if you want to scale as you decode. George

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜