Android Watermark a picture

Source: Internet
Author: User

Speaking of the watermark is actually very simple, is to draw on a picture of another picture or text

The implementation is as follows:

Package Com.tang.watermark;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.bitmapfactory;import Android.graphics.canvas;import Android.graphics.pixelformat;import Android.graphics.bitmap.config;import Android.graphics.drawable.bitmapdrawable;import Android.graphics.drawable.drawable;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;import Android.widget.imageview;public Class Mainactivity extends Activity {ImageView ImageView; Bitmap mark; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); ImageView = (ImageView) Findviewbyid (R.ID.IMAGEVIEW1); Imageview.setimageresource (R.drawable.heihei); mark = Bitmapfactory.decoderesource (Getresources (), R.drawable.ic_ Launcher); Button button = (button) Findviewbyid (R.id.button1); Button.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {// TODO auto-generated Method Stubbitmap temp = Watermark (Drawabletobitmap (imageview.getdrawable ()), mark,50,100);    Imageview.setimagebitmap (temp);}});} /** * Convert bitmap to drawable * @param bitmap * @return */public static drawable bitmaptodrawable (bitmap bi    TMAP) {bitmapdrawable bd = new bitmapdrawable (bitmap);    return BD; }/** * @param src * @param bg * @param color * @return */public static Bitmap watermark (Bi      TMap src,bitmap mark,int x,int y) {int w = src.getwidth ();      int h = src.getheight ();    Bitmap newb = Bitmap.createbitmap (W, H, config.argb_8888);       Canvas CV = new canvas (NEWB);     Cv.drawbitmap (SRC, 0, 0, NULL);    Cv.drawbitmap (mark,x, y, null);        Cv.save (Canvas.all_save_flag);//Save Cv.restore ();//Store return NEWB;} /** * Drawable converted to Bitmap * @param drawable * @return */public static Bitmap Drawabletobitmap (drawable Draw ABLE) {//Take drawable's long width int w =Drawable.getintrinsicwidth ();           int h = drawable.getintrinsicheight (); Take drawable color format bitmap.config Config = drawable.getopacity ()! = Pixelformat.opaque?          Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;           Bitmap Bitmap = Bitmap.createbitmap (w, H, config);          Canvas canvas = new canvas (bitmap);          Drawable.setbounds (0, 0, W, h);          Drawable.draw (canvas);      return bitmap; }  }


Android Watermark 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.