android加载本地图片的工具类,Android开发常用的工具类:ImageUtils添加文字或图片水印...

这个博客介绍了如何在Android中使用ImageUtils类来添加水印和文字到图片的不同位置。提供了五个静态方法,分别用于将水印图片放置在左上角、右上角、中间、左下角和右下角。另外,也有五个方法用于在图片的四个角落和中间添加文字。所有方法都考虑了内边距,并使用dp转换为px进行定位。
摘要由CSDN通过智能技术生成

ImageUtils

设置水印图片在左上角

调用createWaterMaskLeftTop(Context context, Bitmap src, Bitmap watermark,int paddingLeft, int paddingTop)

设置水印图片到右上角

调用createWaterMaskRightTop( Context context, Bitmap src, Bitmap watermark,int paddingRight, int paddingTop)

设置水印图片到中间

调用createWaterMaskCenter(Bitmap src, Bitmap watermark)

设置水印图片到左下角

调用createWaterMaskLeftBottom( Context context, Bitmap src, Bitmap watermark,int paddingLeft, int paddingBottom)

设置水印图片在右下角

调用reateWaterMaskLeftTop(Context context, Bitmap src, Bitmap watermark,int paddingLeft, int paddingTop)

给图片添加文字到左上角

调用drawTextToLeftTop(Context context, Bitmap bitmap, String text,int size, int color, int paddingLeft, int paddingTop)

给图片添加文字到右上角

调用drawTextToRightTop(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingTop)

给图片添加文字到中间

调用reateWaterMaskLeftTop(Context context, Bitmap src, Bitmap watermark,int paddingLeft, int paddingTop)

给图片添加文字到左下角

调用drawTextToLeftBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingLeft, int paddingBottom)

给图片添加文字到右下角

调用drawTextToRightBottom(Context context, Bitmap bitmap, String text, int size, int color, int paddingRight, int paddingBottom)

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Paint;

import android.graphics.Rect;

/**

* Created by cs_android on 2018/1/12.

* 添加文字或水印

*/

public class ImageUtils {

/**

* 设置水印图片在左上角

*/

public static Bitmap createWaterMaskLeftTop(Context context, Bitmap src, Bitmap watermark,

int paddingLeft, int paddingTop) {

return createWaterMaskBitmap(src, watermark, dp_to_px(context, paddingLeft),

dp_to_px(context, paddingTop));

}

/**

* 设置水印图片到右上角

*

*/

public static Bitmap createWaterMaskRightTop(

Context context, Bitmap src, Bitmap watermark,

int paddingRight, int paddingTop) {

return createWaterMaskBitmap(src, watermark,

src.getWidth() - watermark.getWidth() - dp_to_px(context, paddingRight),

dp_to_px(context, paddingTop));

}

/**

* 设置水印图片到中间

*/

public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {

return createWaterMaskBitmap(src, watermark,

(src.getWidth() - watermark.getWidth()) / 2,

(src.getHeight() - watermark.getHeight()) / 2);

}

/**

* 设置水印图片到左下角

*/

public static Bitmap createWaterMaskLeftBottom(

Context context, Bitmap src, Bitmap watermark,

int paddingLeft, int paddingBottom) {

return createWaterMaskBitmap(src, watermark, dp_to_px(context, paddingLeft),

src.getHeight() - watermark.getHeight() - dp_to_px(context, paddingBottom));

}

/**

* 设置水印图片在右下角

*/

public static Bitmap createWaterMaskRightBottom(

Context context, Bitmap src, Bitmap watermark,

int paddingRight, int paddingBottom) {

return createWaterMaskBitmap(src, watermark,

src.getWidth() - watermark.getWidth() - dp_to_px(context, paddingRight),

src.getHeight() - watermark.getHeight() - dp_to_px(context, paddingBottom));

}

/**

* 添加图片水印

* @param bitmap

* @param watermark

* @param paddingLeft

* @param paddingTop

* @return

*/

private static Bitmap createWaterMaskBitmap(Bitmap bitmap, Bitmap watermark, int paddingLeft, int paddingTop) {

if (bitmap == null) {

return null;

}

int width = bitmap.getWidth();

int height = bitmap.getHeight();

//创建一个bitmap

Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图

//将该图片作为画布

Canvas canvas = new Canvas(newb);

//在画布 0,0坐标上开始绘制原始图片

canvas.drawBitmap(bitmap, 0, 0, null);

//在画布上绘制水印图片

canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);

// 保存

canvas.save(Canvas.ALL_SAVE_FLAG);

// 存储

canvas.restore();

return newb;

}

/**

* 给图片添加文字到左上角

*/

public static Bitmap drawTextToLeftTop(Context context, Bitmap bitmap, String text,

int size, int color, int paddingLeft, int paddingTop) {

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(color);

paint.setTextSize(dp_to_px(context, size));

Rect bounds = new Rect();

paint.getTextBounds(text, 0, text.length(), bounds);

return drawTextToBitmap(context, bitmap, text, paint, bounds,

dp_to_px(context, paddingLeft),

dp_to_px(context, paddingTop) + bounds.height());

}

/**

* 给图片添加文字到右上角

*/

public static Bitmap drawTextToRightTop(Context context, Bitmap bitmap, String text,

int size, int color, int paddingRight, int paddingTop) {

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(color);

paint.setTextSize(dp_to_px(context, size));

Rect bounds = new Rect();

paint.getTextBounds(text, 0, text.length(), bounds);

return drawTextToBitmap(context, bitmap, text, paint, bounds,

bitmap.getWidth() - bounds.width() - dp_to_px(context, paddingRight),

dp_to_px(context, paddingTop) + bounds.height());

}

/**

* 给图片添加文字到中间

*/

public static Bitmap drawTextToCenter(Context context, Bitmap bitmap, String text,

int size, int color) {

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(color);

paint.setTextSize(dp_to_px(context, size));

Rect bounds = new Rect();

paint.getTextBounds(text, 0, text.length(), bounds);

return drawTextToBitmap(context, bitmap, text, paint, bounds,

(bitmap.getWidth() - bounds.width()) / 2,

(bitmap.getHeight() + bounds.height()) / 2);

}

/**

* 给图片添加文字到左下角

*/

public static Bitmap drawTextToLeftBottom(Context context, Bitmap bitmap, String text,

int size, int color, int paddingLeft, int paddingBottom) {

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(color);

paint.setTextSize(dp_to_px(context, size));

Rect bounds = new Rect();

paint.getTextBounds(text, 0, text.length(), bounds);

return drawTextToBitmap(context, bitmap, text, paint, bounds,

dp_to_px(context, paddingLeft),

bitmap.getHeight() - dp_to_px(context, paddingBottom));

}

/**

* 给图片添加文字到右下角

*/

public static Bitmap drawTextToRightBottom(Context context, Bitmap bitmap, String text,

int size, int color, int paddingRight, int paddingBottom) {

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

paint.setColor(color);

paint.setTextSize(dp_to_px(context, size));

Rect bounds = new Rect();

paint.getTextBounds(text, 0, text.length(), bounds);

return drawTextToBitmap(context, bitmap, text, paint, bounds,

bitmap.getWidth() - bounds.width() - dp_to_px(context, paddingRight),

bitmap.getHeight() - dp_to_px(context, paddingBottom));

}

/**

*

* @param context

* @param bitmap

* @param text

* @param paint

* @param bounds

* @param paddingLeft

* @param paddingTop

* @return

*/

private static Bitmap drawTextToBitmap(Context context, Bitmap bitmap, String text,

Paint paint, Rect bounds, int paddingLeft, int paddingTop) {

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 = new Canvas(bitmap);

canvas.drawText(text, paddingLeft, paddingTop, paint);

return bitmap;

}

/**

* dip转pix

*

* @param context

* @param dp

* @return

*/

public static int dp_to_px(Context context, float dp) {

final float scale = context.getResources().getDisplayMetrics().density;

return (int) (dp * scale + 0.5f);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值