社区
Android
帖子详情
Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565) 内存溢出
sinat_15620989
2017-08-01 04:56:57
求各路大佬们
小弟在创建一个空的足够大的底图bitmap的时候就报oom
如题
因为要多张图片进行纵向的拼接,所以高度是所有图片的高度和
创建底图的时候就报了oom 这个怎么解决呀
急急急
...全文
334
2
打赏
收藏
Bitmap.createBitmap(width,height, Bitmap.Config.RGB_565) 内存溢出
求各路大佬们 小弟在创建一个空的足够大的底图bitmap的时候就报oom 如题 因为要多张图片进行纵向的拼接,所以高度是所有图片的高度和 创建底图的时候就报了oom 这个怎么解决呀 急急急
复制链接
扫一扫
分享
转发到动态
举报
AI
作业
写回复
配置赞助广告
用AI写文章
2 条
回复
切换为时间正序
请发表友善的回复…
发表回复
打赏红包
sinat_15620989
2017-08-01
打赏
举报
回复
引用 1 楼 jklwan 的回复:
那就不要用bitmap了吧,用自定义布局,计算好图片的排列,然后在onDraw中把所有的图片画出来。
用bitmap主要是显示和保存文件的
jklwan
2017-08-01
打赏
举报
回复
那就不要用bitmap了吧,用自定义布局,计算好图片的排列,然后在onDraw中把所有的图片画出来。
android
Bitmap
用法总结
android
Bitmap
用法总结
Bitmap
用法总结 1、Drawable →
Bitmap
public static
Bitmap
drawableTo
Bitmap
(Drawable drawable) {
Bitmap
bitmap
=
Bitmap
.
create
Bitmap
( drawable.getIntrinsic
Width
(), drawable.getIntrinsic
Height
(), drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap
.
Config
.A
RGB
_8888 :
Bitmap
.
Config
.
RGB
_
565
); Canvas canvas = new Canvas(
bitmap
); // canvas.set
Bitmap
(
bitmap
); drawable.setBounds(0, 0, drawable.getIntrinsic
Width
(), drawable.getIntrinsic
Height
()); drawable.draw(canvas); return
bitmap
; } 2、从资源中获取
Bitmap
Resources res=getResources();
Bitmap
bmp=
Bitmap
Factory.decodeResource(res, R.drawable.pic); 3、
Bitmap
→ byte[] private byte[]
Bitmap
2Bytes(
Bitmap
bm){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(
Bitmap
.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } 4、byte[] →
Bitmap
private
Bitmap
Bytes2Bimap(byte[] b){ if(b.length!=0){ return
Bitmap
Factory.decodeByteArray(b, 0, b.length); } else { return null; } } 5、保存
bitmap
static boolean save
Bitmap
2file(
Bitmap
bmp,String filename){ CompressFormat format=
Bitmap
.CompressFormat.JPEG; int quality = 100; OutputStream stream = null; try { stream = new FileOutputStream("/sdcard/" + filename); } catch (FileNotFoundException e) { // TODO Auto-generated catch block Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. e.printStackTrace(); } return bmp.compress(format, quality, stream); } 6、将图片按自己的要求缩放 // 图片源
Bitmap
bm =
Bitmap
Factory.decodeStream(getResources() .openRawResource(R.drawable.dog)); // 获得图片的宽高 int
width
= bm.get
Width
(); int
height
= bm.get
Height
(); // 设置想要的大小 int new
Width
= 320; int new
Height
= 480; // 计算缩放比例 float scale
Width
= ((float) new
Width
) /
width
; float scale
Height
= ((float) new
Height
) /
height
; // 取得想要缩放的matrix参数 Matrix matrix = new Matrix(); matrix.postScale(scale
Width
, scale
Height
); // 得到新的图片
Bitmap
newbm =
Bitmap
.
create
Bitmap
(bm, 0, 0,
width
,
height
, matrix, true); // 放在画布上 canvas.draw
Bitmap
(newbm, 0, 0, paint); 相关知识链接:http://www.eoeandroid.com/thread-3162-1-1.html 7、
bitmap
的用法小结
Bitmap
Factory.Options option = new
Bitmap
Factory.Options(); option.inSampleSize = 2; //将图片设为原来宽高的1/2,防止
内存溢出
Bitmap
bm =
Bitmap
Factory.decodeFile("",option);//文件流 URL url = new URL(""); InputStream is = url.openStream();
Bitmap
bm =
Bitmap
Factory.decodeStream(is); android:scaleType: android:scaleType是控制图片如何resized/moved来匹对ImageView的size。ImageView.ScaleType / android:scaleType值的意义区别: CENTER /center 按图片的原来size居中显示,当图片长/宽超过View的长/宽,则截取图片的居中部分 显示 CENTER_CROP / centerCrop 按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长 (宽) CENTER_INSIDE / centerInside 将图片的内容完整居中显示,通过按比例缩小或原来的size使得图片 长/宽等于或小于View的长/宽 Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. FIT_CENTER / fitCenter 把图片按比例扩大/缩小到View的宽度,居中显示 FIT_END / fitEnd 把图片按比例扩大/缩小到View的宽度,显示在View的下部分位置 FIT_START / fitStart 把图片按比例扩大/缩小到View的宽度,显示在View的上部分位置 FIT_XY / fitXY 把图片 不按比例 扩大/缩小到View的大小显示 MATRIX / matrix 用矩阵来绘制,动态缩小放大图片来显示。 //放大缩小图片 public static
Bitmap
zoom
Bitmap
(
Bitmap
bitmap
,int w,int h){ int
width
=
bitmap
.get
Width
(); int
height
=
bitmap
.get
Height
(); Matrix matrix = new Matrix(); float scaleWidht = ((float)w /
width
); float scale
Height
= ((float)h /
height
); matrix.postScale(scaleWidht, scale
Height
);
Bitmap
newbmp =
Bitmap
.
create
Bitmap
(
bitmap
, 0, 0,
width
,
height
, matrix, true); return newbmp; } //将Drawable转化为
Bitmap
public static
Bitmap
drawableTo
Bitmap
(Drawable drawable){ int
width
= drawable.getIntrinsic
Width
(); int
height
= drawable.getIntrinsic
Height
();
Bitmap
bitmap
=
Bitmap
.
create
Bitmap
(
width
,
height
, drawable.getOpacity() != PixelFormat.OPAQUE ?
Bitmap
.
Config
.A
RGB
_8888 :
Bitmap
.
Config
.
RGB
_
565
); Canvas canvas = new Canvas(
bitmap
); drawable.setBounds(0,0,
width
,
height
); drawable.draw(canvas); return
bitmap
; Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. } //获得圆角图片的方法 public static
Bitmap
getRoundedCorner
Bitmap
(
Bitmap
bitmap
,float roundPx){
Bitmap
output =
Bitmap
.
create
Bitmap
(
bitmap
.get
Width
(),
bitmap
.get
Height
(),
Config
.A
RGB
_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0,
bitmap
.get
Width
(),
bitmap
.get
Height
()); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawA
RGB
(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.draw
Bitmap
(
bitmap
, rect, rect, paint); return output; } //获得带倒影的图片方法 public static
Bitmap
create
ReflectionImageWithOrigin(
Bitmap
bitmap
){ final int reflectionGap = 4; int
width
=
bitmap
.get
Width
(); int
height
=
bitmap
.get
Height
(); Matrix matrix = new Matrix(); matrix.preScale(1, -1);
Bitmap
reflectionImage =
Bitmap
.
create
Bitmap
(
bitmap
, 0,
height
/2,
width
,
height
/2, matrix, false);
Bitmap
bitmap
WithReflection =
Bitmap
.
create
Bitmap
(
width
, (
height
+
height
/2),
Config
.A
RGB
_8888); Canvas canvas = new Canvas(
bitmap
WithReflection); canvas.draw
Bitmap
(
bitmap
, 0, 0, null); Paint deafalutPaint = new Paint(); Generated by Foxit PDF Creator © Foxit Software http://www.foxitsoftware.com For evaluation only. canvas.drawRect(0,
height
,
width
,
height
+ reflectionGap, deafalutPaint); canvas.draw
Bitmap
(reflectionImage, 0,
height
+ reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0,
bitmap
.get
Height
(), 0,
bitmap
WithReflection.get
Height
() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0,
height
,
width
,
bitmap
WithReflection.get
Height
() + reflectionGap, paint); return
bitmap
WithReflection; } }
应用源码之(
Bitmap
位图渲染与操作.zip
android 源码学习. 资料部分来源于合法的互联网渠道收集和整理,供大家学习参考与交流。本人不对所涉及的版权问题或内容负法律责任。如有侵权,请通知本人删除。感谢CSDN官方提供大家交流的平台
Android图像介绍-
Bitmap
常用操作
Android图像介绍-
Bitmap
常用操作
我注释的
Bitmap
fun
我自己注释的
Bitmap
fun便于理解和修改,只是注释了功能实现逻辑,没有涉及底层的实现逻辑,有时间了就全部注释和讲解。
Android
Bitmap
使用demo
Bitmap
位图的创建和编辑
Bitmap
明度,亮度,
RGB
色相的调整
Android
80,471
社区成员
91,384
社区内容
发帖
与我相关
我的任务
Android
移动平台 Android
复制链接
扫一扫
分享
社区描述
移动平台 Android
android
android-studio
androidx
技术论坛(原bbs)
社区管理员
加入社区
获取链接或二维码
近7日
近30日
至今
加载中
查看更多榜单
社区公告
暂无公告
试试用AI创作助手写篇文章吧
+ 用AI写文章