Android实现截屏与截长图功能详解
创作时间:
作者:
@小白创作中心
Android实现截屏与截长图功能详解
引用
CSDN
1.
https://blog.csdn.net/Android23333/article/details/139112758
在Android开发中,有时需要实现截屏功能,比如将当前屏幕或长列表分享到社交媒体。本文将详细介绍如何在Android中实现截屏和截长图功能,并提供具体的代码示例。
截取当前屏幕
要截取当前屏幕并保存到内存卡,可以使用以下代码:
// 获取指定Activity的截屏,保存到png文件
public static Bitmap takeScreenShot(Activity activity) {
// View是你需要截图的View
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap b1 = view.getDrawingCache();
// 获取状态栏高度
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
System.out.println(statusBarHeight);
// 获取屏幕长和高
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay()
.getHeight();
// 去掉标题栏
// Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
- statusBarHeight);
view.destroyDrawingCache();
return b;
}
// 保存到sdcard
public static void savePic(Bitmap b, String strFileName) {
FileOutputStream fos = null;
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
// 程序入口 截取当前屏幕
public static void shootLoacleView(Activity a,String picpath) {
ScreenShot.savePic(ScreenShot.takeScreenShot(a), picpath);
}
截取ScrollView和ListView
当视图超过一个屏幕时,可以使用以下代码截取ScrollView或ListView:
/**
* 截取scrollview的屏幕
* **/
public static Bitmap getScrollViewBitmap(ScrollView scrollView,String picpath) {
int h = 0;
Bitmap bitmap;
// 获取listView实际高度
for (int i = 0; i < scrollView.getChildCount(); i++) {
h += scrollView.getChildAt(i).getHeight();
}
Log.d(TAG, "实际高度:" + h);
Log.d(TAG, " 高度:" + scrollView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(scrollView.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
scrollView.draw(canvas);
// 测试输出
FileOutputStream out = null;
try {
out = new FileOutputStream(picpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
}
return bitmap;
}
private static String TAG = "Listview and ScrollView item 截图:";
/**
* 截图listview
* **/
public static Bitmap getListViewBitmap(ListView listView,String picpath) {
int h = 0;
Bitmap bitmap;
// 获取listView实际高度
for (int i = 0; i < listView.getChildCount(); i++) {
h += listView.getChildAt(i).getHeight();
}
Log.d(TAG, "实际高度:" + h);
Log.d(TAG, "list 高度:" + listView.getHeight());
// 创建对应大小的bitmap
bitmap = Bitmap.createBitmap(listView.getWidth(), h,
Bitmap.Config.ARGB_8888);
final Canvas canvas = new Canvas(bitmap);
listView.draw(canvas);
// 测试输出
FileOutputStream out = null;
try {
out = new FileOutputStream(picpath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
if (null != out) {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}
} catch (IOException e) {
}
return bitmap;
}
// 程序入口 截取ScrollView
public static void shootScrollView(ScrollView scrollView,String picpath) {
ScreenShot.savePic(getScrollViewBitmap(scrollView, picpath), picpath);
}
// 程序入口 截取ListView
public static void shootListView(ListView listView,String picpath) {
ScreenShot.savePic(getListViewBitmap(listView,picpath), picpath);
}
效果图
截长图的效果图如下:
以上就是Android实现截屏与截长图功能的具体代码实现。希望对大家的学习有所帮助。
热门推荐
Excel中输入数字会改变怎么办?
远程医疗在基层医疗服务中的应用现状、困境与突破路径
十年磨一剑!浅谈飞升后的变化及PK走向
青松聊育儿:穷人的孩子早当家?真相让你大吃一惊!
健身完吃荔枝好吗?营养师教你科学补充
炖牛肉用什么部位的肉?
探索恩施土家女儿城至恩施大峡谷的自然与文化之旅
专业指导:备孕前怎么调理身体,如何打造最佳受孕状态?
连签三份合同,未在合同到期前要求续签无固定期,憾被合法终止
新版老年人平衡膳食宝塔,亮点有哪些
上海天文馆常设展:六大展区带你探索宇宙奥秘
秦始皇统一中国,靠的不只是武力,还有它们|观展
团队如何预防有卧底
武汉一小学这样落实“器乐进课堂”:先学口风琴再学竖笛
医生解答:吃辣后肚子痛是否是肠炎的表现?
乳腺癌术后患者能否食用蜂蜜?医生的专业解答来了
马斯克宣布火星计划:2年内发射无人飞船,20年内建城市!
生活观察丨四季豆有食品安全风险?专家建议这样吃更安全
庚金在八字命格中的意义与象征
卧室面积太小怎样装修?告别空间拥挤,巧妙利用小空间的装修妙招
启蒙运动:理性寻找光明
双代号网络计划是什么?其作用与类型定义详解
田蚡:汉武帝舅舅的权谋人生与悲剧结局
智慧果园云平台:物联网技术在农业中的创新应用
怀孕后高血压需要进行哪些检查?
一文读懂:快充到底会不会伤手机电池?看完就明白了!
薛岳将军之墓在哪?自创天炉战法,歼灭日军10万,名扬世界
充电器新纪元:科技引领充电速度
劳动法病假单开具医院的相关规定及流程
有关蛇的神话传说数不胜数!灵蛇传奇,穿越古今