Android generate share long graph and add full image watermark

Source: Internet
Author: User

Respect the fruits of others ' work. Reprint please indicate source: http://blog.csdn.net/gengqiquan/article/details/65938021. This article is from: "Gengqiquan's Blog"

The leader recently thought that Ctrip's screenshot growth chart sharing effect is better, so we added a. Products think to share the long map to add the company brand watermark, so we also added a. Well, the cause of the incident is this.
Long graphs are usually scrollview and ListView.


We need to get a picture of the full display of both controls.

The principle is very easy to make a canvas that is consistent with the width of the control (that is, to create a bitmap of equal height and width). Then call the control's draw method to draw yourself onto the canvas.
A method for fetching long graphs of two controls separately

 /** * Intercept ScrollView's screen **/     Public StaticBitmapGetscrollviewbitmap(ScrollView ScrollView) {inth =0; Bitmap Bitmap; for(inti =0; I < Scrollview.getchildcount ();        i++) {H + = Scrollview.getchildat (i). GetHeight (); }//Create a corresponding size bitmapBitmap = Bitmap.createbitmap (Screenutils.getscreenwidth (Scrollview.getcontext ()), H, Bitmap.Config.ARGB_444 4);FinalCanvas Canvas =NewCanvas (bitmap); Canvas.drawcolor (Color.parsecolor ("#f2f7fa")); Scrollview.draw (canvas);returnBitmap }
 /** * ListView **/     Public StaticBitmapGetlistviewbitmap(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 corresponding size bitmapBitmap = 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, -, out);                Out.flush ();            Out.close (); }        }Catch(IOException e) {        }returnBitmap }

A way to get a detailed view of the display graph

    /**     * 生成某个view的图片     *     * @author gengqiquan     * @date 2017/3/20 上午10:34     */    publicstaticgetViewDrawingCacheBitmap(View view) {        view = view.getRootView();        if (!view.isDrawingCacheEnabled()) {            view.setDrawingCacheEnabled(true);        }        view.destroyDrawingCache();        view.buildDrawingCache();        Bitmap bm = view.getDrawingCache();        view.setDrawingCacheEnabled(false);        return bm;    }

And a way to generate a linearlayout picture.

/** * Generate a picture of a linearlayout * * @author Gengqiquan * @date 2017/3/20 morning 10:34 */     Public StaticBitmapGetlinearlayoutbitmap(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 corresponding size bitmapBitmap 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. Well. Good people do low, and then send a mosaic image method

/*** Mosaic image * @param First shared long image * @param Second Company logo * @author gengqiquan* @date 2017/3/25 pm 4:56*/
          Public StaticBitmapAdd2bitmap(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

/** * @param First original image * @param Mark Watermark * @author Gengqiquan * @date /c4> 2017/3/25 PM 4:58 * *      Public StaticBitmapWaterMark(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,0HNULL);        H = h + mark.getheight (); }returnFirst }

There's a bug in the pit Daddy Chrome. At this point, a line-break connection up button will crash. Wrote a blog crash seven or eight times. It's really bad luck in the near future.

In fact, I would like to say: Because of the recent injection of an object, the dependency is relatively strong. So this period of time very little blog, back to fill up.

Do you have any suggestions to leave a message?

Let's say my blog is helpful to you. Please leave a message to inspire or order a praise!

I built a QQ group (group number: 121606151) for everyone to discuss the exchange of Android technology issues. Interested to be able to add. We make progress together.

Android generate share long graph and add full image watermark

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.