如何裁剪位图与内存的最低使用? [英] How to crop a Bitmap with a minimum usage of memory?

查看:144
本文介绍了如何裁剪位图与内存的最低使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我加载大量的图片来自网络。这是工作的罚款,到目前为止:

In my app I'm loading plenty of images from the web. That's working fine so far:

@Override
public void onSuccess( byte[] response )
{
    Bitmap image = BitmapFactory.decodeByteArray( response, 0, response.length, options );

    ...
}

但事实上,这将是很好在申请过程中只使用图像的提取物。所以,我想是这样的:

But in fact it would be nice to use only an extract of the image during the application process. So I tried something like this:

@Override
public void onSuccess( byte[] response )
{
    Bitmap source = BitmapFactory.decodeByteArray( response, 0, response.length, options );
    Bitmap image = Bitmap.createBitmap( source, 0, 0, source.getWidth(), source.getHeight() - 30 );
    source.recycle();
    source = null;

    ...
}

但我的应用程序会持续数十个图像(OutOfMemoryException异常)后的崩溃。所以(我猜的)我有两次机会,以摆脱掉高度的30像素(它实际上是一个信用信息,但不要担心,我不是偷,没关系,如果我隐藏它):

But my app keeps crashing after loading a few dozens of images (OutOfMemoryException). So (I guess) I have two opportunities to get rid off the 30 pixels of height (it's actually a credit information, but don't worry, I'm not stealing, it's okay if I hide it):


  • 裁剪放大器;将图像保存与使用更少的内存,或

  • 操纵的ImageView隐藏图像的底部(高度可能因缩放)

但我需要这些技术的一些建议。

But I need some advice for these techniques.

推荐答案

尝试是这样的:

    private Bitmap trimImage(Bitmap source)
    {
        int trimY = 20; //Whatever you want to cut off the top
        Bitmap bmOverlay = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
        Canvas c = new Canvas(bmOverlay);

        //Source and destination Rects based on sprite animation.
        Rect srcRect = new Rect(0, trimY, source.getWidth(), source.getHeight()); 
        Rect dstRect = new Rect(0, 0, source.getWidth(), source.getHeight());
        c.drawBitmap(manual1, srcRect, dstRect, null);

        return bmOverlay;
    }

这尚未经过测试,但这样的事情可能做的伎俩。

This hasn't been tested, but something like this might do the trick.

这篇关于如何裁剪位图与内存的最低使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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