在Android中,圖像主要分為兩部分:2D圖像和3D圖像,2D圖像的實現(xiàn)主要位于android.graphics、android.graphics.drawable和android.view.animation、android. animation等包中,特別說明的是,Android有一種名為“NinePatch”、后綴為.9.png的圖像形態(tài),可以支持圖像的特定縮放,對開發(fā)通用應用的開發(fā)者而言特別需要關注。如果希望制作NinePatch圖像,Android提供了draw9patch工具進行支持。3D圖像是基于OpenGL ES來實現(xiàn)的,增加了部分Android接口,本文重點介紹的是2D圖像的應用。
關于圖像的處理,相關的接口主要位于android.graphics和android.graphics.drawable兩個包中。主要的類包括:Bitmap、BitmapFactory、Camera、Picture、AnimationDrawable、BitmapDrawable、PictureDrawable、RotateDrawable 、ScaleDrawable等。
關于動畫的處理,相關的接口主要位于android.view.animation和android.animation中,主要的類包括ValueAnimator、ObjectAnimator、AnimatorSet、Evaluators、Interpolators、AnimationSet、Animation 、AnimationDrawable等。
1.Bitmap
Bitmap類提供了圖像的基本操作,如獲取圖像基本信息、像素操作等。常用的方法包括:
getHeight() //獲取高度
getWidth() //獲取寬度
getPixel(int x, int y) //獲取某一點的像素
setPixel(int x, int y, int color) //設置某一點的像素
writeToParcel(Parcel p, int flags) //將圖像寫入Parcel
createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) //創(chuàng)建圖像
Bitmap支持的RGB格式在Bitmap.Config中定義,目前支持的RGB格式包括ALPHA_8、RGB_565、ARGB_4444、ARGB_8888等。
2.BitmapFactory
BitmapFactory類對開發(fā)者從文件、流、字節(jié)組、資源文件中解碼圖像提供了支持。常用的方法包括:
decodeByteArray(byte[] data, int offset, int length) //從字節(jié)碼中解碼
decodeFile(String pathName) //從文件中解碼
decodeStream(InputStream is) //從輸入流中解碼
decodeResource(Resources res, int id) //從資源文件中解碼
另外,和BitmapFactory經(jīng)常配合使用的還有BitmapFactory.Options類,其可以配置將一個圖像讀到內(nèi)存中的方式。BitmapFactory.Options類的用法如下:
Options opts = new BitmapFactory.Options();
opts.inDither = true;
opts.inPreferredConfig = Bitmap.Config.RGB_565;
mWood = BitmapFactory.decodeResource(getResources(), R.drawable.wood, opts);
3.Camera
此Camera非攝像頭,而是位于android.graphics包裝的一個圖像類,Camera類主要用來執(zhí)行旋轉(zhuǎn)操作。常用的方法包括:
rotateX(float deg) //X軸旋轉(zhuǎn)
rotateY(float deg) //Y軸旋轉(zhuǎn)
rotateZ(float deg) //Z軸旋轉(zhuǎn)
translate(float x, float y, float z) //X、Y、Z軸3個方向旋轉(zhuǎn)
4.Picture
Picture類主要用于繪制圖像。其常用的方法為:
beginRecording(int width, int height) //開始繪制
endRecording() //結(jié)束繪制
createFromStream(InputStream stream) //從流中創(chuàng)建
writeToStream(OutputStream stream) //將圖像寫入流
5.AnimationDrawable
AnimationDrawable常用于實現(xiàn)幀動畫,通常用于設置動態(tài)背景。其常用的方法為:
addFrame(Drawable frame, int duration) //向動畫中添加一幀
getFrame(int index) //獲取動畫中的一幀
isRunning() //判斷動畫是否正在播放
setOneShot(boolean oneShot) //設置是否循環(huán)播放
setVisible(boolean visible, boolean restart) //設置可視性
start() //播放動畫
stop() //停止播放動畫
AnimationDrawable播放的圖像集通常由AnimationSet定義。
6.BitmapDrawable
BitmapDrawable封裝了一個Bitmap圖像,開發(fā)者可以從一個文件路徑、輸入流、XML 加載一個Bitmap對象。
從BitmapDrawable中獲取Bitmap圖像的方法很簡單,具體如下:
Bitmap bitmap = bitmapDrawable.getBitmap();
從資源文件中獲取BitmapDrawable對象的方法如下:
InputStream source = getResources().openRawResource(R.raw.testimage);
BitmapDrawable bitmapDrawable = new BitmapDrawable(source);
BitmapDrawable繼承于Drawable,Drawable抽象一點說,是一個可以渲染的對象,通常可以從中抽取資源渲染到屏幕上,Drawable可以對Bitmap、Nine Patch、Shape、Layers、States、Levels、Scale等對象進行封裝?梢栽O置渲染對象的透明度、邊界(bounds)、顏色過濾器(colorFilter)等。是一個十分強大的類。
7.PictureDrawable
PictureDrawable封裝了一個圖像?梢栽O置透明度、圖像、顏色過濾器、是否去抖等。下面是PictureDrawable的一個使用實例:
Picture picture = new Picture();
Canvas recodingCanvas = picture.beginRecording(100, 200);
recodingCanvas.drawARGB(255, 0xa, 0xc, 0xb);
picture.endRecording();
pictureDrawable.setPicture(picture);
pictureDrawable.setBounds(0, 0, 100, 200);
8.RotateDrawable
RotateDrawable用于旋轉(zhuǎn)其他的基于Drawable的類。常用于補間動畫中,在xml配置文件中作為元素出現(xiàn)。RotateDrawable可以監(jiān)聽到旋轉(zhuǎn)對象的邊界、等級(level)、狀態(tài)的變化。
9.ScaleDrawable
ScaleDrawable用于改變其他的基于Drawable的類的大小。和RotateDrawable一樣,常用于補間動畫中,在xml配置文件中作為< scale>元素出現(xiàn)。ScaleDrawable可以監(jiān)聽到縮放對象的邊界、等級(level)、狀態(tài)的變化。
10.Matrix
Matrix是一個3×3的矩陣類,用于轉(zhuǎn)換坐標,在圖像處理方面,主要用來實現(xiàn)圖像的縮放、旋轉(zhuǎn)、傾斜(skew)等特效。默認情況下構(gòu)造的是單位矩陣。假設源坐標為(x,y,z),目標坐標為(X,Y,Z)。而轉(zhuǎn)換矩陣為:

則目標坐標和源坐標的換算公式如下:
X = x+2y+3z;
Y= 4x+5y+6z;
Z= 7x+8y+9z;
11.ValueAnimator
ValueAnimator是在Android 3.0中引入的,常用于實現(xiàn)屬性動畫。ValueAnimator僅定義了目標對象的屬性隨時間變換的規(guī)則,但不能通知目標對象相應的屬性已經(jīng)發(fā)生了變化,如果希望能夠監(jiān)聽ValueAnimator定義的屬性的變化,需要創(chuàng)建監(jiān)聽器。Animator.AnimatorListener()監(jiān)聽器能夠監(jiān)聽到動畫的開始、結(jié)束、重復和取消,ValueAnimator.AnimatorUpdateListener()則更細心,能夠監(jiān)聽到動畫的每一幀的播放。
12.ObjectAnimator
ObjectAnimator是在Android3.0中引入的,常用于實現(xiàn)屬性動畫。ObjectAnimator是ValueAnimator的子類,ObjectAnimator能夠監(jiān)聽到屬性的變化并通知目標對象。