Android调用长截屏,Android实现长截屏功能
本文实例为大家分享了Android实现长截屏功能的具体代码,供大家参考,具体内容如下
1、MainActivity
public class MainActivity extends AppCompatActivity {
ScrollView scrollView;
String sdRoot = Environment.getExternalStorageDirectory().getPath();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scroll);
//权限
int permission_write= ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
int permission_read=ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE);
if(permission_write!= PackageManager.PERMISSION_GRANTED
|| permission_read!=PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "正在请求权限", Toast.LENGTH_SHORT).show();
//申请权限,特征码自定义为1,可在回调时进行相关判断
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE},1);
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
shootScrollView(scrollView, "sdRoot");
// takeScreenshot();
super.onWindowFocusChanged(hasFocus);
}
@Override
protected void onResume() {
super.onResume();
}
/**
* 截取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();
scrollView.getChildAt(i).setBackgroundColor(Color.parseColor("#ffffff"));
}
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) {
}
Log.d("TAG", "getScrollViewBitmap() returned: " + bitmap.getHeight());
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);
ScreenShot.saveMyBitmap(getScrollViewBitmap(scrollView, picpath), picpath);
}
// 程序入口 截取ListView
public static void shootListView(ListView listView, String picpath) {
ScreenShot.savePic(getListViewBitmap(listView,picpath), picpath);
}
private void takeScreenshot() {
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
openScreenshot(imageFile);
} catch (Throwable e) {
// Several error may come out with file handling or OOM
e.printStackTrace();
}
}
private void openScreenshot(File imageFile) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(imageFile);
intent.setDataAndType(uri, "image/*");
startActivity(intent);
}
}
2.ScreenShot
public class ScreenShot{
// 获取指定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;
Log.d("TAG", "savePic() returned: " );
try {
fos = new FileOutputStream(strFileName);
if (null != fos) {
b.compress(Bitmap.CompressFormat.PNG, 90, fos);
Log.d("TAG", "savePic() returned: " + b.getHeight());
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);
}
/**
* 保存bitmap到SD卡
* @param bitName 保存的名字
* @param mBitmap 图片对像
* return 生成压缩图片后的图片路径
*/
@SuppressLint("SdCardPath")
public static String saveMyBitmap(Bitmap mBitmap, String bitName) {
String path = Environment.getExternalStorageDirectory()+"/"+bitName+".png";
File f = new File(path);
try {
f.createNewFile();
} catch (IOException e) {
System.out.println("在保存图片时出错:" + e.toString());
}
FileOutputStream fOut = null;
try {
fOut = new FileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
mBitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
} catch (Exception e) {
return "create_bitmap_error";
}
try {
fOut.flush();
} catch (IOException e) {
e.printStackTrace();
}
try {
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
// File file = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".jpg");
// FileOutputStream out = null;
// try {
// out = new FileOutputStream(file);
// mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
// System.out.println("___________保存的__sd___下_______________________");
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
// try {
// out.flush();
// out.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
return path;
// Toast.makeText(HahItemActivity.this,"保存已经至"+Environment.getExternalStorageDirectory()+"下", Toast.LENGTH_SHORT).show();
}
}
3.xml
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。
Android调用长截屏,Android实现长截屏功能相关推荐
- android 调用百度地图规划,Android 调用百度地图API
一.到 百度地图开发平台下载SDK 1.点击自定义下载 2.下载自己想要的功能包我这里选了三个 3.获取密钥 获取密钥之前我们要新建一个应用 填写好相关的信息 二. 新建一个项目工程 1. 把下载好的 ...
- android调用webservice传参数,android调用webservice接口获取信息
我的有一篇博客上讲了如何基于CXF搭建webservice,service层的接口会被部署到tomcat上,这一篇我就讲一下如何在安卓中调用这些接口传递参数. 1.在lib中放入ksoap2的jar包 ...
- android调用文件管理器回调,Android中调用系统管理器选择文件并返回绝对路径
选择文件的时候,如果使用的是第三方软件,一般是直接返回绝对路径的Uri,如果是系统的,则会返回媒体库的xxxx,所以单纯一种方式选择文件,很可能拿到的绝对路径是错的或者是空的,所以需要做几种处理,这里 ...
- android调用java接口_java/Android 接口调用的几种写法
虽然Handler用的地方比较普遍,但是接口也有他的独特之处,比较直观,然后降低了耦合性 如有一接口,需要将数据传给使用的activity中,接口如下 public interfacePushValu ...
- android调用另一个app,Android调用另一个App界面
最近年底了,项目比较忙,虽然用笔把要总结的技巧来技术写下了,但是一直没有时间来更新.今天要说的是在App中调用其他APP界面的总结,我们经常需要给别的程序调用自己程序的UI界面,这时可以有三种方式来提 ...
- android调用资源文件配置,Android资源文件
应用资源是指应用中与源代码分离的资源,如图像.音频文件以及任何与应用的视觉呈现有关的内容.在Android项目中的每一项资源,SDK构建工具都会定义一个唯一的整形ID,可以通过ID对资源进行应用.提供 ...
- Android调用uc播放器,android 视频播放 -- 调用系统播放器
android 视频播放,视频来源有两种:一种是本地视频,一种是网络视频,对android播放来说都统一用uri来表示. 播放视频有三种处理方式: (1) 使用webview 播放 (2) 使用vid ...
- android调用js函数方法,Android和JavaScript相互调用的方法
本文实例讲述了Android和JavaScript相互调用的方法.分享给大家供大家参考,具体如下: Html页面和Java代码结合的方式一般用在界面经常被更改 的情况下,可以讲html放在网络中,软件 ...
- android调用虚拟摄像头方法,Android:如何在模拟器中使用网络摄像头?
我通过在AVD Manager中将前置摄像头设置为" webcam0"将摄像头连接到仿真器. 启动模拟器的相机应用程序时,出现错误 CameraService::connect X ...
- Android调用miui给权限,Android 11+MIUI12,APP任意获取用户隐私的路子被封死
在经历了多个测试版本的迭代和完善之后,安卓11终于来到了用户面前,作为目前全球用户数量最大的操作系统,安卓每一次大版本更新都会为用户带来很多新功能,新体验以及全新的设计系统,底层升级和变化也会一定程度 ...
最新文章
- [转载]静态构造函数
- JavaScript内存优化
- apache kafka技术分享系列(目录索引)--转载
- 计算机学业水平测试题及答案初中,初中信息技术学业水平考试试题30号试题.doc...
- 使用Nodejs发送邮件
- JAVA的rotate怎么用,java如何利用rotate旋转图片_如何在Java中旋转图形
- python爬取所有数据_入门用Python进行Web爬取数据:为数据科学项目提取数据的有效方法...
- 阅读替换净化规则_强力推荐一个开源阅读软件
- 2019 年开源数据库报告发布:MySQL 仍卫冕!
- 低密度奇偶校验码(LDPC)GMSK调制解调器 毕业设计
- 前端-CSS,语法、元素、属性、动画、计算方法布局及简单实战项目
- vue 实现计算器功能
- 机器学习基础(四)——决策树与随机森林
- 西安电子科大计算机学院院长,董伟生 | 个人信息 | 西安电子科技大学个人主页...
- Reverse-it
- 计科学硕考研初试经验贴(11408)
- Windows复制文件提示指定的文件名无效或太长
- 微信公众号开发官方定制ui
- lua菜鸟教程_初学者必看:Lua入门学习教程
- Android中Parcelable接口的应用