Android generates shared long graphs and adds full-image watermarks

Source: Internet
Author: User

Respect for the work of others, reproduced please indicate the source: http://blog.csdn.net/gengqiquan/article/details/65938021, this article from: "Gengqiquan blog"

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

 /** * 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 bitmap of corresponding sizeBitmap = 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 bitmap of 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, -, out);                Out.flush ();            Out.close (); }        }Catch(IOException e) {        }returnBitmap }

A way to get a display diagram of a specific view

    /**     * 生成某个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 bitmap of 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

/*** 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);intwidth = First.getwidth ();intHeight = first.getheight ();        Bitmap result = Bitmap.createbitmap (width, height, Bitmap.Config.ARGB_4444); Canvas Canvas =NewCanvas (result); Canvas.drawbitmap (First,0,0,NULL);inth =0; while(H < height + mark.getheight ()) {Canvas.drawbitmap (Mark,0HNULL);        H = h + mark.getheight (); }returnResult }

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 lately.

Actually I want to say: Because recently was injected an object, the dependence is relatively strong, so this period of time seldom writes the blog, later complements.

Do you have any suggestions to leave a message?

If my blog is helpful to you, please leave a message to encourage or click to praise it!

I built a QQ group (Group No.: 121606151), for everyone to discuss the exchange of Android technology issues, interested can add, we progress together.

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.