Simply add watermarks to images and add watermarks to images
1 package com. zzw. addwatermark; 2 3 import android. app. activity; 4 import android. graphics. bitmap; 5 import android. graphics. canvas; 6 import android. graphics. bitmap. config; 7 import android. graphics. color; 8 import android. graphics. paint; 9 import android. graphics. drawable. bitmapDrawable; 10 import android. graphics. drawable. drawable; 11 import android. OS. bundle; 12 import android. view. menu; 13 import android. view. menuItem; 14 import android. widget. imageView; 15 16 17 public class MainActivity extends Activity {18 19 @ Override20 protected void onCreate (Bundle savedInstanceState) {21 super. onCreate (savedInstanceState); 22 setContentView (R. layout. activity_main); 23 24 ImageView imageView = (ImageView) findViewById (R. id. imageView); 25 26 // use an image in drawable. For example, my avatar is 27 Drawable drawable = getResources (). getDrawable (R. drawable. test); 28 29 // converts Drawable to Bitmap30 BitmapDrawable bd = (BitmapDrawable) drawable; 31 Bitmap bm = bd. getBitmap (); 32 33 // Add watermark for Bitmap 34 Bitmap bitmap = createWatermark (bm, "www.cnblogs.com/zzw1994"); 35 imageView. setImageBitmap (bitmap); 36} 37 38 // Add the watermark text 39 to the image target // Bitmap target: the image added with the watermark 40 // String mark: watermark Article 41 private Bitmap createWatermark (Bitmap target, String mark) {42 int width = target. getWidth (); 43 int height = target. getHeight (); 44 45 Bitmap bitmap = Bitmap. createBitmap (width, height, Config. ARGB_8888); 46 Canvas canvas = new Canvas (bitmap); 47 48 Paint paint = new Paint (); 49 50 // set the watermark color 51 paint. setColor (Color. RED); 52 // set the watermark font size 53 paint. setTextSize (10); 54 55 // de-sawtooth 56 paint. setAntiAlias (true); 57 // get the content of the original image 58 canvas. drawBitmap (target, 0, 0, paint); 59 // Add the watermark 60 canvas at the bottom left. drawText (mark, 0, height, paint); 61 62 canvas. save (Canvas. ALL_SAVE_FLAG); 63 canvas. restore (); 64 65 66 return bitmap; 67} 68 69}