直接丢代码了
效果:
Android 添加图片水印(图片+文字)_第1张图片

    /**      * 左下角添加水印(多行,图标 + 文字)     * 参考资料:     * Android 对Canvas的translate方法总结    https://blog.csdn.net/u013681739/article/details/49588549     * @param photo     */    public static Bitmap addWaterMark(Context context,Bitmap photo, List<String> textList,List<Integer> iconIdList,boolean isShowIcon) {        Bitmap newBitmap = photo;        try {            if (null == photo) {                return null;            }            int srcWidth = photo.getWidth();            int srcHeight = photo.getHeight();            //Resources resources = context.getResources();            //float scale = resources.getDisplayMetrics().density;            int unitHeight = srcHeight > srcWidth ? srcWidth/30 : srcHeight / 25;            int padding = unitHeight;            int marginLeft = padding;            int marginBottom = padding;            int textSize = unitHeight;            //创建一个bitmap            if (!newBitmap.isMutable()) {                newBitmap = copy(photo);            }            //将该图片作为画布            Canvas canvas = new Canvas(newBitmap);            // 设置画笔            TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);            textPaint.setTextSize(textSize);// 字体大小            textPaint.setTypeface(Typeface.DEFAULT);// 采用默认的宽度            textPaint.setColor(Color.WHITE);// 采用的颜色v            Rect bounds = new Rect();            String gText = "hello world!";            textPaint.getTextBounds(gText, 0, gText.length(), bounds);            int iconWidth = bounds.height();//图片宽度            int maxTextWidth = srcWidth - padding*3 - iconWidth;//最大文字宽度            for (int i = textList.size() - 1 ; i >=0 ; i--){                String text = textList.get(i);                int iconId = iconIdList.get(i);                canvas.save();//锁画布(为了保存之前的画布状态)                //文字处理                StaticLayout layout = new StaticLayout(text, textPaint, maxTextWidth, Layout.Alignment.ALIGN_NORMAL,                        1.0f, 0.0f, true); // 确定换行                //在画布上绘制水印图片                if (isShowIcon){                    Bitmap watermark= BitmapFactory.decodeResource(context.getResources(),iconId);                    int iconHeight = iconWidth * ((watermark.getHeight()*1000)/watermark.getWidth())/1000;//维持图片宽高比例,也可以简单粗暴 iconHeight = iconWidth;                    //图片相对文字位置居中                    RectF rectF=new RectF(marginLeft,srcHeight - marginBottom - layout.getHeight()/2 - iconHeight/2                            ,marginLeft + iconWidth,srcHeight - marginBottom - layout.getHeight()/2 + iconHeight/2);                    canvas.drawBitmap(watermark,null,rectF,null);//限定图片显示范围                }                //绘制文字                canvas.translate(isShowIcon ? marginLeft + iconWidth + padding : marginLeft, srcHeight - layout.getHeight() - marginBottom); // 设定画布位置                layout.draw(canvas); // 绘制水印                //marginBottom 更新                marginBottom = marginBottom + (padding + layout.getHeight());                canvas.restore();//把当前画布返回(调整)到上一个save()状态之前            }            // 保存            canvas.save(Canvas.ALL_SAVE_FLAG);            // 存储            canvas.restore();        } catch (Exception e) {            e.printStackTrace();            return newBitmap;        }        return newBitmap;    }
    /**     * 根据原位图生成一个新的位图,并将原位图所占空间释放     *     * @param srcBmp 原位图     * @return 新位图     */    public static Bitmap copy(Bitmap srcBmp) {        Bitmap destBmp = null;        try {            // 创建一个临时文件            File file = new File(Constant.PATH + "temppic/tmp.txt");            if (file.exists()) {// 临时文件 , 用一次删一次                file.delete();            }            file.getParentFile().mkdirs();            RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw");            int width = srcBmp.getWidth();            int height = srcBmp.getHeight();            FileChannel channel = randomAccessFile.getChannel();            MappedByteBuffer map = channel.map(FileChannel.MapMode.READ_WRITE, 0, width * height * 4);            // 将位图信息写进buffer            srcBmp.copyPixelsToBuffer(map);            // 释放原位图占用的空间            srcBmp.recycle();            // 创建一个新的位图            destBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);            map.position(0);            // 从临时缓冲中拷贝位图信息            destBmp.copyPixelsFromBuffer(map);            channel.close();            randomAccessFile.close();            file.delete();        } catch (Exception ex) {            ex.printStackTrace();            destBmp = null;            return srcBmp;        }        return destBmp;    }

更多相关文章

  1. android上传图片到服务器,求服务器那边和android的Activity的完整
  2. Android中上传图片到服务端C#接收图
  3. Android获取手机本地图片缩略图
  4. android imageswitcher gallery 根据数据库内图片名字进行查看/
  5. Android OkHttp3 上传多张图片
  6. Android中TextView中加图片,超链接,一部分字或者背景变色。不断补
  7. flutter包名、应用名称、图标、启动图片修改
  8. 通过xml布局文件实现按钮改变焦点设置背景图片
  9. Android中Bitmap.createBitmap重载方总结 和 Android实现位图剪

随机推荐

  1. android生命周期神器--Lifecycle
  2. 微信转发度最高的十大Android文章
  3. 第一章 Adnroid体系与系统架构
  4. android 使用JavaMail发送邮件
  5. 【Error】MPermissions引入错误 android-
  6. 【Android】第21章 2D图形和动画
  7. ToolBar 去掉默认左间距
  8. Android 下拉框第三方控件 NiceSpinner
  9. 【Android okhttp源码解析 二】同步请求
  10. Android基于XMPP Smack openfire 开发的