图片的缩放:
/***
* 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