在安卓中显示图像和管理内存[英] Displaying images and managing memory in android

本文是小编为大家收集整理的关于在安卓中显示图像和管理内存的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我写了一个程序,随时在屏幕上显示8个用户所选映像.每个图像都从其原始形式取出并缩放到均匀的尺寸.为了执行此操作,我正在使用以下代码:

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

即使我的代码工作不完美.似乎我正在使用很多内存,特别是一次调用此代码可能会发生8次.在代码中的位置我会"回收"内存,并且如何使这段代码可能更好地运行?

编辑:

所以我在我的项目中实现了代码,它的工作完美,然后我试图将它添加到其他部分,它只是停止全部工作.我的代码如下所示:任何想法我做错了什么?

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

推荐答案

您可以使用 bitmapfactory.options 在解码它时缩放图像而不是在完整图像中读取,然后缩放它.

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

编辑 - 乔治是正确的.您应该使用inSampleSize创建较小的图像,您需要的一般大小,然后将其调整为使用ImageView的确切尺寸.我已经纠正了上面的答案来反映这一点.

在任何情况下,如果您在解码期间缩放位图,则应好得多更好地关闭内存.

其他推荐答案

@ matthew-willis我不认为你可以使用outwidth和Outheight来扩展位图.我相信它们只是输出参数:他们报告事实后创建的位图的大小 - 在解码之前设置它们没有效果.如果您想要在解码时缩放,则应使用INSAMPLUSIZE.乔治

本文地址:https://www.itbaoku.cn/post/102717.html

问题描述

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