当前位置: 首页 > news >正文

Android中使用图片水印,并且能够在线下载字体并应用于水印

Android中使用图片水印,并且能够在线下载字体并应用于水印

要在Android中使用图片水印,并且能够在线下载字体并应用于水印,可以按照以下步骤进行:

1.使用PicassoGlide或其他图片加载库加载图片:

ImageView imageView = findViewById(R.id.image_view);
String imageUrl = "https://example.com/image.jpg";
Picasso.get().load(imageUrl).into(imageView);

2.创建一个带有水印的Drawable

BitmapDrawable watermarkDrawable = createWatermarkDrawable("Watermark Text", "font.ttf", 16);

3.创建水印的方法:

private BitmapDrawable createWatermarkDrawable(String text, String fontPath, int textSize) {Typeface typeface = Typeface.createFromFile(fontPath);// 创建一个空白的Bitmap作为画布Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(bitmap);// 设置画布属性Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);paint.setColor(Color.WHITE);paint.setTextSize(textSize);paint.setTypeface(typeface);paint.setTextAlign(Paint.Align.LEFT);// 测量文本的宽度和高度Rect textBounds = new Rect();paint.getTextBounds(text, 0, text.length(), textBounds);// 计算水印的位置int x = (bitmap.getWidth() - textBounds.width()) / 2;int y = (bitmap.getHeight() + textBounds.height()) / 2;// 在画布上绘制文本canvas.drawText(text, x, y, paint);return new BitmapDrawable(getResources(), bitmap);
}

4.将水印应用到图片上:

Drawable imageDrawable = imageView.getDrawable();
Drawable[] layers = new Drawable[2];
layers[0] = imageDrawable;
layers[1] = watermarkDrawable;
LayerDrawable layerDrawable = new LayerDrawable(layers);
imageView.setImageDrawable(layerDrawable);

5.在线下载字体文件: 你可以使用DownloadManager来下载字体文件,并在下载完成后将其应用于水印:

private void downloadFont(String fontUrl, String fontName) {DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);DownloadManager.Request request = new DownloadManager.Request(Uri.parse(fontUrl));request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fontName);long downloadId = downloadManager.enqueue(request);// 注册一个广播接收器监听下载完成事件BroadcastReceiver onCompleteReceiver = new BroadcastReceiver() {@Overridepublic void onReceive(Context context, Intent intent) {long receivedDownloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);if (receivedDownloadId == downloadId) {// 下载完成,应用字体String fontPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath() + "/" + fontName;BitmapDrawable updatedWatermarkDrawable = createWatermarkDrawable("Watermark Text", fontPath, 16);Drawable[] updatedLayers = new Drawable[2];updatedLayers[0] = imageDrawable;updatedLayers[1] = updatedWatermarkDrawable;LayerDrawable updatedLayerDrawable = new LayerDrawable(updatedLayers);imageView.setImageDrawable(updatedLayerDrawable);// 注销广播接收器unregisterReceiver(this);}}};registerReceiver(onCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

例子中使用了DownloadManager来下载字体文件,并在下载完成后应用于水印。请注意,在AndroidManifest.xml中,你需要添加相关权限声明:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

另外,记得在合适的时机调用

downloadFont

方法,并提供正确的字体文件URL和文件名。

http://www.lryc.cn/news/167207.html

相关文章:

  • HTTP文件服务
  • nginx配置获取客户端的真实ip
  • 1990-2022上市公司董监高学历工资特征信息数据/上市公司高管信息数据
  • Java程序连接 Mysql 超时问题 - 数据包过大,导致超时,# 配置网络超时时间 socketTimeout: 1800000
  • c++分层最短路(洛谷飞行路线)acwing版
  • Python bs4 BeautifulSoup库使用记录
  • Jmeter系列-插件安装(5)
  • spring aop源码解析
  • 使用Unity的Input.GetAxis(““)控制物体移动、旋转
  • 【CSS】画个三角形或圆形或环
  • AI项目六:基于YOLOV5的CPU版本部署openvino
  • 记录YDLidar驱动包交叉编译时出现的一点问题
  • 嵌入式学习笔记(32)S5PV210的向量中断控制器
  • linux下安装qt、qt触摸屏校准tslib
  • C++之unordered_map,unordered_set模拟实现
  • React Router,常用API有哪些?
  • JVM类加载和双亲委派机制
  • P-MVSNet ICCV-2019 学习笔记总结 译文 深度学习三维重建
  • vueshowpdf 移动端pdf文件预览
  • C#根据excel文件中的表头创建数据库表
  • js通过xpath定位元素并且操作元素以下拉框select为例
  • 数据类型
  • vue 模板应用
  • Golang教程与Gin教程合集,入门到实战
  • 国家网络安全周 | 天空卫士荣获“2023网络安全优秀创新成果大赛优胜奖”
  • Swift学习笔记一(Array篇)
  • C++项目实战——基于多设计模式下的同步异步日志系统-②-前置知识补充-不定参函数
  • C++使用Boost库加入UDP组播时程序崩溃
  • 华为HCIA(四)
  • Qt --- Day01