安卓截图笔记

Android截屏

Android截屏的原理:获取具体需要截屏的区域的Bitmap,然后绘制在画布上,保存为图片后进行分享或者其它用途

一、Activity截屏

1、截Activity界面(包含空白的状态栏)

/**

* 根据指定的Activity截图(带空白的状态栏)

*

* @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界面(去除状态栏)

?

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

/**

* 根据指定的Activity截图(去除状态栏)

*

* @param activity 要截图的Activity

* @return Bitmap

*/

publicBitmap shotActivityNoStatusBar(Activity activity) {

// 获取windows中最顶层的view

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

view.buildDrawingCache();

// 获取状态栏高度

Rect rect =newRect();

view.getWindowVisibleDisplayFrame(rect);

intstatusBarHeights = rect.top;

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

// 获取屏幕宽和高

intwidths = display.getWidth();

intheights = display.getHeight();

// 允许当前窗口保存缓存信息

view.setDrawingCacheEnabled(true);

// 去掉状态栏

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

statusBarHeights, widths, heights - statusBarHeights);

// 销毁缓存信息

view.destroyDrawingCache();

returnbmp;

}

二、View截屏

/**

* 根据指定的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只有一个childView,虽然没有全部显示在界面上,但是已经全部渲染绘制,因此可以直接 调用scrollView.draw(canvas)来完成截图

/**

* 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是会回收与重用Item,并且只会绘制在屏幕上显示的ItemView,下面的方法采用一个List来存储Item的视图,这种方案依然不够好,当Item足够多的时候,可能会发生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可视区域的截图

/**

* 截取webView可视区域的截图

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

* @return

*/

privateBitmap captureWebViewVisibleSize(WebView webView){

Bitmap bmp = webView.getDrawingCache();

returnbmp;

}

2、截取webView的整个页面


/**

* 截取webView快照(webView加载的整个内容的大小)

* @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、截取手机屏幕,获取界面展示的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;

}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

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

如对本文有疑问,请提交到交流社区,广大热心网友会为你解答!!

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 218,386评论 6 506
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 93,142评论 3 394
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 164,704评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,702评论 1 294
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,716评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,573评论 1 305
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,314评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 39,230评论 0 276
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,680评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,873评论 3 336
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,991评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,706评论 5 346
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,329评论 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,910评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 33,038评论 1 270
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 48,158评论 3 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,941评论 2 355

推荐阅读更多精彩内容