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实现截屏与截长图功能的具体代码实现。希望对大家的学习有所帮助。
热门推荐
如何设置电脑开机密码与屏幕锁?
SpringBoot+Docker+Nginx部署前后端项目
Spring Boot整合MyBatis实战指南
敖丙:从反派预设到灵魂蜕变的惊艳之旅
正地铁房、准地铁房、近地铁房,哪个更好?距地铁站多少米合适?
2024年国家基本公共卫生服务项目全面解读,附:最新宣传海报及视频
什么是带状疱疹?如何预防这个「不死的癌症」?
抗精神病药物氯氮平使用指南:减停更需谨慎!
新加坡:填海造陆的奇迹与变迁
假面骑士Eld设定
出国留学成绩单要求平均多少分?计算方法是什么?
斑马打印机校准标签纸的解决方法
冬天脾胃虚弱?3种中药让你告别消化不良,温暖过冬!
特别授权是什么?一文详解特别授权的概念、区别与实施要点
今年春运动车组票价最大优惠低至3折 安徽多条新线“首秀”表现亮眼
远摄变焦镜头的基础知识
易疲劳体质怎么改善
公司时薪人员裁员制度怎么制定才合理?
东坡肉怎么做才能软烂而不油腻
登山粮食/食物准备大补帖
医生!为什么我牙缝里有个“洞”!
DeepSeek中国压力最大职业排名:教师仅排第2名,第1名令人意外
火麻仁:润肠通便的中药瑰宝
2024年临床医学类专业就业情况分析:医院招聘学历要求高,本科生机遇缩减!
完善中小企业贷款风险补偿机制的制度构建与实施路径研究
刘亦菲因外籍身份被金鹰奖除名,盘点11位国内鲜为人知的外籍明星
如何判断一个孩子是否有阅读障碍
密集库智能仓储中的主要设备有哪些
个股波动的市场规律是什么?如何利用这些规律进行投资决策?
程序员如何应对职场上的不公平对待