1. 程式人生 > >Android 圖片切圓工具類

Android 圖片切圓工具類

                圖片切圓工具類,複製貼上可以直接用微笑,程式碼如下:

------------------------------------------------------------------------------------------------------------------------------------------------------------

/**
 * 這是一個圖片切原工具類
 * 直接複製這個類到工具包
 * 類名.方法就可以得到一個圓圖了,就用面2行程式碼
 * Bitmap bit=BitmapFactory.decodeResource(getResources(), 想切的圖片);
 * ImageView的id.setImageBitmap(QieYuan.getYuan(bit));
 * */
public class QieYuan {
	public static  Bitmap getYuan(Bitmap bitmap){
		Bitmap output= Bitmap.createBitmap(bitmap.getWidth(),
		bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		
		final int color = 0xff424242; 
		final Paint paint = new Paint(); 
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
		final RectF rectF = new RectF(rect); 
		final float roundPx = bitmap.getWidth() / 2;
		final float roundPY = bitmap.getHeight() / 2;
		
		paint.setAntiAlias(true); 
		canvas.drawARGB(0, 0, 0, 0); 
		paint.setColor(color); 
		canvas.drawRoundRect(rectF, roundPx, roundPY, paint); 

		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
		canvas.drawBitmap(bitmap, rect, rect, paint); 
		return output; 
		
	}

}