显示图像和android的管理内存 [英] Displaying images and managing memory in android

查看:105
本文介绍了显示图像和android的管理内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个程序,在任何时间在屏幕上显示8个用户选择的图像。每个图像从原始形式和缩小到一个均匀的尺寸。为了做到这一点,我现在用的是code波纹管:

 位图bitmapOrg = BitmapFactory.de codeFILE(文件位置在此);

        INT宽度= bitmapOrg.getWidth();
        INT高= bitmapOrg.getHeight();
        INT newWidth = 100;
        INT newHeight = 100;

        //计算比例 - 在这种情况下= 0.4f
        浮动scaleWidth =((浮点)newWidth)/宽度;
        浮动scaleHeight =((浮点)newHeight)/身高;

        // createa矩阵用于操作
        字模=新的Matrix();
        //调整位图
        matrix.postScale(scaleWidth,scaleHeight);

        //重新创建新的位图
        位图resizedBitmap = Bitmap.createBitmap(bitmapOrg,0,0,宽度,
                高度,矩阵,真正的);
        //使从位图被拉伸以允许设置位图
        //为ImageView的,ImageButton的或什么都
        BitmapDrawable BMD =新BitmapDrawable(resizedBitmap);

        // ImageView的ImageView的=新ImageView的(这一点);
        ImageView的IV =(ImageView的)findViewById(R.id.imageView1);

        //设置绘制对象上的ImageView
        iv.setImageDrawable(BMD);

        //中心图片
        iv.setScaleType(ScaleType.CENTER);
 

虽然我的code ++工程,它不是完美的。这似乎是即时通讯使用了大量的内存尤其是在调用此code可能是8次一次。凡在code,我会回收的记忆和我怎么能做出这种code可能运行得更好?

编辑:

所以,我实现了code。在我的项目,这是工作完美的,然后我试着将它添加到其他部分,它只是停止工作都在一起。我的code看起来是这样的:任何想法什么即时做错了什么?

  BitmapFactory.Options选项=新BitmapFactory.Options();
 options.outWidth = 50;
 options.outHeight = 50;点阵位图= BitmapFactory.de codeFILE(路径选择);
 ImageView的IV =(ImageView的)findViewById(R.id.imageView7);
 iv.setImageBitmap(位);
 

解决方案

您可以使用的 BitmapFactory.Options 来缩放图像的为c它的,而不是读一个完整的图像,然后进行缩放。

您去$ C $

  BitmapFactory.Options选项=新BitmapFactory.Options();
//您可以使用此设置播放根据您的图片有多大
//例如,扩展〜400x400的图像〜100×100,可以使用4。
options.inSampleSize = 4;
点阵位图= BitmapFactory.de codeFILE(路径选择);
 

修改 - 乔治是正确的。您应该使用 inSampleSize 来创建你所需要的一般大小的小图像,然后把它调整到你想要使用的ImageView的确切大小。我已经纠正了我上面的答案,以反映这一点。

在任何情况下,你应该好得多内存明智的,如果你是缩放过程中去code中的位图。

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 bellow:

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 im 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 im 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.

这篇关于显示图像和android的管理内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆