码迷,mamicode.com
首页 > 移动开发 > 详细

【Android自学日记】关于Bitmap的理解和使用-不完整版

时间:2017-01-19 23:10:55      阅读:428      评论:0      收藏:0      [点我收藏+]

标签:int   oid   read   exce   引用   ace   尺寸   系统   指定   

最近的Android自学刚好学习到异步线程的使用,对于开启异步线程加载网络图片中用到的Bitmap有点小蒙逼,这到底是个啥???所以我就自信的打开了百度!!以下就是我学习到的知识!

百度定义:

位图文件(Bitmap),扩展名可以是.bmp或者.dib。位图是Windows标准格式图形文件,它将图像定义为由点(像素)组成,每个点可以由多种色彩表示,包括2、4、8、16、24和32位色彩。例如,一幅1024×768分辨率的32位真彩图片,其所占存储字节数为:1024×768×32/(8*1024)=3072KB(反正就是图像文件!!)

而在我们的安卓中Bitmap是作为一个定义的,可以说Bitmap是Android系统中的图像处理的最重要类之一。用它可以获取图像文件信息,进行图像剪切、旋转、缩放等操作,并可以指定格式保存图像文件。

Bitmap的生成方式:

Bitmap实现在android.graphics包中。但是Bitmap类的构造函数是私有的,外面并不能实例化,只能是通过JNI实例化。这必然是某个辅助类提供了创建Bitmap的接口,而这个类的实现通过JNI接口来实例化Bitmap的,这个类就是BitmapFactory

1、将文件转化成Bitmap

public static final float DISPLAY_WIDTH = 200;
public static final float DISPLAY_HEIGHT = 200;
/**
 * 从path中获取图片信息
 */
private Bitmap decodeBitmap(String path){
    BitmapFactory.Options op = new BitmapFactory.Options();
    //inJustDecodeBounds 
    //If set to true, the decoder will return null (no bitmap), but the out…
    op.inJustDecodeBounds = true;
    Bitmap bmp = BitmapFactory.decodeFile(path, op); //获取尺寸信息
    //获取比例大小
    int wRatio = (int)Math.ceil(op.outWidth/DISPLAY_WIDTH);
    int hRatio = (int)Math.ceil(op.outHeight/DISPLAY_HEIGHT);
    //如果超出指定大小,则缩小相应的比例
    if(wRatio > 1 && hRatio > 1){
        if(wRatio > hRatio){
            op.inSampleSize = wRatio;
        }else{
            op.inSampleSize = hRatio;
        }
    }
    op.inJustDecodeBounds = false;
    bmp = BitmapFactory.decodeFile(path, op);
    return bmp;
}

在通过BitmapFactory.decodeFile(String path)方法将突破转成Bitmap时,遇到大一些的图片,我们经常会遇到OOM(Out Of Memory)的问题。所以用到了我们上面提到的BitmapFactory.Options这个类加载一个按比例缩小的版本到内存中(Load a Scaled Down Version into Memory)。

以下两个方法是Android提供的获取缩略图的属性:

public boolean inJustDecodeBounds

If set to true, the decoder will return null (no bitmap), but the out... fields will still be set, allowing the caller to query the bitmap without having to allocate the memory for its pixels. 
如果inJustDecodeBounds(boolean flag)的参数是true,则不返回bitmap,但是能获取bitmap的宽和高(op.outWidth和op.outWidth) 。

op.inSampleSize = ratio;

设置该属性能够实现图片的压缩

2.Bitmap对象也可以通过Stream二进制流获取 


/**
 * 从url中以流方式读取
 */
public Bitmap getBitmapFromUrl(String urlString){
        Bitmap bitmap ;
        InputStream inputStream;
        try {
            URL url = new URL(urlString);
            //返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接
            //HttpURLConnection 是支持 HTTP 特定功能的 URLConnection
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            inputStream = new BufferedInputStream(connection.getInputStream());
            try {
                //模仿网络不好的条件下加载网络
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            //博客中会写关于BitmapFactory的简单介绍
            bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

未完待续。。

【Android自学日记】关于Bitmap的理解和使用-不完整版

标签:int   oid   read   exce   引用   ace   尺寸   系统   指定   

原文地址:http://www.cnblogs.com/sev7en-/p/6308929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!