/**
* 图片工具类
*
*/
publicclassImageUtil{
/**
* 设置水印图片在左上角
*@paramContext
*@paramsrc
*@paramwatermark
*@parampaddingLeft
*@parampaddingTop
*@return
*/
publicstaticBitmapcreateWaterMaskLeftTop(
Context context, Bitmap src, Bitmap watermark,
intpaddingLeft,intpaddingTop){
returncreateWaterMaskBitmap(src, watermark,
dp2px(context, paddingLeft), dp2px(context, paddingTop));
}
privatestaticBitmapcreateWaterMaskBitmap(Bitmap src, Bitmap watermark,
intpaddingLeft,intpaddingTop) {
if(src ==null) {
returnnull;
}
intwidth = src.getWidth();
intheight = src.getHeight();
//创建一个bitmap
Bitmap newb = Bitmap.createBitmap(width, height, Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
//将该图片作为画布
Canvas canvas =newCanvas(newb);
//在画布 0,0坐标上开始绘制原始图片
canvas.drawBitmap(src,0,0,null);
//在画布上绘制水印图片
canvas.drawBitmap(watermark, paddingLeft, paddingTop,null);
// 保存
canvas.save(Canvas.ALL_SAVE_FLAG);
// 存储
canvas.restore();
returnnewb;
}
/**
* 设置水印图片在右下角
*@paramContext
*@paramsrc
*@paramwatermark
*@parampaddingRight
*@parampaddingBottom
*@return
*/
publicstaticBitmapcreateWaterMaskRightBottom(
Context context, Bitmap src, Bitmap watermark,
intpaddingRight,intpaddingBottom){
returncreateWaterMaskBitmap(src, watermark,
src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
}
/**
* 设置水印图片到右上角
*@paramContext
*@paramsrc
*@paramwatermark
*@parampaddingRight
*@parampaddingTop
*@return
*/
publicstaticBitmapcreateWaterMaskRightTop(
Context context, Bitmap src, Bitmap watermark,
intpaddingRight,intpaddingTop){
returncreateWaterMaskBitmap( src, watermark,
src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
dp2px(context, paddingTop));
}
/**
* 设置水印图片到左下角
*@paramContext
*@paramsrc
*@paramwatermark
*@parampaddingLeft
*@parampaddingBottom
*@return
*/
publicstaticBitmapcreateWaterMaskLeftBottom(
Context context, Bitmap src, Bitmap watermark,
intpaddingLeft,intpaddingBottom){
returncreateWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
}
/**
* 设置水印图片到中间
*@paramContext
*@paramsrc
*@paramwatermark
*@return
*/
publicstaticBitmapcreateWaterMaskCenter(Bitmap src, Bitmap watermark){
returncreateWaterMaskBitmap(src, watermark,
(src.getWidth() - watermark.getWidth()) /2,
(src.getHeight() - watermark.getHeight()) /2);
}
/**
* 给图片添加文字到左上角
*@paramcontext
*@parambitmap
*@paramtext
*@return
*/
publicstaticBitmapdrawTextToLeftTop(Context context, Bitmap bitmap, String text,
intsize,intcolor,intpaddingLeft,intpaddingTop) {
Paint paint =newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds =newRect();
paint.getTextBounds(text,0, text.length(), bounds);
returndrawTextToBitmap(context, bitmap, text, paint, bounds,
dp2px(context, paddingLeft),
dp2px(context, paddingTop) + bounds.height());
}
/**
* 绘制文字到右下角
*@paramcontext
*@parambitmap
*@paramtext
*@paramsize
*@paramcolor
*@parampaddingLeft
*@parampaddingTop
*@return
*/
publicstaticBitmapdrawTextToRightBottom(Context context, Bitmap bitmap, String text,
intsize,intcolor,intpaddingRight,intpaddingBottom) {
Paint paint =newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds =newRect();
paint.getTextBounds(text,0, text.length(), bounds);
returndrawTextToBitmap(context, bitmap, text, paint, bounds,
bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
bitmap.getHeight() - dp2px(context, paddingBottom));
}
/**
* 绘制文字到右上方
*@paramcontext
*@parambitmap
*@paramtext
*@paramsize
*@paramcolor
*@parampaddingRight
*@parampaddingTop
*@return
*/
publicstaticBitmapdrawTextToRightTop(Context context, Bitmap bitmap, String text,
intsize,intcolor,intpaddingRight,intpaddingTop) {
Paint paint =newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds =newRect();
paint.getTextBounds(text,0, text.length(), bounds);
returndrawTextToBitmap(context, bitmap, text, paint, bounds,
bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
dp2px(context, paddingTop) + bounds.height());
}
/**
* 绘制文字到左下方
*@paramcontext
*@parambitmap
*@paramtext
*@paramsize
*@paramcolor
*@parampaddingLeft
*@parampaddingBottom
*@return
*/
publicstaticBitmapdrawTextToLeftBottom(Context context, Bitmap bitmap, String text,
intsize,intcolor,intpaddingLeft,intpaddingBottom) {
Paint paint =newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds =newRect();
paint.getTextBounds(text,0, text.length(), bounds);
returndrawTextToBitmap(context, bitmap, text, paint, bounds,
dp2px(context, paddingLeft),
bitmap.getHeight() - dp2px(context, paddingBottom));
}
/**
* 绘制文字到中间
*@paramcontext
*@parambitmap
*@paramtext
*@paramsize
*@paramcolor
*@return
*/
publicstaticBitmapdrawTextToCenter(Context context, Bitmap bitmap, String text,
intsize,intcolor) {
Paint paint =newPaint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(color);
paint.setTextSize(dp2px(context, size));
Rect bounds =newRect();
paint.getTextBounds(text,0, text.length(), bounds);
returndrawTextToBitmap(context, bitmap, text, paint, bounds,
(bitmap.getWidth() - bounds.width()) /2,
(bitmap.getHeight() + bounds.height()) /2);
}
//图片上绘制文字
privatestaticBitmapdrawTextToBitmap(Context context, Bitmap bitmap, String text,
Paint paint, Rect bounds,intpaddingLeft,intpaddingTop){
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
paint.setDither(true);// 获取跟清晰的图像采样
paint.setFilterBitmap(true);// 过滤一些
if(bitmapConfig ==null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig,true);
Canvas canvas =newCanvas(bitmap);
canvas.drawText(text, paddingLeft, paddingTop, paint);
returnbitmap;
}
/**
* 缩放图片
*@paramsrc
*@paramw
*@paramh
*@return
*/
publicstaticBitmapscaleWithWH(Bitmap src,doublew,doubleh){
if(w ==0|| h ==0|| src ==null) {
returnsrc;
}else{
// 记录src的宽高
intwidth = src.getWidth();
intheight = src.getHeight();
// 创建一个matrix容器
Matrix matrix =newMatrix();
// 计算缩放比例
floatscaleWidth = (float) (w / width);
floatscaleHeight = (float) (h / height);
// 开始缩放
matrix.postScale(scaleWidth, scaleHeight);
// 创建缩放后的图片
returnBitmap.createBitmap(src,0,0, width, height, matrix,true);
}
}
/**
* dip转pix
*@paramcontext
*@paramdp
*@return
*/
publicstaticintdp2px(Context context,floatdp){
finalfloatscale = context.getResources().getDisplayMetrics().density;
return(int) (dp * scale +0.5f);
}
}
保存一下,经常用到,绘制……
网友评论