android图片上水印字体颜色,Android给图片添加文字和水印
话不多说 上图
gif5新文件.gif
public class ImageUtil {
/**
* 设置水印图片在左上角
*
* @param context 上下文
* @param src
* @param watermark
* @param paddingLeft
* @param paddingTop
* @return
*/
public static Bitmap createWaterMaskLeftTop(Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) {
return createWaterMaskBitmap(src, watermark,
dp2px(context, paddingLeft), dp2px(context, paddingTop));
}
private static Bitmap createWaterMaskBitmap(Bitmap src, Bitmap watermark, int paddingLeft, int paddingTop) {
if (src == null) {
return null;
}
int width = src.getWidth();
int height = src.getHeight();
//创建一个bitmap
Bitmap newb = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// 创建一个新的和SRC长度宽度一样的位图
//将该图片作为画布
Canvas canvas = new Canvas(newb);
//在画布 0,0坐标上开始绘制原始图片
canvas.drawBitmap(src, 0, 0, null);
//在画布上绘制水印图片
canvas.drawBitmap(watermark, paddingLeft, paddingTop, null);
// 保存
canvas.save(Canvas.ALL_SAVE_FLAG);
// 存储
canvas.restore();
return newb;
}
/**
* 设置水印图片在右下角
*
* @param context 上下文
* @param src
* @param watermark
* @param paddingRight
* @param paddingBottom
* @return
*/
public static Bitmap createWaterMaskRightBottom(Context context, Bitmap src, Bitmap watermark, int paddingRight, int paddingBottom) {
return createWaterMaskBitmap(src, watermark,
src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
}
/**
* 设置水印图片到右上角
*
* @param context
* @param src
* @param watermark
* @param paddingRight
* @param paddingTop
* @return
*/
public static Bitmap createWaterMaskRightTop(Context context, Bitmap src, Bitmap watermark, int paddingRight, int paddingTop) {
return createWaterMaskBitmap(src, watermark,
src.getWidth() - watermark.getWidth() - dp2px(context, paddingRight),
dp2px(context, paddingTop));
}
/**
* 设置水印图片到左下角
*
* @param context
* @param src
* @param watermark
* @param paddingLeft
* @param paddingBottom
* @return
*/
public static Bitmap createWaterMaskLeftBottom(Context context, Bitmap src, Bitmap watermark, int paddingLeft, int paddingBottom) {
return createWaterMaskBitmap(src, watermark, dp2px(context, paddingLeft),
src.getHeight() - watermark.getHeight() - dp2px(context, paddingBottom));
}
/**
* 设置水印图片到中间
*
* @param src
* @param watermark
* @return
*/
public static Bitmap createWaterMaskCenter(Bitmap src, Bitmap watermark) {
return createWaterMaskBitmap(src, watermark,
(src.getWidth() - watermark.getWidth()) / 2,
(src.getHeight() - watermark.getHeight()) / 2);
}
/**
* 给图片添加文字到左上角
*
* @param context
* @param bitmap
* @param text
* @return
*/
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(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
dp2px(context, paddingLeft),
dp2px(context, paddingTop) + bounds.height());
}
/**
* 绘制文字到右下角
*
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @return
*/
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(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
bitmap.getHeight() - dp2px(context, paddingBottom));
}
/**
* 绘制文字到右上方
*
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @param paddingRight
* @param paddingTop
* @return
*/
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(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
bitmap.getWidth() - bounds.width() - dp2px(context, paddingRight),
dp2px(context, paddingTop) + bounds.height());
}
/**
* 绘制文字到左下方
*
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @param paddingLeft
* @param paddingBottom
* @return
*/
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(dp2px(context, size));
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
return drawTextToBitmap(context, bitmap, text, paint, bounds,
dp2px(context, paddingLeft),
bitmap.getHeight() - dp2px(context, paddingBottom));
}
/**
* 绘制文字到中间
*
* @param context
* @param bitmap
* @param text
* @param size
* @param color
* @return
*/
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(dp2px(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);
}
//图片上绘制文字
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;
}
/**
* 缩放图片
*
* @param src
* @param w
* @param h
* @return
*/
public static Bitmap scaleWithWH(Bitmap src, double w, double h) {
if (w == 0 || h == 0 || src == null) {
return src;
} else {
// 记录src的宽高
int width = src.getWidth();
int height = src.getHeight();
// 创建一个matrix容器
Matrix matrix = new Matrix();
// 计算缩放比例
float scaleWidth = (float) (w / width);
float scaleHeight = (float) (h / height);
// 开始缩放
matrix.postScale(scaleWidth, scaleHeight);
// 创建缩放后的图片
return Bitmap.createBitmap(src, 0, 0, width, height, matrix, true);
}
}
/**
* dip转pix
*
* @param context
* @param dp
* @return
*/
public static int dp2px(Context context, float dp) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}
在Activity中获取到ImageView对象,并且获取Bitmap对象,对Bitmap进行canva绘图,添加水印
public class MainActivity extends AppCompatActivity {
private ImageView mSourImage;
private ImageView mWartermarkImage;
private ImageView mWartermarkImage2;
private ImageView mWartermarkImage3;
private ImageView mWartermarkImage4;
private ImageView mWartermarkImage5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mSourImage = (ImageView) findViewById(R.id.sour_pic);
mWartermarkImage = (ImageView) findViewById(R.id.wartermark_pic);
mWartermarkImage2 = (ImageView) findViewById(R.id.wartermark_pic2);
mWartermarkImage3 = (ImageView) findViewById(R.id.wartermark_pic3);
mWartermarkImage4 = (ImageView) findViewById(R.id.wartermark_pic4);
mWartermarkImage5 = (ImageView) findViewById(R.id.wartermark_pic5);
Bitmap sourBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sour_pic);
mSourImage.setImageBitmap(sourBitmap);
Bitmap waterBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.weixin);
Bitmap watermarkBitmap = ImageUtil.createWaterMaskCenter(sourBitmap, waterBitmap);
Bitmap watermarkBitmap2 = ImageUtil.createWaterMaskLeftBottom(this, sourBitmap, waterBitmap, 0, 0);
Bitmap watermarkBitmap3 = ImageUtil.createWaterMaskRightBottom(this, sourBitmap, waterBitmap, 0, 0);
Bitmap watermarkBitmap4 = ImageUtil.createWaterMaskLeftTop(this, sourBitmap, waterBitmap, 0, 0);
Bitmap watermarkBitmap5 = ImageUtil.createWaterMaskRightTop(this, sourBitmap, waterBitmap, 0, 0);
Bitmap textBitmap = ImageUtil.drawTextToLeftTop(this, watermarkBitmap4, "左上角", 16, Color.RED, 0, 0);
Bitmap textBitmap2 = ImageUtil.drawTextToRightBottom(this, watermarkBitmap3, "右下角", 16, Color.RED, 0, 0);
Bitmap textBitmap3 = ImageUtil.drawTextToRightTop(this, watermarkBitmap5, "右上角", 16, Color.RED, 0, 0);
Bitmap textBitmap4 = ImageUtil.drawTextToLeftBottom(this, watermarkBitmap2, "左下角", 16, Color.RED, 0, 0);
Bitmap textBitmap5 = ImageUtil.drawTextToCenter(this, watermarkBitmap, "中间", 16, Color.RED);
mWartermarkImage.setImageBitmap(textBitmap);
mWartermarkImage2.setImageBitmap(textBitmap2);
mWartermarkImage3.setImageBitmap(textBitmap3);
mWartermarkImage4.setImageBitmap(textBitmap4);
mWartermarkImage5.setImageBitmap(textBitmap5);
}
}
添加一个布局,上面是原始图片,下面是添加水印后的图片
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:id="@+id/sour_pic_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="原图" />
android:id="@+id/sour_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:scaleType="centerInside" />
android:id="@+id/watermark_pic_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="水印" />
android:id="@+id/wartermark_pic"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:scaleType="centerInside" />
android:id="@+id/wartermark_pic2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:scaleType="centerInside" />
android:id="@+id/wartermark_pic3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:scaleType="centerInside" />
android:id="@+id/wartermark_pic4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:scaleType="centerInside" />
android:id="@+id/wartermark_pic5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:scaleType="centerInside" />
android图片上水印字体颜色,Android给图片添加文字和水印相关推荐
- android notification设置不同字体颜色,Android Notification自定义通知样式你要知道的事...
本文将根据个人经验对Notification做个总结,以供参考! 什么是通知(Notification) 通知是一个可以在应用程序正常的用户界面之外显示给用户的消息. 通知发出时,它首先出现在状态栏的 ...
- android 怎么获取app 字体颜色,Android APP使用自定义字体实现方法
android系统内置字体 android 系统本身内置了一些字体,可以在程序中使用,并且支持在xml配置textView的时候进行修改字体的样式.支持字段为android:textStyle ,an ...
- android 怎么获取app 字体颜色,android app 修改字体
android中可能会遇到修改字体的情况,虽然说需求比较少,但是偶尔还会遇到 可以使用三方框架来帮助我们简单做到 api "uk.co.chrisjenx:calligraphy:2.2.0 ...
- android如何设置透明字体颜色,android TextView文字透明度跟背景透明度设置
当前位置: 我的异常网 » Android » android TextView文字透明度跟背景透明度设置 android TextView文字透明度跟背景透明度设置 www.myexceptions ...
- Android动态改变TextView字体颜色
Android动态改变TextView字体颜色 分类: Android 2012-06-04 21:56 141人阅读 评论(0) 收藏 举报 androidcolorslayout 必须在在res/ ...
- android selector点击修改颜色,Android Selector 按下修改背景和文本颜色的实现代码
1,selector 按下修改背景和文本颜色 [1]点击改变字体颜色 - android:state_pressed(按压状态) [2]selector状态选择器(bg_btn_two (存放 res ...
- vue如何动态获取数据改变背景颜色和字体颜色以及获取图片
vue如何动态获取数据改变背景颜色和字体颜色以及获取图片 首先要想获取多条数据要用到v-for循环,写到循环就必须写:key 话不多说上代码 重点就是:style="{background: ...
- 在线分析图片上的字体
在线分析图片上的字体 新闻来源:呐喊网络部落格 当我们看到一张图片,上面有很漂亮的字体,可是这是什么字体呢?去浩瀚的英文字体库一一对照不是完全没有准确找到的可能,只是,谁有那样的耐力?不是标榜自己,本 ...
- C#图片处理类(颜色透明化,图片切割,图片合并,图片旋转等)(转)
目录 1.背景透明化 2.指定颜色透明化 3.指定颜色替换成另一种颜色 4.图片按比例缩放 5.图片旋转 6.图片更改透明度 7.图片添加文字 8. ...
最新文章
- mysql 5.7编译安装重启_mysql5.7源码编译安装
- python同步异步_python中Tornado的同步与异步I/O的介绍(附示例)
- mysql为什么用索引_MySql为什么使用B+树做索引
- 特别完善的面试知识总结
- 新装Ubuntu系统,窗口过大,无法拖动,按钮都点不到怎么办?
- 不同业务场景下如何进行数据库水平切分?
- hdu 1856 并查集——ac的不一定正确
- 【项目实施随笔】生产领料
- 一个码砖的码农,在CSDN写一年博客,出书了?
- matlab 离散频域分析,离散系统频域分析及matlab实现.doc
- 2.Collection、Iterator迭代器、泛型、斗地主案例
- ThinkPHP5支付宝支付(当面付)付款码ISV服务商模式
- libvpx将yuv转vp8/vp9(八)
- websocket以及聊天室的实现
- go如何实现可选参数
- python人脸识别系统源码
- Mint-ui设计移动端系统
- Flowable入门系列文章62 - 异步延续
- JAVA后端代码打包_Jenkins持续构建打包后端服务流程详解
- 给小学生科普计算机知识竞赛,小学生科普知识竞赛PPT.ppt