在Android中圖形的旋轉(zhuǎn)和變化提供了方便的矩陣Maxtrix類,Maxtrix類的setRotate方法接受圖形的變換角度和縮放,最終Bitmap類的createBitmap方法中其中的重載函數(shù),可以接受Maxtrix對象,方法原型如下
view plaincopy to clipboardprint?
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
public static Bitmap createBitmap (Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
參數(shù)的具體意思
source 源 bitmap對象
x 源坐標(biāo)x位置
y 源坐標(biāo)y位置
width 寬度
height 高度
m 接受的maxtrix對象,如果沒有可以設(shè)置 為null
filter 該參數(shù)僅對maxtrix包含了超過一個(gè)翻轉(zhuǎn)才有效。
下面Android123給大家一個(gè)比較經(jīng)典的例子 ,rotate方法是靜態(tài)方法可以直接調(diào)用,參數(shù)為源Bitmap對象,參數(shù)二為旋轉(zhuǎn)的角度,從 0~360,返回值為新的Bitmap對象。其中具體的寬高可以調(diào)整。
view plaincopy to clipboardprint?
public static Bitmap rotate(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees,
(float) b.getWidth() / 2, (float) b.getHeight() / 2);
try {
Bitmap b2 = Bitmap.createBitmap(
b, 0, 0, b.getWidth(), b.getHeight(), m, true);
if (b != b2) {
b.recycle(); //Android開發(fā)網(wǎng)再次提示Bitmap操作完應(yīng)該顯示的釋放
b = b2;
}
} catch (OutOfMemoryError ex) {
// Android123建議大家如何出現(xiàn)了內(nèi)存不足異常,最好return 原始的bitmap對象。.
}
}
return b;
}
public static Bitmap rotate(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.setRotate(degrees,
(float) b.getWidth() / 2, (float) b.getHeight() / 2);
try {
Bitmap b2 = Bitmap.createBitmap(
b, 0, 0, b.getWidth(), b.getHeight(), m, true);
if (b != b2) {
b.recycle(); //Android開發(fā)網(wǎng)再次提示Bitmap操作完應(yīng)該顯示的釋放
b = b2;
}
} catch (OutOfMemoryError ex) {
// Android123建議大家如何出現(xiàn)了內(nèi)存不足異常,最好return 原始的bitmap對象。.
}
}
return b;
}
補(bǔ)充一點(diǎn)吧:水平翻轉(zhuǎn)
view plaincopy to clipboardprint?
public static Bitmap rotate(Bitmap b, int degrees) {
if (degrees != 0 && b != null) {
Matrix m = new Matrix();
m.postScale(1, -1);
m.setRotate(degrees,
(float) b.getWidth() / 2, (float) b.getHeight() / 2);
try {
Bitmap b2 = Bitmap.createBitmap(
b, 0, 0, b.getWidth(), b.getHeight(), m, true);
if (b != b2) {
b.recycle(); //Android開發(fā)網(wǎng)再次提示Bitmap操作完應(yīng)該顯示的釋放
b = b2;
}
} catch (OutOfMemoryError ex) {
// Android123建議大家如何出現(xiàn)了內(nèi)存不足異常,最好return 原始的bitmap對象。.
}
}
return b;
}
本文來自CSDN博客,轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/JavaTiger427/archive/2010/11/25/6034572.aspx
--
學(xué)海無涯