-
Android Bitmap
2015-09-17 21:18:40开发应用过程中图片的使用是必不可少的,在Android中除了使用Drawable资源中的图片,我们还可以使用Bitmap,Picture类等创建图片。 Bitmap Bitmap代表一张位图。Bitmap能能够直接创建,要通过Bitmap.Factory来...开发应用过程中图片的使用是必不可少的,在Android中除了使用Drawable资源中的图片,我们还可以使用Bitmap,Picture类等创建图片。
Bitmap
Bitmap代表一张位图。Bitmap能能够直接创建,要通过Bitmap.Factory来创建Bitmap的对象。
Bitmap.Factory中为我们提供了多个方法来获得Bitmap的对象:- decodeByteArray(byte[] data, int offset, int length)
将制定字节数组从offset字节开始length长度的字节解析成Bitmap对象。
- decodeFile(String pathName)
将指定路径下的文件解析成Bitmap对象。
- decodeFileDescriptor(FileDescriptor fd)
将FileDescriptor对应文件中解析,创建Bitmap对象。
- decodeResource(Resources res, int id)
将给定的资源ID解析成Bitmap对象。
decodeStream(InputStream is)
将指定的字节流解析成Bitmap对象。
除此之外Bitmap还提供了一些静态的方法创建Bitmap对象:createBitmap(Bitmap source, int x, int y, int width, int height)
从源位图的指定坐标(x, y)开始,挖取宽度为width,高度为height的图像创建Bitmap对象。
- createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
将源位图缩放成宽度为dstWidth,高度为dstHeight的Bitmap的对象。
- createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
从源位图的(x, y)坐标开始,挖取宽度为width,高度为height的图像并按照Matrix的规定设置为型的Bitmap对象。
Bitmap与BiamapDrawable
BitmapDrawable中封装的就是一个Bitmap对象:
BitmapDrawable drawable = new BitmapDrawable (bitmap);
通过调用BitmapDrawable的getBitmap()方法获得BitmapDrawable中封装的Bitmap对象。
Bitmap bitmap= drawable.getBitmap();
- decodeByteArray(byte[] data, int offset, int length)
-
android Bitmap
2011-09-22 18:25:391)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap main.xml文件如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical...1)从android的资源文件夹layout中加载xml布局文件,并把布局文件映射为Bitmap
main.xml文件如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="256px" android:layout_height="256px"> <TextView android:layout_width="wrap_content" android:id="@+id/city" android:layout_height="wrap_content" android:textSize="20px" android:textColor="#ffffff" android:shadowColor="#0000AA" android:shadowDx="0" android:shadowDy="-2" android:shadowRadius="0.1" android:layout_gravity="right" android:layout_marginRight="5px" /> </LinearLayout>
java代码中的处理,方法一:
//加载xml布局文件 LayoutInflater factory = LayoutInflater.from(context); View view = factory.inflate(R.layout.main, null); //获得布局文件中的TextView TextView city = (TextView) view.findViewById(R.id.city); //设置city的文本信息 city.setText("xml中的textview"); //启用绘图缓存 view.setDrawingCacheEnabled(true); //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY)); //这个方法也非常重要,设置布局的尺寸和位置 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); //获得绘图缓存中的Bitmap view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache();
java代码中的处理,方法二:
//加载xml布局文件 LayoutInflater factory = LayoutInflater.from(context); View view = factory.inflate(R.layout.main, null); //获得布局文件中的TextView TextView city = (TextView) view.findViewById(R.id.city); //设置city的文本信息 city.setText("xml中的textview"); //调用下面这个方法非常重要,如果没有调用这个方法,得到的bitmap为null view.measure(MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY), MeasureSpec.makeMeasureSpec(256, MeasureSpec.EXACTLY)); //这个方法也非常重要,设置布局的尺寸和位置 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); //生成bitmap Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.RGB_565); //利用bitmap生成画布 Canvas canvas = new Canvas(bitmap); //把view中的内容绘制在画布上 view.draw(canvas);
2)Bitmap转换为byte[]数组方法一:
private byte[] Bitmap_To_Bytes(Bitmap bitmap){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
方法二:
public static byte[] readStream(InputStream inStream) throws Exception { byte[] buffer = new byte[1024]; int len = -1; ByteArrayOutputStream baos = new ByteArrayOutputStream(); while ((len = inStream.read(buffer)) != -1) { baos .write(buffer, 0, len); } byte[] data = baos .toByteArray(); baos .close(); inStream.close(); return data; }
3)设置在应用中支持32位的图像:在onCreate()函数中加入以下两行:
getWindow().setFormat(PixelFormat.RGBX_8888); BitmapFactory.setDefaultConfig(Bitmap.Config.ARGB_8888);
以使之 支持32bit的图像。4)把drawable文件夹下的文件转成Bitmap
Bitmap bm = BitmapFactory.decodeResource(getApplicationContext().getResources(), R.drawable.down);
-
android jni AndroidBitmap_lockPixels
2017-05-10 03:12:55AndroidBitmap_lockPixels(env, bitmap, (void**)&pixels); android java中 int[] pixels = new int[bitmap.getWidth()*bitmap.getHeight()]; bitmap.getPixels(pixels,0,bitmap.getWidth(),0,0,bitmap.getWidth(),... -
Android bitmap bytearray转换
2016-05-31 12:51:20Android bitmap bytearray转换 bitmap转byte public static byte[] bitmapToByte(Bitmap b) { ByteArrayOutputStream o = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG, 100, o); reAndroid bitmap bytearray转换
bitmap转byte public static byte[] bitmapToByte(Bitmap b) { ByteArrayOutputStream o = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.PNG, 100, o); return o.toByteArray(); } bytearray 转bitmap public static Bitmap byteToBitmap(byte[] b) { return (b == null || b.length == 0) ? null : BitmapFactory.decodeByteArray(b, 0, b.length); }
-
Android Bitmap位图
2013-08-30 15:27:27Android Bitmap位图 1. Bitmap 对象只能通过BitmapFactory创建,BitmapFactory提供了静态方法decodeXXXXX(****)方法分别将五中资源加载成Bitmap对象中。 2. BitmapFactory生成Bitmap对象也是调用底层的JNI接口...Android Bitmap位图
1. Bitmap 对象只能通过BitmapFactory创建,BitmapFactory提供了静态方法decodeXXXXX(****)方法分别将五种资源加载成Bitmap对象中。2. BitmapFactory生成Bitmap对象是调用底层的JNI接口,所以在加载Bitmap图片这个过程中会包含两部分的内存区域,Java部分和C部分。 Java部分的会通过系统的垃圾回收机制自动回收,但是C部分的就不能自动回收了,所以Bitmap使用结束要调用recycle()方法释放C部分的内存。recycle()也是调用JNI接口实现。3. 读取Bitmap位图时,Android系统分配给虚拟机中的堆大小只有8M,所以Bitmap对象用完一定的及时回收。
4. Android 3.0后对Bitmap图片加载又对图片大小有限制了,主要是是硬件加速器对图片大小的限制,如果加载图片大于了这个限制就不能加载,提示Bitmap too large to be uploaded into a texture exception。这个限制不同的机子不一样,据说最小的是2048 * 2048。现在有一个粗暴的解决办法:<application android:hardwareAccelerated="false" ...> 关掉硬件加速器,但我个人认为这样不太好,如果有更好的解决方案的请留言。// 释放bitmap图片 if (bitmap != null && bitmap.isRecycled()) { // 释放C部分占用的内存 bitmap.recycle(); // 加快Java部分的内存回收 bitmap = null; } // 加快Java回收机制对Java部分中垃圾的回收,不一定马上回收,根据回收算法决定 System.gc();
-
android Bitmap内存优化(一) Bitmap 详解
2015-08-22 21:35:42Bitmap 详解 图片压缩 Android Bitmap内存优化 -
android bitmap 获取像素
2015-07-23 15:08:29android bitmap 获取像素并可以重新创建bitmap Bitmap b = Bitmap.createBitmap(100, 100, Config.ARGB_4444); b.setPixel(99, 99, Color.WHITE); int[] buf = new int[b.getRowBytes()/2*b.getHeight()]; try... -
Android Bitmap 蒙版使用
2014-04-22 11:14:46android bitmap 蒙版效果可以使用Paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN)); -
android Bitmap学习总结
2010-08-24 11:34:00android Bitmap学习总结 -
Android Bitmap与Canvas
2010-08-01 16:31:00Android Bitmap与Canvas -
undefined reference to 'AndroidBitmap_getInfo'
2016-11-07 17:25:54在使用NDK进行图像处理的时候或许会用到Android/bitmap.h当中的内容,例如AndroidBitmap_getInfo/AndroidBitmap_lockPixels/AndroidBitmap_unlockPixels等等。 我在eclipse中集成NDK的项目中使用了这些内容... -
android bitmap保存为文件及读取
2016-07-23 11:30:46android bitmap保存为文件及读取 -
Android Bitmap inBitmap 图片复用
2016-06-01 18:21:21原文链接:Android Bitmap inBitmap 图片复用? 主要就是指的复用内存块,不需要在重新给这个bitmap申请一块新的内存,避免了一次内存的分配和回收,从而改善了运行效率。 需要注意的是inBitmap只能在3.0... -
android bitmap和base64之间的转换
2017-11-14 10:27:59android bitmap和base64之间的转换 -
Android Bitmap使用
2018-04-12 11:17:16不过如果图片处理不当就会造成内存溢出(OOM),所以了解Bitmap相关用法就有必要了,Bitmap在Android中指的是一张图片,图片类型可以是png、jpg等。1、BitmapFactoryBitmapFactory进一步封装了获取Bitmap对象,... -
Android Studio JNI 报错 - error: undefined reference to 'AndroidBitmap_getInfo'
2016-12-08 09:09:11学习JNI遇到了点坑 网上也能找到...error: undefined reference to 'AndroidBitmap_getInfo' error: undefined reference to 'AndroidBitmap_lockPixels' error: undefined reference to 'AndroidBitmap_unlockPix -
Android Bitmap优化
2018-07-10 10:10:10概述 在日常开发中我们经常遇到加载图片报出oom的错误,我们...android 中图片是以bitmap的形式存在的,那么bitmap中所占的内存,直接影响到了是否oom,我们了解一下bitmap的占用内存的计算方法 图片的长度 * 图片... -
Android Bitmap 和 ByteArray的互相转换
2018-03-29 14:14:12原文地址:https://www.cnblogs.com/psklf/p/5889978.htmlAndroid Bitmap 和 ByteArray的互相转换移动平台图像处理,需要将图像传给native处理,如何传递?将bitmap转换成一个 byte[] 方便传递也方便cpp代码直接... -
Android Bitmap转换
2018-07-25 14:30:571、将Bitmap对象读到字节数组中 ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); byte[] datas = baos.toByteArray(); 2、将字节数组... -
Android Studio 下安卓 jni 开发错误 undefined reference to AndroidBitmap_getInfo
2016-03-26 20:47:28Android Studio 下安卓 jni 开发错误 undefined reference to AndroidBitmap_getInfo -
AndroidStudio报错: undefined reference to 'AndroidBitmap_getInfo'
2017-04-03 21:35:18Error:(425) undefined reference to AndroidBitmap_getInfo' Error:(440) undefined reference toAndroidBitmap_lockPixels’ 解决方法: 在CMakeLists.txt文件中加入target_link_libraries( # Specifies the ... -
Android bitmap 释放 解决Error create Hprof file问题
2016-12-23 12:25:06Android bitmap 释放实战, 解决Error create Hprof file问题 -
Android Bitmap 变色
2015-06-09 17:56:41一张图是 a*b 像素, 即有 a*b个像素点 ...private Bitmap sharkColor(Bitmap mBitmap) { int mBitmapWidth = mBitmap.getWidth(); int mBitmapHeight = mBitmap.getHeight(); int mArrayColorL -
Android Bitmap用法大全,以后再也不担心了
2014-08-22 14:56:42Android Bitmap用法大全,以后再也不担心了 -
Android Bitmap压缩策略
2020-02-27 21:58:24现在的高清大图,动辄就要好几M,而Android对单个应用所施加的内存限制,只有 小几十M,如16M,这导致加载Bitmap的时候很容易出现内存溢出。如下异常信 息,便是在开发中经常需要的: java.lang.OutofMemoryError:... -
android Bitmap的问题
2017-04-07 13:54:40Attempt to invoke virtual method 'boolean android.graphics.Bitmap.isRecycled()' on a null 这个问题怎么解决。求大神 -
android BitMap回收
2015-06-22 11:38:20bitmap在android中使用较多,但是如果不对其进行回收,将会导致内存问题。 【第一种方法】及时回收bitmap内存: 一般而言,回收bitmap内存可以用到以下代码 if(bitmap != null && !bitmap.isRecycled()){ ... -
Error:(32) android studio开发,报错undefined reference to `AndroidBitmap_getInfo'
2015-06-11 11:13:34Error:(32) undefined reference to `AndroidBitmap_getInfo' Error:(38) undefined reference to `AndroidBitmap_lockPixels' Error:(50) undefined reference to `AndroidBitmap_unlockPixels' collect2 -
android bitmap out of memory总结、心得
2013-08-17 16:47:07android bitmap out of memory总结、心得
收藏数
22,553
精华内容
9,021
-
三轮生肖发行数量(2004-2015)
-
mac 如何查看 java程序的进程id ?
-
1.函数延时.rar
-
MMM 集群部署实现 MySQL 高可用和读写分离
-
2020美团技术年货-算法篇.pdf
-
【硬核】一线Python程序员实战经验分享(1)
-
项目管理工具与方法
-
7.1声卡主持效果.rar
-
弹窗提示 运行之后 右键单击可退出
-
c++DFs<体积>.txt
-
虚拟执行环境工具.zip
-
基于电商业务的全链路数据中台落地方案(全渠道、全环节、全流程)
-
java实现es创建索引和索引设置
-
龙芯实训平台应用实战(希云)
-
Galera 高可用 MySQL 集群(PXC v5.6 + Ngin
-
Galera 高可用 MySQL 集群(PXC v5.7+Hapro)
-
Wide Deep Learning for Recommender Systems.pdf
-
composite模式
-
okex_swap_orderbook.BTCUSDT.20200415.csv.tar.gz
-
六合一gif格式转换器工具箱 | 极品反向GIF制作工具