android屏幕和view的截圖

整個activity或者View組件顯示的內容可以通過cache機制保存爲bitmap, 使用到的api有
    void  setDrawingCacheEnabled(boolean flag),
    Bitmap  getDrawingCache(boolean autoScale),
    void  buildDrawingCache(boolean autoScale),
    void  destroyDrawingCache()
    我們要獲取它的cache先要通過setDrawingCacheEnable方法把cache開啓,然後再調用getDrawingCache方法就可以獲得view的cache圖片了。buildDrawingCache方法可以不用調用,因爲調用getDrawingCache方法時,若果 cache沒有建立,系統會自動調用buildDrawingCache方法生成cache。若果要更新cache, 必須要調用destoryDrawingCache方法把舊的cache銷燬,才能建立新的。
當調用setDrawingCacheEnabled方法設置爲false, 系統也會自動把原來的cache銷燬。
   ViewGroup在繪製子view時,而外提供了兩個方法
   void setChildrenDrawingCacheEnabled(boolean enabled)
   setChildrenDrawnWithCacheEnabled(boolean enabled)
   setChildrenDrawingCacheEnabled方法可以使viewgroup裏所有的子view開啓cache, setChildrenDrawnWithCacheEnabled使在繪製子view時,若該子view開啓了cache, 則使用它的cache進行繪製,從而節省繪製時間。
   獲取cache通常會佔用一定的內存,所以通常不需要的時候有必要對其進行清理,通過destroyDrawingCache或setDrawingCacheEnabled(false)實現。

1. android  屏幕截圖 代碼如下:

 

 

android屏幕截圖 
  
    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 {   
        // 獲取指定Activity的截屏,保存到png文件   
        private static Bitmap takeScreenShot(Activity activity){   
            //View是你需要截圖的View   
            View view = activity.getWindow().getDecorView();   
            view.setDrawingCacheEnabled(true);   
            view.buildDrawingCache();   
            Bitmap b1 = view.getDrawingCache();   
                
            //獲取狀態欄高度   
            Rect frame = new Rect();     
            activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);     
            int statusBarHeight = frame.top;     
            System.out.println(statusBarHeight);   
                
            //獲取屏幕長和高   
            int width = activity.getWindowManager().getDefaultDisplay().getWidth();     
            int height = activity.getWindowManager().getDefaultDisplay().getHeight();     
            //去掉標題欄   
            //Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);   
            Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight);   
            view.destroyDrawingCache();   
            return b;   
        }   
            
        //保存到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();   
            }   
        }   
            
        //程序入口   
        public static void shoot(Activity a){   
            ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");   
        }   
    } 




2.  截取listview中的Item  代碼:

	private AdapterView.OnItemLongClickListener mListenerLongClickMemItem = new AdapterView.OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> adapter, View view,
				int pos, long id) {
			// TODO Auto-generated method stub
			AvcRoomItem 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;
		}
	};


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章