Android開發(fā)截屏截圖方法匯總(Activity州刽、View空执、ScrollView、ListView穗椅、RecyclerView辨绊、WebView截屏截圖)

Android截屏的原理:獲取具體需要截屏的區(qū)域的Bitmap,然后繪制在畫布上匹表,保存為圖片后進(jìn)行分享或者其它用途

一门坷、Activity截屏

1、截Activity界面(包含空白的狀態(tài)欄)

/** 
     * 根據(jù)指定的Activity截圖(帶空白的狀態(tài)欄) 
     * 
     * @param context 要截圖的Activity 
     * @return Bitmap 
     */  
    public static Bitmap shotActivity(Activity context) {  
        View view = context.getWindow().getDecorView();  
        view.setDrawingCacheEnabled(true);  
        view.buildDrawingCache();  
  
        Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());  
        view.setDrawingCacheEnabled(false);  
        view.destroyDrawingCache();  
        return bitmap;  
    }  

2袍镀、截Activity界面(去除狀態(tài)欄)

/** 
     * 根據(jù)指定的Activity截圖(去除狀態(tài)欄) 
     * 
     * @param activity 要截圖的Activity 
     * @return Bitmap 
     */  
    public Bitmap shotActivityNoStatusBar(Activity activity) {  
        // 獲取windows中最頂層的view  
        View view = activity.getWindow().getDecorView();  
        view.buildDrawingCache();  
  
        // 獲取狀態(tài)欄高度  
        Rect rect = new Rect();  
        view.getWindowVisibleDisplayFrame(rect);  
        int statusBarHeights = rect.top;  
        Display display = activity.getWindowManager().getDefaultDisplay();  
  
        // 獲取屏幕寬和高  
        int widths = display.getWidth();  
        int heights = display.getHeight();  
  
        // 允許當(dāng)前窗口保存緩存信息  
        view.setDrawingCacheEnabled(true);  
  
        // 去掉狀態(tài)欄  
        Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(), 0,  
                statusBarHeights, widths, heights - statusBarHeights);  
  
        // 銷毀緩存信息  
        view.destroyDrawingCache();  
  
        return bmp;  
    }  

二默蚌、View截屏

/** 
 * 根據(jù)指定的view截圖 
 * 
 * @param v 要截圖的view 
 * @return Bitmap 
 */  
public static Bitmap getViewBitmap(View v) {  
    if (null == v) {  
        return null;  
    }  
    v.setDrawingCacheEnabled(true);  
    v.buildDrawingCache();  
    if (Build.VERSION.SDK_INT >= 11) {  
        v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(), View.MeasureSpec.EXACTLY),  
                View.MeasureSpec.makeMeasureSpec(v.getHeight(), View.MeasureSpec.EXACTLY));  
        v.layout((int) v.getX(), (int) v.getY(), (int) v.getX() + v.getMeasuredWidth(), (int) v.getY() + v.getMeasuredHeight());  
    } else {  
        v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),  
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));  
        v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());  
    }  
  
    Bitmap bitmap = Bitmap.createBitmap(v.getDrawingCache(), 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());  
    v.setDrawingCacheEnabled(false);  
    v.destroyDrawingCache();  
    return bitmap;  
}  

三、ScrollView截屏

ScrollView只有一個(gè)childView苇羡,雖然沒有全部顯示在界面上绸吸,但是已經(jīng)全部渲染繪制,因此可以直接 調(diào)用scrollView.draw(canvas)來完成截圖

/** 
 * Scrollview截屏 
 * 
 * @param scrollView 要截圖的ScrollView 
 * @return Bitmap 
 */  
public static Bitmap shotScrollView(ScrollView scrollView) {  
    int h = 0;  
    Bitmap bitmap = null;  
    for (int i = 0; i < scrollView.getChildCount(); i++) {  
        h += scrollView.getChildAt(i).getHeight();  
        scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));  
    }  
    bitmap = Bitmap.createBitmap(scrollView.getWidth(), h, Bitmap.Config.RGB_565);  
    final Canvas canvas = new Canvas(bitmap);  
    scrollView.draw(canvas);  
    return bitmap;  
}  

四、ListView截屏

ListView是會(huì)回收與重用Item锦茁,并且只會(huì)繪制在屏幕上顯示的ItemView攘轩,下面的方法采用一個(gè)List來存儲(chǔ)Item的視圖,這種方案依然不夠好码俩,當(dāng)Item足夠多的時(shí)候度帮,可能會(huì)發(fā)生oom。

/** 
 * ListView截圖 
 * 
 * @param listView 要截圖的ListView 
 * @return Bitmap 
 */  
public static Bitmap shotListView(ListView listView) {  
    ListAdapter adapter = listView.getAdapter();  
    int itemsCount = adapter.getCount();  
    int allItemsHeight = 0;  
  
    ArrayList<Bitmap> bmps = new ArrayList<>();  
    for (int i = 0; i < itemsCount; i++) {  
        View childView = adapter.getView(i, null, listView);  
        childView.measure(View.MeasureSpec.makeMeasureSpec(listView.getWidth(),View.MeasureSpec.EXACTLY),  
                View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED));  
  
        childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());  
        childView.setDrawingCacheEnabled(true);  
        childView.buildDrawingCache();  
        bmps.add(childView.getDrawingCache());  
        allItemsHeight += childView.getMeasuredHeight();  
    }  
  
    Bitmap bigBitmap = Bitmap.createBitmap(listView.getMeasuredWidth(), allItemsHeight, Bitmap.Config.ARGB_8888);  
    Canvas bigCanvas = new Canvas(bigBitmap);  
  
    Paint paint = new Paint();  
    int iHeight = 0;  
  
    for (int i = 0; i < bmps.size(); i++) {  
        Bitmap bmp = bmps.get(i);  
        bigCanvas.drawBitmap(bmp, 0, iHeight, paint);  
        iHeight += bmp.getHeight();  
  
        bmp.recycle();  
        bmp = null;  
    }  
    return bigBitmap;  
}  

五稿存、RecyclerView截屏

/** 
     * RecyclerView截屏 
     * 
     * @param view 要截圖的RecyclerView 
     * @return Bitmap 
     */  
    public static Bitmap shotRecyclerView(RecyclerView view) {  
        RecyclerView.Adapter adapter = view.getAdapter();  
        Bitmap bigBitmap = null;  
        if (adapter != null) {  
            int size = adapter.getItemCount();  
            int height = 0;  
            Paint paint = new Paint();  
            int iHeight = 0;  
            final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);  
  
            // Use 1/8th of the available memory for this memory cache.  
            final int cacheSize = maxMemory / 8;  
            LruCache<String, Bitmap> bitmaCache = new LruCache<>(cacheSize);  
            for (int i = 0; i < size; i++) {  
                RecyclerView.ViewHolder holder = adapter.createViewHolder(view, adapter.getItemViewType(i));  
                adapter.onBindViewHolder(holder, i);  
                holder.itemView.measure(  
                        View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),  
                        View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));  
                holder.itemView.layout(0, 0, holder.itemView.getMeasuredWidth(),  
                        holder.itemView.getMeasuredHeight());  
                holder.itemView.setDrawingCacheEnabled(true);  
                holder.itemView.buildDrawingCache();  
                Bitmap drawingCache = holder.itemView.getDrawingCache();  
                if (drawingCache != null) {  
  
                    bitmaCache.put(String.valueOf(i), drawingCache);  
                }  
                height += holder.itemView.getMeasuredHeight();  
            }  
  
            bigBitmap = Bitmap.createBitmap(view.getMeasuredWidth(), height, Bitmap.Config.ARGB_8888);  
            Canvas bigCanvas = new Canvas(bigBitmap);  
            Drawable lBackground = view.getBackground();  
            if (lBackground instanceof ColorDrawable) {  
                ColorDrawable lColorDrawable = (ColorDrawable) lBackground;  
                int lColor = lColorDrawable.getColor();  
                bigCanvas.drawColor(lColor);  
            }  
  
            for (int i = 0; i < size; i++) {  
                Bitmap bitmap = bitmaCache.get(String.valueOf(i));  
                bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);  
                iHeight += bitmap.getHeight();  
                bitmap.recycle();  
            }  
        }  
        return bigBitmap;  
    }  

六笨篷、WebView截屏

1、截取webView可視區(qū)域的截圖

 /**   
    * 截取webView可視區(qū)域的截圖   
    * @param webView 前提:WebView要設(shè)置webView.setDrawingCacheEnabled(true);   
    * @return   
    */    
private Bitmap captureWebViewVisibleSize(WebView webView){    
    Bitmap bmp = webView.getDrawingCache();    
    return bmp;    
}  

2挠铲、截取webView的整個(gè)頁面

/**   
 * 截取webView快照(webView加載的整個(gè)內(nèi)容的大小)   
 * @param webView   
 * @return   
 */    
private Bitmap captureWebView(WebView webView){    
    Picture snapShot = webView.capturePicture();    
        
    Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),snapShot.getHeight(), Bitmap.Config.ARGB_8888);    
    Canvas canvas = new Canvas(bmp);    
    snapShot.draw(canvas);    
    return bmp;    
}    

3冕屯、截取手機(jī)屏幕寂诱,獲取界面展示的webview

/** 
 * 截屏 
 * 
 * @param context 
 * @return 
 */  
private Bitmap captureScreen(Activity context) {  
    View cv = context.getWindow().getDecorView();  
    Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_8888);  
    Canvas canvas = new Canvas(bmp);  
    cv.draw(canvas);  
    return bmp;  
}  
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末拂苹,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子痰洒,更是在濱河造成了極大的恐慌瓢棒,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評(píng)論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件丘喻,死亡現(xiàn)場(chǎng)離奇詭異脯宿,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)泉粉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門连霉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人嗡靡,你說我怎么就攤上這事跺撼。” “怎么了讨彼?”我有些...
    開封第一講書人閱讀 168,017評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵歉井,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我哈误,道長(zhǎng)哩至,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評(píng)論 1 296
  • 正文 為了忘掉前任蜜自,我火速辦了婚禮菩貌,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘重荠。我一直安慰自己菜谣,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評(píng)論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著尾膊,像睡著了一般媳危。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上冈敛,一...
    開封第一講書人閱讀 52,255評(píng)論 1 308
  • 那天待笑,我揣著相機(jī)與錄音,去河邊找鬼抓谴。 笑死暮蹂,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的癌压。 我是一名探鬼主播仰泻,決...
    沈念sama閱讀 40,825評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼滩届!你這毒婦竟也來了集侯?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,729評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤帜消,失蹤者是張志新(化名)和其女友劉穎棠枉,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體泡挺,經(jīng)...
    沈念sama閱讀 46,271評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡辈讶,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了娄猫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片贱除。...
    茶點(diǎn)故事閱讀 40,498評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖媳溺,靈堂內(nèi)的尸體忽然破棺而出梧喷,到底是詐尸還是另有隱情障簿,我是刑警寧澤闸婴,帶...
    沈念sama閱讀 36,183評(píng)論 5 350
  • 正文 年R本政府宣布膀跌,位于F島的核電站,受9級(jí)特大地震影響屯阀,放射性物質(zhì)發(fā)生泄漏缅帘。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評(píng)論 3 333
  • 文/蒙蒙 一难衰、第九天 我趴在偏房一處隱蔽的房頂上張望钦无。 院中可真熱鬧,春花似錦盖袭、人聲如沸失暂。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評(píng)論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽弟塞。三九已至凭峡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間决记,已是汗流浹背摧冀。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評(píng)論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留系宫,地道東北人索昂。 一個(gè)月前我還...
    沈念sama閱讀 48,906評(píng)論 3 376
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像扩借,于是被迫代替她去往敵國和親椒惨。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評(píng)論 2 359

推薦閱讀更多精彩內(nèi)容