BitmapFactory类

在Android中,提供的BitmapFactory类,该类为一个工具类,用于从不同的数据源解析、创建Bitmap对象,BitmapFactory类提供的创建的Bitmap对象,提供了如下方法:

<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());
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值