Simply add a watermark to a picture

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.