Android gives ScrollView screenshot over screen size to form a long picture

Source: Internet
Author: User

A lot of times, we want to share all the content of an interface, but too much content, more than the size of the screen, simple screenshot has not satisfied our needs, this time we can according to the layout of scrollview height to intercept the picture.

The code is as follows:

/** * Intercept ScrollView's screen * @param scrollView * @return */public static Bitmap Getbitmapbyview (ScrollView scrollView) {int h = 0; Bitmap Bitmap = null;//gets scrollView actual height for (int i = 0; i < Scrollview.getchildcount (); i++) {H + = Scrollview.getchilda T (i). GetHeight (); Scrollview.getchildat (i). SetBackgroundColor (Color.parsecolor ("#ffffff"));} Create a corresponding size Bitmapbitmap = Bitmap.createbitmap (Scrollview.getwidth (), h,bitmap.config.rgb_565); final canvas canvas = new Canvas (bitmap); Scrollview.draw (canvas); return bitmap;} /** * Compress picture * @param image * @return */public static Bitmap compressimage (Bitmap image) {Bytearrayoutputstream BAOs = new B Ytearrayoutputstream ();//Quality compression method, here 100 means no compression, the compressed data stored in the BAOs image.compress (Bitmap.CompressFormat.JPEG, BAOs); The int options = 100;//Loop Determines if the compressed image is greater than 100kb, which is greater than the continue compression while (Baos.tobytearray (). length/1024 > 100) {//reset Baosbaos.reset (); This compresses the options%, storing the compressed data in the BAOs image.compress (Bitmap.CompressFormat.JPEG, Options, BAOs);//Reduce 10options = 10 per time;} Store the compressed data BAOs to byTearrayinputstream Bytearrayinputstream ISBM = new Bytearrayinputstream (Baos.tobytearray ());// Generate picture of bytearrayinputstream data bitmap bitmap = Bitmapfactory.decodestream (ISBM, NULL, NULL); return bitmap;} /** * Saved to SDcard * @param b * @return */public static String savepic (Bitmap b) {SimpleDateFormat SDF = new SimpleDateFormat ("Yyyy-mm-dd_hh-mm-ss", locale.us); File outfile = new file ("/sdcard/image");//If the file does not exist, create a new one if (!outfile.isdirectory ()) {try {outfile.mkdir ();} catch ( Exception e) {e.printstacktrace ();}} String fname = outfile + "/" + Sdf.format (new Date ()) + ". png"; FileOutputStream fos = null;try {fos = new FileOutputStream (fname); if (null! = FOS) {b.compress (Bitmap.CompressFormat.PNG , Fos.flush (), FOS); Fos.close ();}} catch (FileNotFoundException e) {e.printstacktrace ();} catch (IOException e) {e.printstacktrace ();} return fname;}


Call the Getbitmapbyview () method where you need to use itcan:

String fname = Screenshot.savepic (Screenshot.getbitmapbyview (ScrollView));

But this is sometimes because the interception of the picture is too long and too large to report OutOfMemory's fault, so in order to avoid memory overflow, program collapse, pay attention to use config.rgb_565, will be less than argb_8888 memory. There is the picture compression, at least I did not report oom wrong, namely:
String fname = Screenshot.savepic (Screenshot.compressimage (Screenshot.getbitmapbyview (ScrollView)));


Android gives ScrollView over screen size to form long graphs

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.