Android generates a long image when taking a screenshot of scrollView beyond the screen size. androidscrollview

Source: Internet
Author: User

Android generates a long image for the scrollView that exceeds the screen size. androidscrollview

Many times, we want to share all the content of an interface, but there is too much content, beyond the size of the screen, simple screenshots can no longer meet our needs, at this time, we can capture the Image Based on the height of the scrollView in the layout.

The Code is as follows:

/*** Capture the scrollview screen * @ param scrollView * @ return */public static Bitmap getBitmapByView (ScrollView scrollView) {int h = 0; Bitmap bitmap = null; // obtain the actual height of the scrollview for (int I = 0; I <scrollView. getChildCount (); I ++) {h + = scrollView. getChildAt (I ). getHeight (); scrollView. getChildAt (I ). setBackgroundColor (Color. parseColor ("# ffffff");} // create a bitmapbitmap = Bitmap. createBitmap (scrollView. getWidth (), H, Bitmap. config. RGB_565); final Canvas canvas = new Canvas (bitmap); scrollView. draw (canvas); return bitmap;}/*** compressed image * @ param image * @ return */public static Bitmap compressImage (Bitmap image) {ByteArrayOutputStream baos = new ByteArrayOutputStream (); // quality compression method. Here, 100 indicates no compression. The compressed data is stored in the baos image. compress (Bitmap. compressFormat. JPEG, 100, baos); int options = 100; // cyclically determine whether the size of the image after compression is greater than kb. S. toByteArray (). length/1024> 100) {// reset baosbaos. reset (); // here, compress options % and store the compressed data in baos image. compress (Bitmap. compressFormat. JPEG, options, baos); // reduce each time by 10 options-= 10;} // Save the compressed data baos to ByteArrayInputStream isBm = new ByteArrayInputStream (baos. toByteArray (); // generate ByteArrayInputStream data to the image Bitmap bitmap = BitmapFactory. decodeStream (isBm, null, null); return bitmap;}/*** save 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 File 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, 90, fos); fos. flush (); fos. close () ;}} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return fname ;}


Call the getBitmapByView () method as needed:

String fname = ScreenShot.savePic(ScreenShot.getBitmapByView(scrollView));

However, in this case, the outofmemory error is reported because the captured image is too long. To avoid memory overflow, you must use Config. RGB_565, will occupy less memory than ARGB_8888. Also, compress the image. At least I will not report the oom error, that is:
String fname = ScreenShot.savePic(ScreenShot.compressImage(ScreenShot.getBitmapByView(scrollView)));



How can I scale an image during the Sliding Process of android scrollview ????

This has nothing to do with scrollview .. If you want to scale the image, first obtain the width and height of the image, and then set the listener to change the width and height of the image proportionally in XX mode ..

Hello, how can I enlarge and drag an image in android over the screen size? Just like in a photo album, I have tried many ways.

Is it necessary to write such a function? Generally, the images in the API provided by android are Bitmap, and you can use Canvas. the drawBItMap method draws an image in the View. In the method, you need to input a parameter of the Matrix type. You can set some parameters of Martrix to enlarge the image, this is just a display, that is, the View layer in MVC, and then rewrite some of the touch functions provided to implement some of the functions you need, such as dragging, is actually changing the position of BitMap painting. Some tutorials can be searched on the Internet for processing touch and Drawing Images. I will not list them. In the end, I need to use the control function to change the image rendering method, this may be the feature you need.

Related Article

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.