Android generates shared long graphs and adds full-image watermarks

Source: Internet
Author: User

Reprinted from: http://blog.csdn.net/gengqiquan/article/details/65938021

The leader recently felt that Ctrip's screenshot of the growth chart sharing effect is better, so we also added a, product feel to share out of the long map needs to add the company brand watermark, so we also added A; Well, the cause of the incident is this.
Long graphs are generally scrollview and ListView.
We need to get a picture of the full display of both controls. The principle is simple, make a canvas that is consistent with the width of the control (that is, create a bitmap with a high width equal). Then call the control's draw method to draw yourself onto the canvas.
A method for fetching long graphs of two controls separately

 /*** Capture ScrollView's screen **/     Public StaticBitmap Getscrollviewbitmap (ScrollView ScrollView) {intH = 0;        Bitmap Bitmap;  for(inti = 0; I < Scrollview.getchildcount (); i++) {h+=Scrollview.getchildat (i). GetHeight (); }        //Create a bitmap of the corresponding sizeBitmap=Bitmap.createbitmap (Screenutils.getscreenwidth (Scrollview.getcontext ()), H, Bitmap.Config.ARGB_4444); FinalCanvas Canvas =NewCanvas (bitmap); Canvas.drawcolor (Color.parsecolor ("#f2f7fa"));        Scrollview.draw (canvas); returnbitmap; }

 /*** ListView **/     Public StaticBitmap Getlistviewbitmap (ListView ListView, String Picpath) {intH = 0;        Bitmap Bitmap; //get listview Actual Height         for(inti = 0; I < Listview.getchildcount (); i++) {h+=Listview.getchildat (i). GetHeight ();        }listview.getheight ()); //Create a bitmap of the corresponding sizeBitmap =Bitmap.createbitmap (Listview.getwidth (), H, Bitmap.Config.RGB_565); FinalCanvas Canvas =NewCanvas (bitmap);        Canvas.drawcolor (Color.White);        Listview.draw (canvas); //Test OutputFileOutputStream out =NULL; Try{ out=NewFileOutputStream (Picpath); } Catch(FileNotFoundException e) {e.printstacktrace (); }        Try {            if(NULL!=Out ) {bitmap.compress (Bitmap.CompressFormat.PNG,100, out);                Out.flush ();            Out.close (); }        } Catch(IOException e) {}returnbitmap; }

A way to get a display diagram of a specific view

    /*** Create a picture of a view * *@authorGengqiquan * @date 2017/3/20 a.m. 10:34*/     Public StaticBitmap getviewdrawingcachebitmap (view view) {View=View.getrootview (); if(!view.isdrawingcacheenabled ()) {view.setdrawingcacheenabled (true);        } view.destroydrawingcache ();        View.builddrawingcache (); Bitmap BM=View.getdrawingcache (); View.setdrawingcacheenabled (false); returnBM; }

And a way to generate a linearlayout picture.

/*** Create a picture of a linearlayout * *@authorGengqiquan * @date 2017/3/20 a.m. 10:34*/     Public StaticBitmap Getlinearlayoutbitmap (linearlayout linearlayout) {intH = 0; //get linearlayout actual height         for(inti = 0; I < Linearlayout.getchildcount (); i++) {Linearlayout.getchildat (i). Measure (0, 0); H+=Linearlayout.getchildat (i). Getmeasuredheight (); } linearlayout.measure (0, 0); //Create a bitmap of the corresponding sizeBitmap Bitmap =Bitmap.createbitmap (Linearlayout.getmeasuredwidth (), H, Bitmap.Config.RGB_565); FinalCanvas Canvas =NewCanvas (bitmap);        Canvas.drawcolor (Color.White);        Linearlayout.draw (canvas); returnbitmap; }

The product will definitely let you add the company logo image below or above, uh. Good people do low, and then send a mosaic image method

/*** Stitching Pictures *@paramFirst Shared Long chart *@paramSecond Company logo map *@authorgengqiquan* @date 2017/3/25 pm 4:56*/     Public StaticBitmap Add2bitmap (Bitmap First, Bitmap second) {floatScale = ((float) First.getwidth ())/second.getwidth (); Second=imageutil.scaleimg (second, scale); intwidth =first.getwidth (); intHeight = first.getheight () +second.getheight (); Bitmap result=bitmap.createbitmap (width, height, Bitmap.Config.ARGB_4444); Canvas Canvas=NewCanvas (Result); Canvas.drawbitmap (First,0, 0,NULL); Canvas.drawbitmap (Second,0, First.getheight (),NULL); returnresult; }

Another way to add a full-image watermark

/**     * @paramFirst Original map *@paramMark Watermark *@authorGengqiquan * @date 2017/3/25 pm 4:58*/      Public StaticBitmap WaterMark (Bitmap First, Bitmap mark) {floatScale = ((float) First.getwidth ())/mark.getwidth (); Mark=imageutil.scaleimg (mark, scale); intHeight =first.getheight (); Canvas Canvas=NewCanvas (first); intH = 0;  while(H < height +mark.getheight ()) {Canvas.drawbitmap (Mark,0, H,NULL); H= h +mark.getheight (); }        returnFirst ; }

Other:

Http://www.cnblogs.com/meieiem/archive/2012/08/15/2639543.html Android for Image Watermark

Android generates shared long graphs and adds full-image watermarks

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.