Bitmap:
1、Drawable → Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap
.createBitmap(
drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(),
drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
: Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
// canvas.setBitmap(bitmap);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight());
drawable.draw(canvas);
return bitmap;
}
2、從資源中獲取Bitmap
Resources res=getResources();
Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);
3、Bitmap → byte[]
private byte[] Bitmap2Bytes(Bitmap bm){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
return baos.toByteArray();
}
4、byte[] → Bitmap
private Bitmap Bytes2Bimap(byte[] b){
if(b.length!=0){
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
else {
return null;
}
}
5、保存bitmap
static boolean saveBitmap2file(Bitmap bmp,String filename){
CompressFormat format= Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream("/sdcard/" + filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Generated by Foxit PDF Creator © Foxit Software
http://www.foxitsoftware.com For evaluation only.
e.printStackTrace();
}
return bmp.compress(format, quality, stream);
}
6、將圖片按自己的要求縮放
// 圖片源
Bitmap bm = BitmapFactory.decodeStream(getResources()
.openRawResource(R.drawable.dog));
// 獲得圖片的寬高
int width = bm.getWidth();
int height = bm.getHeight();
// 設置想要的大小
int newWidth = 320;
int newHeight = 480;
// 計算縮放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要縮放的matrix參數
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的圖片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
// 放在畫布上
canvas.drawBitmap(newbm, 0, 0, paint);
File圖片轉Bitmap
Bitmap bt = BitmapFactory.decodeFile("/sdcard/myImage/" + "head.jpg");//圖片地址
//圖片轉Bitmap
public Bitmap drawableToBitamp(int drawableResource) {<span style="white-space: pre;"> </span>//可以取raw裏面的資源
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
InputStream is = this.getResources().openRawResource(drawableResource);
BitmapFactory.decodeStream(is, null, opt);
return BitmapFactory.decodeStream(is, null, opt);
}
Canvas:
Canvas():創建一個空的畫布,可以使用setBitmap()方法來設置繪製的具體畫布;
Canvas(Bitmap bitmap):以bitmap對象創建一個畫布,則將內容都繪製在bitmap上,bitmap不得爲null;
Canvas(GL gl):在繪製3D效果時使用,與OpenGL有關;
drawColor:設置畫布的背景色;
setBitmap:設置具體的畫布;
clipRect:設置顯示區域,即設置裁剪區;
isOpaque:檢測是否支持透明;
rotate:旋轉畫布;
canvas.drawRect(RectF,Paint)方法用於畫矩形,第一個參數爲圖形顯示區域,第二個參數爲畫筆,設置好圖形顯示區域Rect和畫筆Paint後,即可畫圖;
canvas.drawRoundRect(RectF, float, float, Paint) 方法用於畫圓角矩形,第一個參數爲圖形顯示區域,第二個參數和第三個參數分別是水平圓角半徑和垂直圓角半徑。
canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint):第一個參數oval爲RectF類型,即圓弧顯示區域,startAngle和sweepAngle均爲float類型,分別表示圓弧起始角度和圓弧度數,3點鐘方向爲0度,useCenter設置是否顯示圓心,boolean類型,paint爲畫筆;
canvas.drawCircle(float,float, float, Paint)方法用於畫圓,前兩個參數代表圓心座標,第三個參數爲圓半徑,第四個參數是畫筆;
void drawPath(Path path, Paint paint) //繪製一個路徑,參數一爲Path路徑對象
void drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) //貼圖,參數一就是我們常規的Bitmap對象,參數二是源區域(這裏是bitmap),參數三是目標區域(應該在 canvas的位置和大小),參數四是Paint畫刷對象,因爲用到了縮放和拉伸的可能,當原始Rect不等於目標Rect時性能將會有大幅損失。
void drawLine(float startX, float startY, float stopX, float stopY, Paint paint) //畫線,參數一起始點的x軸位置,參數二起始點的y軸位置,參數三終點的x軸水平位置,參數四y軸垂直位置,最後一個參數爲Paint畫刷對象。前四個參數的類型均爲float,最後一個參數類型爲Paint。表示用畫筆paint從點(startX,startY)到點(stopX,stopY)畫一條直線;
void drawPoint(float x, float y, Paint paint) //畫點,參數一水平x軸,參數二垂直y軸,第三個參數爲Paint對象。
void drawText(String text, float x, float y, Paint paint) //渲染文本,Canvas類除了上面的還可以描繪文字,參數一是String類型的文本,參數二x軸,參數三y軸,參數四是Paint對象。
void drawTextOnPath(String text, Path path, float hOffset, float vOffset, Paint paint) //在路徑上繪製文本,相對於上面第二個參數是Path路徑對象
Paint:
setARGB(int a,int r,int g,int b);
設置繪製的顏色,a代表透明度,r,g,b代表顏色值。
setAlpha(int a);
設置繪製圖形的透明度。
setColor(int color);
設置繪製的顏色,使用顏色值來表示,該顏色值包括透明度和RGB顏色。
etAntiAlias(boolean aa);
設置是否使用抗鋸齒功能,會消耗較大資源,繪製圖形速度會變慢。
setDither(boolean dither);
設定是否使用圖像抖動處理,會使繪製出來的圖片顏色更加平滑和飽滿,圖像更加清晰
setFilterBitmap(boolean filter);
如果該項設置爲true,則圖像在動畫進行中會濾掉對Bitmap圖像的優化操作,加快顯示
速度,本設置項依賴於dither和xfermode的設置
setMaskFilter(MaskFilter maskfilter);
設置MaskFilter,可以用不同的MaskFilter實現濾鏡的效果,如濾化,立體等 *
setColorFilter(ColorFilter colorfilter);
設置顏色過濾器,可以在繪製顏色時實現不用顏色的變換效果
setPathEffect(PathEffect effect);
設置繪製路徑的效果,如點畫線等
setShader(Shader shader);
設置圖像效果,使用Shader可以繪製出各種漸變效果
setShadowLayer(float radius ,float dx,float dy,int color);
在圖形下面設置陰影層,產生陰影效果,radius爲陰影的角度,dx和dy爲陰影在x軸和y軸上的距離,color爲陰影的顏色
setStyle(Paint.Style style);
設置畫筆的樣式,爲FILL,FILL_OR_STROKE,或STROKE
setStrokeCap(Paint.Cap cap);
當畫筆樣式爲STROKE或FILL_OR_STROKE時,設置筆刷的圖形樣式,如圓形樣式
Cap.ROUND,或方形樣式Cap.SQUARE
setSrokeJoin(Paint.Join join);
設置繪製時各圖形的結合方式,如平滑效果等
setStrokeWidth(float width);
當畫筆樣式爲STROKE或FILL_OR_STROKE時,設置筆刷的粗細度
setXfermode(Xfermode xfermode);
設置圖形重疊時的處理方式,如合併,取交集或並集,經常用來製作橡皮的擦除效果
如:setXfermode (new PorterDuffXfermode(Mode.SRC_IN))
2.文本繪製
setFakeBoldText(boolean fakeBoldText);
模擬實現粗體文字,設置在小字體上效果會非常差
setSubpixelText(boolean subpixelText);
設置該項爲true,將有助於文本在LCD屏幕上的顯示效果
setTextAlign(Paint.Align align);
設置繪製文字的對齊方向
etTextScaleX(float scaleX);
設置繪製文字x軸的縮放比例,可以實現文字的拉伸的效果
setTextSize(float textSize);
設置繪製文字的字號大小
setTextSkewX(float skewX);
設置斜體文字,skewX爲傾斜弧度
setTypeface(Typeface typeface);
設置Typeface對象,即字體風格,包括粗體,斜體以及襯線體,非襯線體等
setUnderlineText(boolean underlineText);
設置帶有下劃線的文字效果
setStrikeThruText(boolean strikeThruText);
設置帶有刪除線的效果