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朊健!