android截屏功能实现代码
作者:fmp8 时间:2022-02-02 19:45:00
android开发中通过View的getDrawingCache方法可以达到截屏的目的,只是缺少状态栏!
原始界面
截屏得到的图片
代码实现
1. 添加权限(AndroidManifest.xml文件里)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. 添加1个Button(activity_main.xml文件)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
<Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Screenshot"
/>
</RelativeLayout>
3. 实现截屏(MainActivity.java文件)
package com.example.androidtest;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) this.findViewById(R.id.btn_save);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
screenshot();
}
});
}
private void screenshot()
{
// 获取屏幕
View dView = getWindow().getDecorView();
dView.setDrawingCacheEnabled(true);
dView.buildDrawingCache();
Bitmap bmp = dView.getDrawingCache();
if (bmp != null)
{
try {
// 获取内置SD卡路径
String sdCardPath = Environment.getExternalStorageDirectory().getPath();
// 图片文件路径
String filePath = sdCardPath + File.separator + "screenshot.png";
File file = new File(filePath);
FileOutputStream os = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
标签:android,截屏
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JAVA NIO实现简单聊天室功能
2023-05-01 10:32:49
Springboot+SpringSecurity+JWT实现用户登录和权限认证示例
2021-11-14 11:06:11
![](https://img.aspxhome.com/file/2023/3/71583_0s.png)
java ArrayList集合中的某个对象属性进行排序的实现代码
2022-05-27 19:54:24
![](https://img.aspxhome.com/file/2023/0/62530_0s.jpg)
Java 创建PDF打印小册子案例
2023-01-17 04:19:40
![](https://img.aspxhome.com/file/2023/3/70503_0s.png)
Java字符串拼接新方法 StringJoiner用法详解
2022-06-16 13:02:13
springboot实现在工具类(util)中调用注入service层方法
2021-06-17 20:02:51
![](https://img.aspxhome.com/file/2023/6/60616_0s.png)
Java实现克隆的三种方式实例总结
2021-11-21 15:26:14
![](https://img.aspxhome.com/file/2023/2/76632_0s.png)
android TextView实现跑马灯效果
2023-07-27 16:35:31
Android Flutter实现仿闲鱼动画效果
2023-07-15 15:32:47
![](https://img.aspxhome.com/file/2023/3/87323_0s.jpg)
Flutter Widgets MediaQuery控件屏幕信息适配
2023-06-29 04:48:21
![](https://img.aspxhome.com/file/2023/2/120092_0s.jpg)
springMVC+ajax实现文件上传且带进度条实例
2022-01-15 16:42:36
SpringBoot构建RESTful API的实现示例
2022-04-13 14:45:08
java并发编程_线程池的使用方法(详解)
2023-03-29 22:23:00
![](https://img.aspxhome.com/file/2023/1/69711_0s.png)
解决Eclipse创建android项目无法正常预览布局文件问题的方法
2023-07-31 09:51:12
![](https://img.aspxhome.com/file/2023/6/82316_0s.png)
Swagger2匹配多个controller代码实例
2022-07-31 03:42:24
SpringBoot详细讲解异步任务如何获取HttpServletRequest
2023-01-04 18:01:09
java高并发的并发级别详解
2023-08-05 15:44:38
![](https://img.aspxhome.com/file/2023/9/78259_0s.jpg)
java生成XML的方法
2023-08-12 11:45:46
Java Exchanger并发类使用方法
2023-08-19 20:20:41
java IO流将一个文件拆分为多个子文件代码示例
2023-08-30 12:46:15
![](https://img.aspxhome.com/file/2023/3/58193_0s.jpg)