安卓截圖筆記

Android截屏

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

一矾麻、Activity截屏

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

/**

* 根據(jù)指定的Activity截圖(帶空白的狀態(tài)欄)

*

* @param context 要截圖的Activity

* @return Bitmap

*/

publicstaticBitmap 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();

returnbitmap;

}

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

/**

* 根據(jù)指定的Activity截圖(去除狀態(tài)欄)

*

* @param activity 要截圖的Activity

* @return Bitmap

*/

publicBitmap shotActivityNoStatusBar(Activity activity) {

// 獲取windows中最頂層的view

View view = activity.getWindow().getDecorView();

view.buildDrawingCache();

// 獲取狀態(tài)欄高度

Rect rect =newRect();

view.getWindowVisibleDisplayFrame(rect);

intstatusBarHeights = rect.top;

Display display = activity.getWindowManager().getDefaultDisplay();

// 獲取屏幕寬和高

intwidths = display.getWidth();

intheights = display.getHeight();

// 允許當(dāng)前窗口保存緩存信息

view.setDrawingCacheEnabled(true);

// 去掉狀態(tài)欄

Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache(),0,

statusBarHeights, widths, heights - statusBarHeights);

// 銷(xiāo)毀緩存信息

view.destroyDrawingCache();

returnbmp;

}

二、View截屏

/**

* 根據(jù)指定的view截圖

*

* @param v 要截圖的view

* @return Bitmap

*/

publicstaticBitmap getViewBitmap(View v) {

if(null== v) {

returnnull;

}

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();

returnbitmap;

}

三弄喘、ScrollView截屏:ScrollView只有一個(gè)childView玖喘,雖然沒(méi)有全部顯示在界面上,但是已經(jīng)全部渲染繪制蘑志,因此可以直接 調(diào)用scrollView.draw(canvas)來(lái)完成截圖

/**

* Scrollview截屏

*

* @param scrollView 要截圖的ScrollView

* @return Bitmap

*/

publicstaticBitmap shotScrollView(ScrollView scrollView) {

inth =0;

Bitmap bitmap =null;

for(inti =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);

finalCanvas canvas =newCanvas(bitmap);

scrollView.draw(canvas);

returnbitmap;

}


四累奈、ListView截屏:ListView是會(huì)回收與重用Item,并且只會(huì)繪制在屏幕上顯示的ItemView卖漫,下面的方法采用一個(gè)List來(lái)存儲(chǔ)Item的視圖费尽,這種方案依然不夠好,當(dāng)Item足夠多的時(shí)候羊始,可能會(huì)發(fā)生oom旱幼。

/**

* ListView截圖

*

* @param listView 要截圖的ListView

* @return Bitmap

*/

publicstaticBitmap shotListView(ListView listView) {

ListAdapter adapter = listView.getAdapter();

intitemsCount = adapter.getCount();

intallItemsHeight =0;

ArrayList bmps =newArrayList<>();

for(inti =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 =newCanvas(bigBitmap);

Paint paint =newPaint();

intiHeight =0;

for(inti =0; i < bmps.size(); i++) {

Bitmap bmp = bmps.get(i);

bigCanvas.drawBitmap(bmp,0, iHeight, paint);

iHeight += bmp.getHeight();

bmp.recycle();

bmp =null;

}

returnbigBitmap;

}

五、RecycleView截屏


/**

* RecyclerView截屏

*

* @param view 要截圖的RecyclerView

* @return Bitmap

*/

publicstaticBitmap shotRecyclerView(RecyclerView view) {

RecyclerView.Adapter adapter = view.getAdapter();

Bitmap bigBitmap =null;

if(adapter !=null) {

intsize = adapter.getItemCount();

intheight =0;

Paint paint =newPaint();

intiHeight =0;

finalintmaxMemory = (int) (Runtime.getRuntime().maxMemory() /1024);

// Use 1/8th of the available memory for this memory cache.

finalintcacheSize = maxMemory /8;

LruCache bitmaCache =newLruCache<>(cacheSize);

for(inti =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 =newCanvas(bigBitmap);

Drawable lBackground = view.getBackground();

if(lBackgroundinstanceofColorDrawable) {

ColorDrawable lColorDrawable = (ColorDrawable) lBackground;

intlColor = lColorDrawable.getColor();

bigCanvas.drawColor(lColor);

}

for(inti =0; i < size; i++) {

Bitmap bitmap = bitmaCache.get(String.valueOf(i));

bigCanvas.drawBitmap(bitmap, 0f, iHeight, paint);

iHeight += bitmap.getHeight();

bitmap.recycle();

}

}

returnbigBitmap;

}

六突委、WebView截屏

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

/**

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

* @param webView 前提:WebView要設(shè)置webView.setDrawingCacheEnabled(true);

* @return

*/

privateBitmap captureWebViewVisibleSize(WebView webView){

Bitmap bmp = webView.getDrawingCache();

returnbmp;

}

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


/**

* 截取webView快照(webView加載的整個(gè)內(nèi)容的大小)

* @param webView

* @return

*/

privateBitmap captureWebView(WebView webView){

Picture snapShot = webView.capturePicture();

Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(),snapShot.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas =newCanvas(bmp);

snapShot.draw(canvas);

returnbmp;

}

3匀油、截取手機(jī)屏幕缘缚,獲取界面展示的webview

/**

* 截屏

*

* @param context

* @return

*/

privateBitmap captureScreen(Activity context) {

View cv = context.getWindow().getDecorView();

Bitmap bmp = Bitmap.createBitmap(cv.getWidth(), cv.getHeight(), Bitmap.Config.ARGB_8888);

Canvas canvas =newCanvas(bmp);

cv.draw(canvas);

returnbmp;

}

感謝閱讀,希望能幫助到大家敌蚜,謝謝大家對(duì)本站的支持桥滨!

原文鏈接:http://blog.csdn.net/billy_zuo/article/details/71077681

如對(duì)本文有疑問(wèn),請(qǐng)?zhí)峤坏浇涣魃鐓^(qū)弛车,廣大熱心網(wǎng)友會(huì)為你解答F朊健!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末纷跛,一起剝皮案震驚了整個(gè)濱河市喻括,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌贫奠,老刑警劉巖唬血,帶你破解...
    沈念sama閱讀 218,284評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異唤崭,居然都是意外死亡拷恨,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,115評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門(mén)谢肾,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)挑随,“玉大人,你說(shuō)我怎么就攤上這事《蛋ぃ” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,614評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵眯分,是天一觀的道長(zhǎng)拌汇。 經(jīng)常有香客問(wèn)我,道長(zhǎng)弊决,這世上最難降的妖魔是什么噪舀? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,671評(píng)論 1 293
  • 正文 為了忘掉前任,我火速辦了婚禮飘诗,結(jié)果婚禮上与倡,老公的妹妹穿的比我還像新娘。我一直安慰自己昆稿,他們只是感情好纺座,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,699評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著溉潭,像睡著了一般净响。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上喳瓣,一...
    開(kāi)封第一講書(shū)人閱讀 51,562評(píng)論 1 305
  • 那天馋贤,我揣著相機(jī)與錄音,去河邊找鬼畏陕。 笑死配乓,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的惠毁。 我是一名探鬼主播犹芹,決...
    沈念sama閱讀 40,309評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼仁讨!你這毒婦竟也來(lái)了羽莺?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,223評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤洞豁,失蹤者是張志新(化名)和其女友劉穎盐固,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體丈挟,經(jīng)...
    沈念sama閱讀 45,668評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡刁卜,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,859評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了曙咽。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片蛔趴。...
    茶點(diǎn)故事閱讀 39,981評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖例朱,靈堂內(nèi)的尸體忽然破棺而出孝情,到底是詐尸還是另有隱情鱼蝉,我是刑警寧澤,帶...
    沈念sama閱讀 35,705評(píng)論 5 347
  • 正文 年R本政府宣布箫荡,位于F島的核電站魁亦,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏羔挡。R本人自食惡果不足惜洁奈,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,310評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望绞灼。 院中可真熱鬧利术,春花似錦、人聲如沸低矮。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,904評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)商佛。三九已至喉钢,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間良姆,已是汗流浹背肠虽。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,023評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留玛追,地道東北人税课。 一個(gè)月前我還...
    沈念sama閱讀 48,146評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像痊剖,于是被迫代替她去往敵國(guó)和親韩玩。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,933評(píng)論 2 355

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

  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線(xiàn)程陆馁,因...
    小菜c閱讀 6,419評(píng)論 0 17
  • View 自定義View中在onDraw()方法中可以設(shè)置padding嗎找颓?答案是不能,設(shè)置padding后叮贩,Vi...
    ElvenShi閱讀 1,845評(píng)論 0 0
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 172,138評(píng)論 25 707
  • 第一行先感恩击狮。 昨天突然靈光一閃,對(duì)未來(lái)有了很好的規(guī)劃益老,所以興匆匆要去辭職彪蓬。 上午沒(méi)見(jiàn)到上司,于是安靜地度過(guò)了兩個(gè)...
    悠然Yolanda閱讀 212評(píng)論 0 0
  • 七月底,因?yàn)橄矚g讀書(shū),而加入了讀書(shū)會(huì)酷誓,卻徹底發(fā)覺(jué)——我并不會(huì)讀書(shū)披坏。 本書(shū)構(gòu)建個(gè)一個(gè)屌絲通過(guò)讀書(shū)逆襲的故事,...
    瀟湘館主夢(mèng)今朝閱讀 258評(píng)論 0 5