Android显示系统之Pixel、Bitmap、Drawable、Canvas、Paint和Matrix之间的联系

来源:互联网 发布:怎么投诉淘宝店家 编辑:程序博客网 时间:2024/05/17 08:55

转自:http://blog.csdn.net/conowen/article/details/7832789


1、Pixel与Bitmap 

      Pixel

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


    Bitmap 

Bitmap

extends Object
implements Parcelablejava.lang.Object   ↳android.graphics.Bitmap

1.1、定义:

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


1.2、编码方式:


RGB编码方式

  位图颜色的一种编码方法,用红、绿、蓝三原色的光学强度来表示一种颜色。这是最常见的位图编码方法,可以直接用于屏幕显示。


CMYK编码方式

  位图颜色的一种编码方法,用青、品红、黄、黑四种颜料含量来表示一种颜色。常用的位图编码方法之一,可以直接用于彩色印刷。


1.3、色彩深度

色彩深度又叫色彩位数,即位图中要用多少个二进制位来表示每个点的颜色,是分辨率的一个重要指标。常用有1位(单色),2位(4色,CGA),4位(16色,VGA),8 位(256色),16位(增强色),24位和32位(真彩色)等。色深16位以上的位图还可以根据其中分别表示RGB三原色或CMYK四原色(有的还包括 Alpha通道)的位数进一步分类,如16位位图图片还可分为R5G6B5,R5G5B5X1(有1位不携带信息),R5G5B5A1,R4G4B4A4 等等。


1.4、在这里不得不提一下矢量图:

    矢量图定义:

        矢量图[vector],也叫做向量图,简单的说,就是缩放不失真的图像格式。矢量图是通过多个对象的组合生成的,对其中的每一个对象的纪录方式,都是以数学函数来实现的,也就是说,矢量图实际上并不是象位图那样纪录画面上每一点的信息,而是纪录了元素形状及颜色的算法,当你打开一付矢量图的时候,软件对图形象对应的函数进行运算,将运算结果[图形的形状和颜色]显示给你看。无论显示画面是大还是小,画面上的对象对应的算法是不变的,所以,即使对画面进行倍数相当大的缩放,其显示效果仍然相同[不失真]。(位图缩放会失真)


(以上参考于wikipedia


1.5、在Android中得到一个Bitmap对象的方法


1.5.1、使用常用的静态方法获取Bitmap对象:

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.


1.5.2、使用BitmapFactory工厂类获取Bitmap对象

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.

1.5.3、使用BitmapDrawable获取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();

1.6、附上Bitmap与byte[]的转换关系

1.6.1、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();      }  

1.6.2、Bytes2Bitmap

static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)//Decode an immutable bitmap from the specified byte array.//解析byte[]

2、Drawable

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

Drawable 文件夹支持的图像格式有GIF、PNG、JPG,BMP。


2.1、Bitmap与Drawable的转换关系


2.1.1、Bitmap转为Drawable:

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

2.1.2、Drawable转为Bitmap

参考第一点获取Bitmap的方法1.5.3。


3、Canvas 、Paint

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

 

4、Matrix

Matrix

extends Object
java.lang.Object   ↳android.graphics.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)//旋转


原创粉丝点击