Screen implementation methods for Android programming (including ScrollView and ListView) _android

Source: Internet
Author: User
Tags flush

This example describes the screen implementation of Android programming. Share to everyone for your reference, specific as follows:

public class Screenshot {//Get screenshots of specified activity, save to PNG file public static Bitmap Takescreenshot (activity activity) {/Vie
  W is the View View view that you need to take a screenshot = Activity.getwindow (). Getdecorview ();
  View.setdrawingcacheenabled (TRUE);
  View.builddrawingcache ();
  Bitmap B1 = View.getdrawingcache ();
  Gets the status bar height Rect frame = new Rect ();
  Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
  int statusbarheight = Frame.top;
  System.out.println (Statusbarheight);
  Gets the screen length and high int width = Activity.getwindowmanager (). Getdefaultdisplay (). GetWidth ();
  int height = Activity.getwindowmanager (). Getdefaultdisplay (). GetHeight ();
  Remove title bar//Bitmap B = Bitmap.createbitmap (B1, 0, 25, 320, 455);
  Bitmap B = Bitmap.createbitmap (b1, 0, Statusbarheight, width, height-statusbarheight);
  View.destroydrawingcache ();
  Savepic (b, "/sdcard/screen_test.png");
 return b; }//Save to SDcard public static void Savepic (Bitmap B, String strFileName) {FileOutputStream fos = null;
  try {fos = new FileOutputStream (strFileName);
    if (null!= fos) {b.compress (Bitmap.CompressFormat.PNG, N, FOS);
    Fos.flush ();
   Fos.close ();
  } catch (FileNotFoundException e) {e.printstacktrace ();
  catch (IOException e) {e.printstacktrace (); }/** * Converts the View object to Bitmap */public static Bitmap Convertviewtobitmap (view view) {View.measure (measurespec.ma
  Kemeasurespec (0, measurespec.unspecified), Measurespec.makemeasurespec (0, measurespec.unspecified));
  View.layout (0, 0, view.getmeasuredwidth (), View.getmeasuredheight ());
  View.builddrawingcache ();
  Bitmap Bitmap = View.getdrawingcache ();
   if (bitmap!= null) {SYSTEM.OUT.PRINTLN ("This is not Nullde1");
  LOG.D ("Nullde1", "nullde1");
  else {System.out.println ("this Nullnulllnulnlul");
 return bitmap; ///Program entry 1 public static void shoot (activity a) {Screenshot.savepic (Screenshot.takescreenshot (a), "/sdcard/screen_tes
 T.png "); ///Program entry 2 public static void Shootview (ViewView) {Screenshot.savepic (Screenshot.convertviewtobitmap (view), "Sdcard/xx.png");
  public static Bitmap Getviewbitmap (View v) {v.clearfocus ();
  V.setpressed (FALSE);
  Boolean willnotcache = V.willnotcachedrawing ();
  V.setwillnotcachedrawing (FALSE); Reset the drawing cache background color to fully transparent//for the duration of this operation int color = V.G
  Etdrawingcachebackgroundcolor ();
  V.setdrawingcachebackgroundcolor (0);
  if (color!= 0) {v.destroydrawingcache ();
  } v.builddrawingcache ();
  Bitmap Cachebitmap = V.getdrawingcache ();
   if (Cachebitmap = = null) {LOG.E ("ttttttttactivity", "Failed Getviewbitmap (" + V + ")", New RuntimeException ());
  return null;
  } Bitmap Bitmap = Bitmap.createbitmap (Cachebitmap);
  Restore the View V.destroydrawingcache ();
  V.setwillnotcachedrawing (Willnotcache);
  V.setdrawingcachebackgroundcolor (color);
 return bitmap; /** * intercept scrollview screen * **/public static Bitmap Getbitmapbyview(ScrollView ScrollView)
  {int h = 0;
  Bitmap Bitmap = null; Gets the listview actual height for (int i = 0; i < Scrollview.getchildcount (); i++) {H + = Scrollview.getchildat (i). GetHeight ()
   ;
  Scrollview.getchildat (i). Setbackgroundresource (R.DRAWABLE.BG3);
  LOG.D (TAG, "actual height:" + h);
  LOG.D (TAG, "Height:" + scrollview.getheight ());
  Create a corresponding size of bitmap bitmap = Bitmap.createbitmap (Scrollview.getwidth (), H, Bitmap.Config.ARGB_8888);
  Final Canvas Canvas = new Canvas (bitmap);
  Scrollview.draw (canvas);
  Test output FileOutputStream out = null;
  try {out = new FileOutputStream ("/sdcard/screen_test.png");
  catch (FileNotFoundException e) {e.printstacktrace ();
    try {if (null!= out) {bitmap.compress (Bitmap.CompressFormat.PNG, N, out);
    Out.flush ();
   Out.close ();
 } catch (IOException e) {//Todo:handle exception} return bitmap;
 private static String TAG = "Listview and ScrollView Item screenshot:"; /** * Screenshot ListView * **/public staticBitmap Getbbitmap (ListView ListView) {int h = 0;
  Bitmap Bitmap = null;
  Gets the ListView actual height for (int i = 0; i < Listview.getchildcount (); i++) {H + = Listview.getchildat (i). GetHeight ();
  LOG.D (TAG, "actual height:" + h);
  LOG.D (TAG, "list height:" + listview.getheight ());
  Create a corresponding size of bitmap bitmap = Bitmap.createbitmap (Listview.getwidth (), H, Bitmap.Config.ARGB_8888);
  Final Canvas Canvas = new Canvas (bitmap);
  Listview.draw (canvas);
  Test output FileOutputStream out = null;
  try {out = new FileOutputStream ("/sdcard/screen_test.png");
  catch (FileNotFoundException e) {e.printstacktrace ();
    try {if (null!= out) {bitmap.compress (Bitmap.CompressFormat.PNG, N, out);
    Out.flush ();
   Out.close ();
 } catch (IOException e) {//Todo:handle exception} return bitmap;

 }
}

I hope this article will help you with your Android programming.

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.