/** * 给图片添加水印 * @param srcBitmap 资源图片 * @param waterBitmap 水印图片 * @param title 文字 * @param alpha 透明度 0-255 * @return */ public Bitmap addWaterBitmap(Bitmap srcBitmap, Bitmap waterBitmap, String title,int alpha) { if (srcBitmap == null||waterBitmap==null) return null; int width = srcBitmap.getWidth(); int height = srcBitmap.getHeight(); int waterWidth = waterBitmap.getWidth(); int waterHeight = waterBitmap.getHeight(); //创建一个和srcBitmap宽和高相同的新Bitmap Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565); //新建画笔 Paint paint = new Paint(); paint.setAlpha(alpha); paint.setAntiAlias(true); //以新Bitmap创建画布 Canvas canvas = new Canvas(newBitmap); //画资源Bitmap canvas.drawBitmap(srcBitmap, 0, 0, paint); //srcBitmap右上角画waterBitmap canvas.drawBitmap(waterBitmap, width-waterWidth-5, 5, paint); String famliyName = "宋体"; Typeface typeface =Typeface.create(famliyName,Typeface.NORMAL); TextPaint textPaint = new TextPaint(); textPaint.setColor(Color.WHITE); textPaint.setTypeface(typeface); textPaint.setTextSize(40); int textWidth = (int) textPaint.measureText(title); canvas.drawText(title,width-textWidth-5,height-20,textPaint); canvas.save(Canvas.ALL_SAVE_FLAG); canvas.restore(); return newBitmap; }
给图片添加水印图片文字
最新推荐文章于 2019-06-11 16:25:13 发布