Screenshots of Android screens and views

Source: Internet
Author: User

The content displayed by the entire activity or view component can be saved as bitmap through the cache mechanism. The APIs used include
Void setdrawingcacheenabled (Boolean flag ),
Bitmap getdrawingcache (Boolean autoscale ),
Void builddrawingcache (Boolean autoscale ),
Void destroydrawingcache ()
To obtain its cache, you must first enable the cache using the setdrawingcacheenable method, and then call the getdrawingcache method to obtain the cache image of the view. You do not need to call the builddrawingcache method, because when you call the getdrawingcache method, if the cache is not created, the system automatically calls the builddrawingcache method to generate the cache. To update the cache, you must call the destorydrawingcache method to destroy the old cache to create a new one.
When the setdrawingcacheenabled method is called, the system automatically destroys the original cache.
Viewgroup provides two methods to draw a child view.
Void setchildrendrawingcacheenabled (Boolean enabled)
Setchildrendrawnwithcacheenabled (Boolean enabled)
The setchildrendrawingcacheenabled method enables cache for all sub-views in the viewgroup, and setchildrendrawnwithcacheenabled enables the Sub-view to be drawn when the cache is enabled, this saves the Painting time.
Obtaining a cache usually occupies a certain amount of memory, so it is usually unnecessary to clean it, through destroydrawingcache or setdrawingcacheenabled (false.

1. the android screen code is as follows:

 

 

Android screen import Java. io. filenotfoundexception; import Java. io. fileoutputstream; import Java. io. ioexception; import android. app. activity; import android. graphics. bitmap; import android. graphics. rect; import android. view. view; public class screenshot {// obtain the screenshot of the specified activity and save it to the PNG file Private Static bitmap takescreenshot (activity) {// view is the view = activity you need. getwindow (). getdecorview (); View. setdrawingcacheenabled (true); view. builddrawingcache (); bitmap b1 = view. getdrawingcache (); // get the rect frame of the status bar = new rect (); activity. getwindow (). getdecorview (). getwindowvisibledisplayframe (FRAME); int statusbarheight = frame. top; system. out. println (statusbarheight); // get the screen length and height int width = activity. getwindowmanager (). getdefadisplay display (). getwidth (); int Height = activity. getwindowmanager (). g Etdefaultdisplay (). getheight (); // remove the title bar // bitmap B = bitmap. createbitmap (B1, 0, 25,320,455); bitmap B = bitmap. createbitmap (B1, 0, statusbarheight, width, height-statusbarheight); view. destroydrawingcache (); return B;} // save it to sdcard Private Static void savepic (Bitmap B, string strfilename) {fileoutputstream Fos = NULL; try {Fos = new fileoutputstream (strfilename ); if (null! = FOS) {B. compress (bitmap. compressformat. PNG, 90, FOS); FOS. flush (); FOS. close () ;}} catch (filenotfoundexception e) {e. printstacktrace ();} catch (ioexception e) {e. printstacktrace () ;}// program entry public static void shoot (activity a) {screenshot. savepic (screenshot. takescreenshot (a), "sdcard/xx.png ");}}

2. Intercept the item code in listview:

private AdapterView.OnItemLongClickListener mListenerLongClickMemItem = new AdapterView.OnItemLongClickListener() {@Overridepublic boolean onItemLongClick(AdapterView<?> adapter, View view,int pos, long id) {// TODO Auto-generated method stubAvcRoomItem item = mCore.getRoomShowMembers().get(pos);if (item.getmType() == RoomItemType.ROOM_ITEM_CARD&& mCore.getMyself().isPresider()) {view.destroyDrawingCache();view.setDrawingCacheEnabled(true);Bitmap bm = Bitmap.createBitmap(view.getDrawingCache());if (!mTemplate.isShown())mCore.doShowPScreen();mTempDevItem = item;startDrag(bm, (int) mListMember.getLastMotionEvent().getRawX(),(int) mListMember.getLastMotionEvent().getRawY());}return false;}};

 

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.