Pixel,Bitmap,Drawable,Canvas,Paint,Matrix,BitmapDrawable,BitmapFactory图相关

Pixel

      像素,又称画素,为图像显示的基本单位。每个像素可有各自的颜色值,可采用三原色显示,因而又分成红、绿、蓝三种子像素(RGB色域),或者青、品红、黄和黑(CMYK色域,印刷行业以及打印机中常见)。照片是一个个采样点的集合,故而单位面积内的像素越多代表解析度越高,所显示的图像就会接近于真实物体。由像素组成的图像称为Bitmap(位图)。通常来说,对于一个显示屏幕,一个点就对应一个像素。


Bitmap

称作位图,又称栅格图(英语:Raster graphics)或称点阵图,是使用像素阵列来表示的图像,每个像素的颜色信息由RGB组合或者灰度值表示。根据颜色信息所需的数据位分为1、4、8、16、24及32位等,位数越高颜色越丰富,相应的数据量越大。其中使用1位表示一个像素颜色的位图因为一个数据位只能表示两种颜色,所以又称为二值位图。通常使用24位RGB组合数据位表示的的位图称为真彩色位图。一般来说,位图是没有经过压缩的,位图文件体积比较大。(位图常用的压缩算法是通过“索引颜色表”实现的),位图大多支持alpha通道(透明通道)。

常用方法:
static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)  
//Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.   
static Bitmap     createBitmap(int width, int height, Bitmap.Config config)  
//Returns a mutable bitmap with the specified width and height.   
static Bitmap     createBitmap(Bitmap source, int x, int y, int width, int height)  
//Returns an immutable bitmap from the specified subset of the source bitmap.   
static Bitmap     createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)  
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.   
static Bitmap     createBitmap(Bitmap src)  
//Returns an immutable bitmap from the source bitmap.   
static Bitmap     createBitmap(int[] colors, int width, int height, Bitmap.Config config)  
//Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.   
static Bitmap     createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)  
//Creates a new bitmap, scaled from an existing bitmap, when possible.  

从资源文件中获取:Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); 

Drawable转化为Bitmap:
private Bitmap bitmap;
private void drawableToBitamp(Drawable drawable)
    {
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        System.out.println("Drawable转Bitmap");
        Bitmap.Config config = 
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565;
        bitmap = Bitmap.createBitmap(w,h,config);
        //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
        Canvas canvas = new Canvas(bitmap);   
        drawable.setBounds(0, 0, w, h);   
        drawable.draw(canvas);
    }

另外一种思想:

public static Bitmap drawableToBitmap(Drawable drawable) {        
        Bitmap bitmap = Bitmap
                        .createBitmap(
                                        drawable.getIntrinsicWidth(),
                                        drawable.getIntrinsicHeight(),
                                        drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                                                        : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        //canvas.setBitmap(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
}

从资源文件中得到Bitmap:
private Bitmap bitmap;
private void drawableToBitamp(Drawable drawable)
    {
        BitmapDrawable bd = (BitmapDrawable) drawable;

        bitmap = bd.getBitmap();
    }

BitmapFactory

工厂类是一个工具类,提供了大量的方法,大多数是从不同的数据源来解码、创建Bitmap对象

static Bitmap   decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)  
//Decode an immutable bitmap from the specified byte array.   
//解析byte[]   
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) will 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.  


BitmapDrawable

继承于Drawable
//方法一   
Resources res;  
InputStream is=res.openRawResource(R.drawable.pic);  
BitmapDrawable bitmapDrawable=new BitmapDrawable(is);  
Bitmap bmp=bitmapDrawable.getBitmap();    
//方法二   Resources res;  
BitmapDrawable bitmapDrawable=(BitmapDrawable)res.getDrawable(R.drawable.pic);  
Bitmap bmp=bitmapDrawable.getBitmap();    
//方法三   

ImageView image;  
image.setImageBitmap(BitmapFactory.decodeStream(~~~~));  
BitmapDrawable bitmapDrawable=(BitmapDrawable)image.getDrawable();  
Bitmap bmp=bitmapDrawable.getBitmap();  


Drawable

当在Android工程的Drawable文件夹中导入图像文件时,Android SDK会为这个文件生成一个Drawable对象。可以通过R.drawable的方式访问这个对象。一般是调用Resource.getDrawable(int id)的方式直接获取。
Drawable 文件夹支持的图像格式有GIF、PNG、JPG,BMP。


Bitmap转为Drawable:
BitmapDrawable bitmapDrawable= new BitmapDrawable(bitmap)   
 因为BtimapDrawable是Drawable的子类,最终直接使用bitmapDrawable即可。  

bitmap转为Canvas,Canvas转为Drawable

Canvas canvas = new Canvas(bitmap);   
drawable.setBounds(0, 0, w, h);   
drawable.draw(canvas);


Canvas Paint
理解Canvas对象,可以把它当做画布,Canvas的方法大多数是设置画布的大小、形状、画布背景颜色等等,要想在画布上面画画,一般要与Paint对象结合使用,顾名思义,Paint就是画笔的风格,颜料的色彩之类的。


Matrix
Matrix为矩阵的意思,一般用来与Bitmap配合,实现图像的缩放、变形、扭曲等操作。
public static Bitmap scaleBitmap(Bitmap bitmap, int scalWidth, int scaleHeight) {    
        int w = bitmap.getWidth();    
        int h = bitmap.getHeight();    
        // 创建操作图片用的Matrix对象     
        Matrix matrix = new Matrix();    
        // 计算缩放比例     
        float sx= ((float) scaleWidth / w);    
        float sy= ((float) scaleHeight / h);    
        // 设置缩放比例     
        matrix.postScale(sx, sy);    
        // 建立新的bitmap,其内容是对原bitmap的缩放后的图    
        Bitmap scaleBmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);    
        return scaleBmp;    
    }    
Matrix类的其他典型方法。
boolean  postScale(float sx, float sy)//缩放   
boolean     postSkew(float kx, float ky)//扭曲   
boolean     postTranslate(float dx, float dy)//转换   
boolean     preConcat(Matrix other)//合并   
boolean     preRotate(float degrees)//旋转  



Bitmap2Bytes
public byte[] Bitmap2Bytes(Bitmap bmp) {    
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();    
        //public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)   
        bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream );    
        return byteArrayOutputStream.toByteArray();    
    }    

另外一种兼容代码:

private byte[] Bitmap2Bytes(Bitmap bm){
 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
 bm.compress(Bitmap.CompressFormat.PNG, 100, baos); 
 return baos.toByteArray();
 }
Bytes2Bitmap
static Bitmap   decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)  
//Decode an immutable bitmap from the specified byte array.   
//解析byte[]  

另外一种兼容代码:

private Bitmap Bytes2Bimap(byte[] b){
   if(b.length!=0){
    return BitmapFactory.decodeByteArray(b, 0, b.length);
   }
   else {
    return null;
   }
 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值