android.graphics.Bitmap 一

7 篇文章 0 订阅
7 篇文章 0 订阅

    学习android,总感觉做了很多东西,回头望望又发现自己其实也没涉及多少方面,想想干脆总结或者说翻译下sdk的东东。

    讲到Bitmap,不能不提BItmapFactory,BItmapFactory在sdk上的简介是从包括文件,数据流,数组的资源中生成Bitmap对象.它的功能也的确实如此,它里边除了一个内部类BitmapFactory.Options之外就全是静态函数,全部是用来生成Bitmap的.

static Bitmap  decodeFile(String pathName) 
static Bitmap  decodeFile(String pathName, BitmapFactory.Options opts) 

这两个函数是从文件系统中读取图片,用法如下

       Bitmap b1;

       b1=BitmapFactory.decodeFile("/mnt/sdcard/audio/ic_launcher.png");
这是模拟器中的读取方法,在手机上读取sdcard上的文件应该是

       b1=BitmapFactory.decodeFile("/sdcard/audio/ic_launcher.png");

static Bitmap  decodeByteArray(byte[] data, int offset, int length) 
static Bitmap  decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts) 

这两个函数是从字节数组中生成Bitmap,函数调用没什么特别的,关键是字节数组怎么来,目前发现这两种方法生成:
       ByteArrayOutputStream os=new ByteArrayOutputStream();
       b1.compress(Bitmap.CompressFormat.JPEG, 10, os);
       byte data[]=os.toByteArray();
       b2=BitmapFactory.decodeByteArray(data, 0, data.length);
这种方法是把OutputStream转化成byte数组,另一种方法是把InputStream转化成OutputStream然后再转化成byte数组,因为从资源中读取出来的都是InputStream,然而BitmapFactory提供了从InputStream生成Bitmap的函数,所以这种方法其实没多少必要,但是也看一下:
       is=context.getResources().openRawResource(R.drawable.gallery_photo_5);
       ByteArrayOutputStream os2=new ByteArrayOutputStream(1024);
       byte buffer[]=new byte[1024];
       int len;
          try {
              while ((len = is.read(buffer)) >= 0) {
                  os2.write(buffer, 0, len);
              }
          } catch (java.io.IOException e) {
          }
          byte data2[]=os2.toByteArray();
          b3=BitmapFactory.decodeByteArray(data2, 0, data2.length);
中间的offset一般是0,但是有时候也不一定,要看数组data的内容,有时候data的可能不全是图片信息。

static Bitmap  decodeFileDescriptor(FileDescriptor fd) 
static Bitmap  decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)
这两个函数跟decodeFile是差不多的,都是从文件系统中获取图片,只不过一个是通过路径,一个是文件描述符,应用如下:
          FileInputStream fi;
          FileDescriptor fd=null;
          try{
          fi=new FileInputStream("/mnt/sdcard/audio/ic_launcher.png");
          fd=fi.getFD();
          }catch(java.io.IOException e)
          {
          
          }
        if(fd!=null)        
          b3=BitmapFactory.decodeFileDescriptor(fd); 
第2个函数多了一个Rect outPadding,这个矩形是用来给Bitmap与其他元素分开的间隔用的,就是padding.

static Bitmap  decodeResource(Resources res, int id, BitmapFactory.Options opts) 
static Bitmap  decodeResource(Resources res, int id) 

这两个函数的调用简单,如下:
          b1=BitmapFactory.decodeResource(context.getResources(), R.drawable.gallery_photo_5);

static Bitmap  decodeStream(InputStream is)      
static Bitmap  decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts) 

这两个函数调用跟上两个差不多,如下:
       is=context.getResources().openRawResource(R.drawable.gallery_photo_5);
       b1=BitmapFactory.decodeStream(is);
在讲byte数组如何生成的时候其实就已经可以用这个函数生成Bitmap了

static Bitmap  decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts) 
 这个函数跟 decodeStream差不多, res,value参数都是为了给opts赋值的,其参考代码如下
    public static Bitmap decodeResourceStream(Resources res, TypedValue value,
            InputStream is, Rect pad, Options opts) {

        if (opts == null) {
            opts = new Options();
        }

        if (opts.inDensity == 0 && value != null) {
            final int density = value.density;
            if (density == TypedValue.DENSITY_DEFAULT) {
                opts.inDensity = DisplayMetrics.DENSITY_DEFAULT;
            } else if (density != TypedValue.DENSITY_NONE) {
                opts.inDensity = density;
            }
        }
       
        if (opts.inTargetDensity == 0 && res != null) {
            opts.inTargetDensity = res.getDisplayMetrics().densityDpi;
        }
       
        return decodeStream(is, pad, opts);
    }
用法如下
        final TypedValue value = new TypedValue(); 
        is=context.getResources().openRawResource(R.drawable.gallery_photo_5,value);
        b1=BitmapFactory.decodeResourceStream(context.getResources(), value, is, null, null);

BitmapFactory基本就这些函数,全是用来生成Bitmap的,不愧为它的字面意思,生产Bitmap的工厂。
全部代码如下

package com.android.blog;

import android.view.View;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.TypedValue;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileDescriptor;
public class BlogView extends View{
Bitmap b1,b2,b3;
InputStream is;
public BlogView(Context context)
{
       super(context);
       b1=BitmapFactory.decodeFile("/mnt/sdcard/audio/ic_launcher.png");
          b1=BitmapFactory.decodeResource(context.getResources(), R.drawable.gallery_photo_5); 
       ByteArrayOutputStream os=new ByteArrayOutputStream();
       b1.compress(Bitmap.CompressFormat.JPEG, 10, os);
       byte data[]=os.toByteArray();
       b2=BitmapFactory.decodeByteArray(data, 0, data.length);
       is=context.getResources().openRawResource(R.drawable.gallery_photo_5);
       b1=BitmapFactory.decodeStream(is);
       ByteArrayOutputStream os2=new ByteArrayOutputStream(1024);
       byte buffer[]=new byte[1024];
       int len;
          try {
              while ((len = is.read(buffer)) >= 0) {
                  os2.write(buffer, 0, len);
              }
          } catch (java.io.IOException e) {
          }
          byte data2[]=os2.toByteArray();
          b3=BitmapFactory.decodeByteArray(data2, 0, data2.length);
          FileInputStream fi;
          FileDescriptor fd=null;
          Rect r1=new Rect(10,10,20,20);
          try{
          fi=new FileInputStream("/mnt/sdcard/audio/ic_launcher.png");
          fd=fi.getFD();
          }catch(java.io.IOException e)
          {
          
          }
        if(fd!=null)        
          b3=BitmapFactory.decodeFileDescriptor(fd,r1,null);
        final TypedValue value = new TypedValue(); 
        is=context.getResources().openRawResource(R.drawable.gallery_photo_5,value);
        b1=BitmapFactory.decodeResourceStream(context.getResources(), value, is, null, null);
}
@Override
protected void onDraw(Canvas canvas)
{
 Paint p=new Paint();
 canvas.drawColor(Color.WHITE);
 canvas.drawBitmap(b1, 10, 10, p);
 canvas.drawBitmap(b2, 50, 50, p);
 canvas.drawBitmap(b3, 100, 100, p); 

}
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值