1 PackageCom.zzw.addwatermark;2 3 Importandroid.app.Activity;4 ImportAndroid.graphics.Bitmap;5 ImportAndroid.graphics.Canvas;6 ImportAndroid.graphics.Bitmap.Config;7 ImportAndroid.graphics.Color;8 ImportAndroid.graphics.Paint;9 Importandroid.graphics.drawable.BitmapDrawable;Ten Importandroid.graphics.drawable.Drawable; One ImportAndroid.os.Bundle; A ImportAndroid.view.Menu; - ImportAndroid.view.MenuItem; - ImportAndroid.widget.ImageView; the - - Public classMainactivityextendsActivity { - + @Override - protected voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (r.layout.activity_main); at -ImageView imageview=(ImageView) Findviewbyid (R.id.imageview); - - //use one of the pictures in the drawable, my avatar as an example -Drawable drawable=getresources (). getdrawable (r.drawable.test); - in //convert drawable to bitmap -Bitmapdrawable bd=(bitmapdrawable) drawable; toBitmap bm=Bd.getbitmap (); + - //add a watermark to bitmap theBitmap Bitmap=createwatermark (BM, "www.cnblogs.com/zzw1994"); * Imageview.setimagebitmap (bitmap); $ }Panax Notoginseng - //add watermark text to the image target the //Bitmap Target: Watermark-Added picture + //String mark: Watermark article A PrivateBitmap Createwatermark (Bitmap target,string mark) { the intWidth=target.getwidth (); + intheight=target.getheight (); - $Bitmap bitmap=bitmap.createbitmap (width, height, config.argb_8888); $Canvas canvas=NewCanvas (bitmap); - -Paint paint=NewPaint (); the - //set the color of the watermarkWuyi Paint.setcolor (color.red); the //set the size of the watermark font -Paint.settextsize (10); Wu - //de-aliasing AboutPaint.setantialias (true); $ //Get the contents of the original picture -Canvas.drawbitmap (target, 0, 0, paint); - //start adding watermarks at the bottom left position -Canvas.drawtext (Mark, 0, height, paint); A + Canvas.save (canvas.all_save_flag); the Canvas.restore (); - $ the returnbitmap; the } the the}
Simply add a watermark to a picture