歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android顯示系統之Pixel、Bitmap、Drawable、Canvas、Paint和Matrix之間的聯系

Android顯示系統之Pixel、Bitmap、Drawable、Canvas、Paint和Matrix之間的聯系

日期:2017/3/1 10:13:38   编辑:Linux編程

1、Pixel與Bitmap

Pixel

像素,又稱畫素,為圖像顯示的基本單位。每個像素可有各自的顏色值,可采用三原色顯示,因而又分成紅、綠、藍三種子像素(RGB色域),或者青、品紅、黃和黑(CMYK色域,印刷行業以及打印機中常見)。照片是一個個采樣點的集合,故而單位面積內的像素越多代表解析度越高,所顯示的圖像就會接近於真實物體。由像素組成的圖像稱為Bitmap(位圖)。通常來說,對於一個顯示屏幕,一個點就對應一個像素。(關於像素的DPI還有布局中的dip、px等等參看下一篇博文 http://www.linuxidc.com/Linux/2012-08/67619.htm )


Bitmap

Bitmap

extends Object
implements Parcelable java.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對象:

  1. static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
  2. //Returns an immutable bitmap from subset of the source bitmap, transformed by the optional matrix.
  3. static Bitmap createBitmap(int width, int height, Bitmap.Config config)
  4. //Returns a mutable bitmap with the specified width and height.
  5. static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height)
  6. //Returns an immutable bitmap from the specified subset of the source bitmap.
  7. static Bitmap createBitmap(int[] colors, int offset, int stride, int width, int height, Bitmap.Config config)
  8. //Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
  9. static Bitmap createBitmap(Bitmap src)
  10. //Returns an immutable bitmap from the source bitmap.
  11. static Bitmap createBitmap(int[] colors, int width, int height, Bitmap.Config config)
  12. //Returns a immutable bitmap with the specified width and height, with each pixel value set to the corresponding value in the colors array.
  13. static Bitmap createScaledBitmap(Bitmap src, int dstWidth, int dstHeight, boolean filter)
  14. //Creates a new bitmap, scaled from an existing bitmap, when possible.

1.5.2、使用BitmapFactory工廠類獲取Bitmap對象

BitmapFactory工廠類是一個工具類,提供了大量的方法,大多數是從不同的數據源來解碼、創建Bitmap對象,典型方法如下。

  1. static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)
  2. //Decode an immutable bitmap from the specified byte array.
  3. //解析byte[]
  4. static Bitmap decodeByteArray(byte[] data, int offset, int length)
  5. //Decode an immutable bitmap from the specified byte array.
  6. static Bitmap decodeFile(String pathName)
  7. //Decode a file path into a bitmap.
  8. static Bitmap decodeFile(String pathName, BitmapFactory.Options opts)
  9. //Decode a file path into a bitmap.
  10. static Bitmap decodeFileDescriptor(FileDescriptor fd)
  11. //Decode a bitmap from the file descriptor.
  12. static Bitmap decodeFileDescriptor(FileDescriptor fd, Rect outPadding, BitmapFactory.Options opts)
  13. //Decode a bitmap from the file descriptor.
  14. static Bitmap decodeResource(Resources res, int id, BitmapFactory.Options opts)
  15. //Synonym for opening the given resource and calling decodeResourceStream(Resources, TypedValue, InputStream, Rect, BitmapFactory.Options).
  16. static Bitmap decodeResource(Resources res, int id)
  17. //Synonym for decodeResource(Resources, int, android.graphics.BitmapFactory.Options) will null Options.
  18. static Bitmap decodeResourceStream(Resources res, TypedValue value, InputStream is, Rect pad, BitmapFactory.Options opts)
  19. //Decode a new Bitmap from an InputStream.
  20. static Bitmap decodeStream(InputStream is)
  21. //Decode an input stream into a bitmap.
  22. static Bitmap decodeStream(InputStream is, Rect outPadding, BitmapFactory.Options opts)
  23. //Decode an input stream into a bitmap.

1.5.3、使用BitmapDrawable獲取Bitmap對象

BitmapDrawable繼承於Drawable

  1. //方法一
  2. Resources res;
  3. InputStream is=res.openRawResource(R.drawable.pic);
  4. BitmapDrawable bitmapDrawable=new BitmapDrawable(is);
  5. Bitmap bmp=bitmapDrawable.getBitmap();
  6. //方法二
  7. Resources res;
  8. BitmapDrawable bitmapDrawable=(BitmapDrawable)res.getDrawable(R.drawable.pic);
  9. Bitmap bmp=bitmapDrawable.getBitmap();
  10. //方法三
  11. ImageView image;
  12. image.setImageBitmap(BitmapFactory.decodeStream(~~~~));
  13. BitmapDrawable bitmapDrawable=(BitmapDrawable)image.getDrawable();
  14. Bitmap bmp=bitmapDrawable.getBitmap();

1.6、附上Bitmap與byte[]的轉換關系

1.6.1、Bitmap2Bytes

  1. public byte[] Bitmap2Bytes(Bitmap bmp) {
  2. ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
  3. //public boolean compress (Bitmap.CompressFormat format, int quality, OutputStream stream)
  4. bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream );
  5. return byteArrayOutputStream.toByteArray();
  6. }
1.6.2、Bytes2Bitmap
  1. static Bitmap decodeByteArray(byte[] data, int offset, int length, BitmapFactory.Options opts)
  2. //Decode an immutable bitmap from the specified byte array.
  3. //解析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:

  1. BitmapDrawable bitmapDrawable= new BitmapDrawable(bitmap)
  2. 因為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配合,實現圖像的縮放、變形、扭曲等操作。

  1. public static Bitmap scaleBitmap(Bitmap bitmap, int scalWidth, int scaleHeight) {
  2. int w = bitmap.getWidth();
  3. int h = bitmap.getHeight();
  4. // 創建操作圖片用的Matrix對象
  5. Matrix matrix = new Matrix();
  6. // 計算縮放比例
  7. float sx= ((float) scaleWidth / w);
  8. float sy= ((float) scaleHeight / h);
  9. // 設置縮放比例
  10. matrix.postScale(sx, sy);
  11. // 建立新的bitmap,其內容是對原bitmap的縮放後的圖
  12. Bitmap scaleBmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
  13. return scaleBmp;
  14. }

Matrix類的其他典型方法。

  1. boolean postScale(float sx, float sy)//縮放
  2. boolean postSkew(float kx, float ky)//扭曲
  3. boolean postTranslate(float dx, float dy)//轉換
  4. boolean preConcat(Matrix other)//合並
  5. boolean preRotate(float degrees)//旋轉
Copyright © Linux教程網 All Rights Reserved