網上搜了很多,都要使用Canvas,這個絕對是繞遠了……
經實驗,圖片裁切只要一句就可以 ,下面是我寫的一個按正方形區域裁剪的方法
/**
參考
http://stackoverflow.com/questions/3846338/how-to-crop-an-image-in-android
* 按正方形裁切圖片
*/
public static Bitmap ImageCrop(Bitmap bitmap) {
int w = bitmap.getWidth(); // 得到圖片的寬,高
int h = bitmap.getHeight();
int wh = w > h ? h : w; // 裁切后所取的正方形區域邊長
int retX = w > h ? (w - h) / 2 : 0 ; //基於原圖,取 正方形左上角x坐標
int retY = w > h ? 0 : (h - w) / 2 ;
// 下面這句是關鍵
return Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null , false );
}
*/
public static Bitmap ImageCrop(Bitmap bitmap) {
int w = bitmap.getWidth(); // 得到圖片的寬,高
int h = bitmap.getHeight();
int wh = w > h ? h : w; // 裁切后所取的正方形區域邊長
int retX = w > h ? (w - h) / 2 : 0 ; //基於原圖,取 正方形左上角x坐標
int retY = w > h ? 0 : (h - w) / 2 ;
// 下面這句是關鍵
return Bitmap.createBitmap(bitmap, retX, retY, wh, wh, null , false );
}
本博客所有隨筆,若未明確標示為轉載或未帶有原文鏈接,皆為原創。
本博客所有隨筆版權歸博客園和kai.ma所有,歡迎轉載,轉載請保留:
本博客所有隨筆版權歸博客園和kai.ma所有,歡迎轉載,轉載請保留:
- 出處:http://kaima.cnblogs.com
- 作者:kai.ma