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

來源:互聯網
上載者:User

/********************************************************************************************
 * author:conowen@大鐘                                                                                                                          
 * E-mail:conowen@hotmail.com                                                                                                             
 * http://blog.csdn.net/conowen                                                                                                              
 * 註:本文為原創,僅作為學習交流使用,轉載請標明作者及出處。     

 ********************************************************************************************/

1、Pixel與Bitmap 

      Pixel

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

    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對象:

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)//旋轉

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.