1.從資源中獲取位圖(Bitmap)
可以使用BitmapDrawable或者BitmapFactory來獲取資源中的位圖。
當然,首先需要獲取資源:Resources res=getResources();
使用BitmapDrawable獲取位圖
(1)使用BitmapDrawable (InputStream is)構造一個BitmapDrawable;
(2)使用BitmapDrawable類的getBitmap()獲取得到位圖;
// 讀取InputStream並得到位圖 InputStream is=res.openRawResource(R.drawable.pic180); BitmapDrawable bmpDraw=new BitmapDrawable(is); Bitmap bmp=bmpDraw.getBitmap();
或者采用下面的方式:
BitmapDrawable bmpDraw=(BitmapDrawable)res.getDrawable(R.drawable.pic180);
Bitmap bmp=bmpDraw.getBitmap();
使用BitmapFactory獲取位圖
(Creates Bitmap objects from various sources, including files, streams, and byte-arrays.)
使用BitmapFactory類decodeStream(InputStream is)解碼位圖資源,獲取位圖。
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180);
BitmapFactory的所有函數都是static,這個輔助類可以通過資源ID、路徑、文件、數據流等方式來獲取位圖。
在Android SDK中說明可以支持的圖片格式如下:png (preferred), jpg (acceptable), gif (discouraged),和bmp(Android SDK Support Media Format)。
2.獲取位圖的信息
獲取到Bitmap后通過bitmap類的接口 可以獲取到位圖的位圖大小、像素、density、透明度、顏色格式等信息
-
在Bitmap中對RGB顏色格式使用Bitmap.Config定義,僅包括ALPHA_8、ARGB_4444、ARGB_8888、RGB_565,缺少了一些其他的,比如說RGB_555,在開發中可能需要注意這個小問題;
-
Bitmap還提供了compress()接口來壓縮圖片,不過AndroidSAK只支持 PNG、JPG格式的壓縮;其他格式的需要Android開發人員自己補充了。
3.顯示位圖
顯示位圖有三種方法可以實現
(1)可以使用核心類Canvas,通過Canvas類的drawBirmap()顯示位圖;
(2)借助於BitmapDrawable來將Bitmap繪制到Canvas;
(3)通過BitmapDrawable將位圖顯示到View中。
轉換為BitmapDrawable對象顯示位圖
// 獲取位圖 Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic180); // 轉換為BitmapDrawable對象 BitmapDrawable bmpDraw=new BitmapDrawable(bmp); // 顯示位圖 ImageView iv2 = (ImageView)findViewById(R.id.ImageView02); iv2.setImageDrawable(bmpDraw);
使用Canvas類顯示位圖
這兒采用一個繼承自View的子類Panel,在子類的OnDraw中顯示
public class MainActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(new Panel(this)); } class Panel extends View{ public Panel(Context context) { super(context); } public void onDraw(Canvas canvas){ Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); canvas.drawColor(Color.BLACK); canvas.drawBitmap(bmp, 10, 10, null); } } }
4.位圖縮放
(1)將一個位圖按照需求重畫一遍,畫后的位圖就是我們需要的了,與位圖的顯示幾乎一樣:drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)。
(2)在原有位圖的基礎上,縮放原位圖,創建一個新的位圖:CreateBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
(3)借助Canvas的scale(float sx, float sy) (Preconcat the current matrix with the specified scale.),不過要注意此時整個畫布都縮放了。
(4)借助Matrix:
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); Matrix matrix=new Matrix(); matrix.postScale(0.2f, 0.2f); Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true); canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, 10, 10, null);
5.位圖旋轉
旋轉實現和縮放實現類似,也可以借助Matrix或者Canvas實現;
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic180); Matrix matrix=new Matrix(); matrix.postScale(0.8f, 0.8f); matrix.postRotate(45); Bitmap dstbmp=Bitmap.createBitmap(bmp,0,0,bmp.getWidth(), bmp.getHeight(),matrix,true); canvas.drawColor(Color.BLACK); canvas.drawBitmap(dstbmp, 10, 10, null);
6.Canvas的save和restore
onDraw方法會傳入一個Canvas對象,它是你用來繪制控件視覺界面的畫布。
在onDraw方法里,我們經常會看到調用save和restore方法,它們到底是干什么用的呢?
❑ save:用來保存Canvas的狀態。save之后,可以調用Canvas的平移、放縮、旋轉、錯切、裁剪等操作。
❑ restore:用來恢復Canvas之前保存的狀態。防止save后對Canvas執行的操作對后續的繪制有影響。
save和restore要配對使用(restore可以比save少,但不能多),如果restore調用次數比save多,會引發Error。save和restore之間,往往夾雜的是對Canvas的特殊操作。
原文鏈接:
http://www.cnblogs.com/feisky/archive/2010/01/10/1643460.html