首页 > 其他分享 >Android截屏截图的几种方法总结

Android截屏截图的几种方法总结

时间:2022-12-09 18:31:37浏览次数:53  
标签:截图 return int MeasureSpec Bitmap 截屏 Android View view


Android截屏

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

一、Activity截屏

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

/**

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

*

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

/**

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

*

* @param activity 要截图的Activity

* @return Bitmap

*/

public Bitmap shotActivityNoStatusBar(Activity activity) {

// 获取windows中最顶层的view

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

view.buildDrawingCache();



// 获取状态栏高度

Rect rect = new Rect();

view.getWindowVisibleDisplayFrame(rect);

int statusBarHeights = rect.top;

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



// 获取屏幕宽和高

int widths = display.getWidth();

int heights = display.getHeight();



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

view.setDrawingCacheEnabled(true);



// 去掉状态栏

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

statusBarHeights, widths, heights - statusBarHeights);



// 销毁缓存信息

view.destroyDrawingCache();



return bmp;

}

二、View截屏

/**

* 根据指定的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只有一个childView,虽然没有全部显示在界面上,但是已经全部渲染绘制,因此可以直接 调用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是会回收与重用Item,并且只会绘制在屏幕上显示的ItemView,下面的方法采用一个List来存储Item的视图,这种方案依然不够好,当Item足够多的时候,可能会发生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;

}

五、RecycleView截屏

/**

* 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可视区域的截图

/** 

* 截取webView可视区域的截图

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

* @return

*/

private Bitmap captureWebViewVisibleSize(WebView webView){

Bitmap bmp = webView.getDrawingCache();

return bmp;

2、截取webView的整个页面


/** 

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

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

}

标签:截图,return,int,MeasureSpec,Bitmap,截屏,Android,View,view
From: https://blog.51cto.com/u_15907753/5926369

相关文章

  • UVCCamera && AndroidUSBCamera示例运行错误的解决办法
    Android设备USB摄像头框架,主要都是基于UVCCamera(github:​​https://github.com/saki4510t/UVCCamera​​​)在此之上,AndroidUSBCamera最新3.x版本支持了多摄像头的使用。......
  • Android13.0 PackageManagerService 的重要改变(package服务的获取)
    PackageManagerService不再extendsIPackageManager.Stub ——> 改为由抽象类IPackageManagerBaseextendsIPackageManager.Stub不再以pkms为基础类addService"p......
  • selenium获取验证码截图
    获取验证码截图代码:获取验证码代码:#!/user/bin/envpython3#-*-coding:utf-8-*-importrequestsfromseleniumimportwebdriverfromselenium.webdriver.co......
  • Android GestureDetector
    之前一直不知道这个类,在Android就以为只有鼠标的down和up事件,原来android为了增加用户体验,新增了GestureDetector类,也就是手势识别类,感觉就是将手指触摸屏幕的touch事件更加......
  • android调试工具DDMS
       DDMS全称DalvikDebugMonitorService.DDMS为IDE和emultor及真正的android设备架起来了一座桥梁,Android DDMS将捕捉到终端的ID,并通过adb建立调试器,从而实现发送指......
  • Android 装载器---重启装载器
    在使用initLoader()方法时,如果指定ID的装载器存在,就使用这个既存的装载器,否则会创建一个新的。但是有些时候你会想要废弃旧的数据并重启装载器。你可以使用restartLoader(......
  • android 动画xml属性总结
    XML中 alpha渐变透明度动画效果scale渐变尺寸伸缩动画效果translate画面转换位置移动动画效果rotate画面转移旋转动画效果JavaCode中 AlphaAnimation渐变透明度动画效果S......
  • [译]Android防止内存的八种方法(下)
    在上一篇​​Android内存泄漏的八种可能(上)​​中,我们讨论了八种容易发生内存泄漏的代码。其中,尤其严重的是泄漏​​Activity​​对象,因为它占用了大量系统内存。不管内存泄......
  • Android中的EditText默认时不弹出软键盘的方法
    在做项目过程中,父Activity中用ViewPager中的子Activity EditText默认弹出软键盘。这是想屏蔽软键盘应该从 父Activity中处理。处理子Activity达不到效果。......
  • [译]Android内存泄漏的八种可能(上)
    ​​原文EightWaysYourAndroidAppCanLeakMemor​​Java是垃圾回收语言的一种,其优点是开发者无需特意管理内存分配,降低了应用由于局部故障(segmentationfault)导致......