BitmapFactory类
2016-07-24 13:49
441 查看
在Android中,提供的BitmapFactory类,该类为一个工具类,用于从不同的数据源解析、创建Bitmap对象,BitmapFactory类提供的创建的Bitmap对象,提供了如下方法:
写得很清楚,总结一下:
主要解析来源有四个:byteArray、File、Resource、InputStream
(1)byteArray:
(2)File:
主要有两种:decodeFile和decodeFileDescriptor,decodeFileDescriptor()更省内存
原因:查看BitmapFactory的源码,对比一下两者的实现,可以发现decodeFile()最终是以流的方式生成bitmap,decodeFileDescriptor的源码,可以找到native本地方法decodeFileDescriptor,通过底层生成bitmap
(3)Resource
解析Drawable资源中保存的图片文件并创建对应的Bitmap对象
(4)InputStream
以看到此工具类非常方便,但是加载和显示图片是很消耗内存的一件事,BitmapFactory.Options 类允许我们定义图片以何种方式如何读到内存。
inJustDecodeBounds:如果值设为true,那么将不返回实际的bitmap,也不给其分配内存空间,这样就避免了内存溢出。但是允许我们查询图片的信息,这其中就包括图片的大小信息(options.outHeight(图片的原始高度)和
options.outWidth(图片的原始宽度))。
inSampleSize:我们可以充分利用它,实现缩放。如果设置为>1,要求解码器解码出原始图片的一个子样本,返回
一个较小的bitmap,以节省空间。例如,inSampleSize==2,则取出的缩略图的宽和高都是原始图片的1/2,图片的大
小就是原始图片的1/4。对于任何值<=1的同样处置为1.
如下:
<span style="font-size:18px;">static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) Decode an immutable bitmap from the specified byte array. static Bitmap decodeByteArray(byte[] data, int offset, int length) Decode an immutable bitmap from the specified byte array. static Bitmap decodeFile(String pathName) Decode a file path into a bitmap. static Bitmap decodeFile(String pathName, BitmapFactory.Options opts) Decode a file path into a bitmap. static Bitmap decodeFileDescriptor(FileDescriptor fd) Decode a bitmap from the file descriptor. static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts) Decode a bitmap from the file descriptor. static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts) Synonym for opening the given resource and calling decodeResourceStream(Resources, TypedValue, InputStream, Rect, BitmapFactory.Options). static Bitmap decodeResource(Resources res, int id) Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options) with null Options. static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts) Decode a new Bitmap from an InputStream. static Bitmap decodeStream(InputStream is) Decode an input stream into a bitmap. static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) Decode an input stream into a bitmap.</span>
写得很清楚,总结一下:
主要解析来源有四个:byteArray、File、Resource、InputStream
(1)byteArray:
static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) //从byteArray中offset开始长度为length的位置解析,解析属性有opts static Bitmap decodeByteArray(byte[] data, int offset, int length) //从byteArray中offset开始长度为length的位置解析
(2)File:
static Bitmap decodeFile(String pathName) static Bitmap decodeFile(String pathName, BitmapFactory.Options opts) static Bitmap decodeFileDescriptor(FileDescriptor fd) static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)
主要有两种:decodeFile和decodeFileDescriptor,decodeFileDescriptor()更省内存
原因:查看BitmapFactory的源码,对比一下两者的实现,可以发现decodeFile()最终是以流的方式生成bitmap,decodeFileDescriptor的源码,可以找到native本地方法decodeFileDescriptor,通过底层生成bitmap
(3)Resource
解析Drawable资源中保存的图片文件并创建对应的Bitmap对象
static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts) //Synonym for opening the given resource and calling //decodeResourceStream(Resources, TypedValue, InputStream, Rect, //BitmapFactory.Options). static Bitmap decodeResource(Resources res, int id) //Synonym for decodeResource(Resources, int, //android.graphics.BitmapFactory.Options) with null Options. static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)
(4)InputStream
static Bitmap decodeStream(InputStream is) static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)
以看到此工具类非常方便,但是加载和显示图片是很消耗内存的一件事,BitmapFactory.Options 类允许我们定义图片以何种方式如何读到内存。
inJustDecodeBounds:如果值设为true,那么将不返回实际的bitmap,也不给其分配内存空间,这样就避免了内存溢出。但是允许我们查询图片的信息,这其中就包括图片的大小信息(options.outHeight(图片的原始高度)和
options.outWidth(图片的原始宽度))。
inSampleSize:我们可以充分利用它,实现缩放。如果设置为>1,要求解码器解码出原始图片的一个子样本,返回
一个较小的bitmap,以节省空间。例如,inSampleSize==2,则取出的缩略图的宽和高都是原始图片的1/2,图片的大
小就是原始图片的1/4。对于任何值<=1的同样处置为1.
如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv_1" android:text="Hello World!" /> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_2" android:text="hello"/> </LinearLayout>
package com.example.administrator.bitmap; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private TextView tv_1,tv_2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv_1= (TextView) findViewById(R.id.tv_1); tv_2= (TextView) findViewById(R.id.tv_2); decode(); } public void decode(){ BitmapFactory.Options options=new BitmapFactory.Options(); options.inJustDecodeBounds=true; Bitmap bitmap=BitmapFactory.decodeResource(MainActivity.this.getResources(),R.mipmap.ic_launcher,options); if(bitmap==null){ Toast.makeText(MainActivity.this,"bitmap为空",Toast.LENGTH_SHORT); } int realWidth=options.outWidth; int realHeight=options.outHeight; tv_1.setText("图片真实宽度为"+realWidth+"图片真实高度为"+realHeight); int scal=((realHeight>realWidth?realWidth:realHeight)/100); // if(scal<=0){ // scal=1; // } scal=2; options.inSampleSize=scal;// 进行scal比例的缩放 options.inJustDecodeBounds=false; bitmap=BitmapFactory.decodeResource(MainActivity.this.getResources(),R.mipmap.ic_launcher,options); if(bitmap==null){ Toast.makeText(MainActivity.this,"bitmap为空2",Toast.LENGTH_SHORT).show(); } tv_2.setText("图片缩略图高度"+bitmap.getHeight()+"宽度"+bitmap.getWidth()); } }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories