Android图片的处理类

图片的缩放:

/***
	 * scaling the image
	 * 
	 * @param bitMap
	 *            source image resources
	 * 
	 * @param newWidth
	 *            new width of the image
	 * 
	 * @param newHeight
	 *            new height of the image
	 * 
	 * @return return the image after scaling .if the input bitmap is null,return null;
	 */
 
	public static Bitmap zoomImage(Bitmap bitMap, int newWidth, int newHeight) {
 
		// File file = new File("");
		// Bitmap b = B
		if (bitMap == null) {
			return null;
		}
		// 获取这个图片的宽和高
		int width = bitMap.getWidth();
		int height = bitMap.getHeight();
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		// 计算缩放率,新尺寸除原始尺寸
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// 新尺寸大于原始尺寸则不缩放
		if (scaleWidth > 1.0f || scaleHeight > 1.0f) {
			return bitMap;
		}
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);
		Bitmap bitmap = Bitmap.createBitmap(bitMap, 0, 0, width, height, matrix, true);
		return bitmap;
 
	}

图片圆角:

/**
	 * get the round corner of image
	 * 
	 * @param bitmap
	 * @return
	 */
	public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
		if (bitmap == null) {
			return null;
		}
		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);
 
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
 
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
 
		return output;
	}
获取带阴影的图片
public static Bitmap getShadowBitmap(Bitmap bitmap,int shadow,int bgColor)
	{
		if(bitmap==null)
			return null;
		int width = bitmap.getWidth()+shadow*2;
		int height = bitmap.getHeight()+shadow*2;
		int dx = shadow/2;
		int dy = 2*shadow/3;
		Bitmap output = Bitmap.createBitmap(width+5, height+5, Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		
		Paint paint = new Paint();
		Rect rect = new Rect(shadow-dx, shadow-dy, width-shadow-dx, height-shadow-dy);
		RectF rectF = new RectF(rect);
 
		paint.setAntiAlias(true);
		paint.setColor(bgColor);
		
		canvas.drawColor(bgColor);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		// 设定阴影(柔边, X 轴位移, Y 轴位移, 阴影颜色)  
		paint.setShadowLayer(shadow, dx, dy, Color.argb(0xff, 0xdd, 0xdd, 0xdd));
		
		canvas.drawRoundRect(rectF, 0, 0, paint);
		canvas.drawBitmap(bitmap, shadow-dx, shadow-dy, paint);
 
		return output;
	}

转载自: http://my.eoe.cn/yaming/archive/404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值