如何裁剪位图与内存的最低使用?

编程入门 行业动态 更新时间:2024-10-27 09:37:51
本文介绍了如何裁剪位图与内存的最低使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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.

更多推荐

如何裁剪位图与内存的最低使用?

本文发布于:2023-08-02 03:48:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1274366.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:位图   最低   内存

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!