当前位置:网站首页>Adnroid activity截屏 保存显示到相册 View显示图片 动画消失
Adnroid activity截屏 保存显示到相册 View显示图片 动画消失
2022-06-23 04:14:00 【木子102】
今天项目有个需求,截屏Activity界面,保存到本地,然后即时发送图片的时候能找到这张图片发送
流程:截屏->保存到本地->发送广播通知更新图库
先贴效果图
截屏过程 
截屏后保存的文件 
截屏的图片 
1、写个ScreenCaptureUtil工具,实现截屏和保存,代码如下
/** * Created by nxm on 2018/1/20. */
public class ScreenCaptureUtil {
private static ScreenCaptureUtil instance = null;
private ScreenCaptureUtil() {
}
public static ScreenCaptureUtil getInstance() {
if (null == instance) {
instance = new ScreenCaptureUtil();
}
return instance;
}
public Bitmap getScreen(Activity activity) {
View dView = activity.getWindow().getDecorView();
dView.setDrawingCacheEnabled(false);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());
if (bitmap != null) {
try {
// 首先保存图片
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "HIS";
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
activity.sendBroadcast(intent);
dView.destroyDrawingCache();
return bitmap;
} catch (Exception e) {
return null;
}
} else {
return null;
}
}
}
重点:
1)获取activity截屏生成Bitmap
View dView = activity.getWindow().getDecorView();
dView.setDrawingCacheEnabled(false);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());2)创建文件路径 保存bitmap(保存到相册根目录->File.separator)
// 首先保存图片
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "HIS";
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();3)发送广播通知相册刷新加入这张图片
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
activity.sendBroadcast(intent);4)销毁缓存(不销毁,保存的总是第一张的内容)
dView.destroyDrawingCache();2、在activity中使用(项目需求是截屏后,显示一下,然后动画退出消失)
case R.id.btn_two:
//截屏
Bitmap screen = ScreenCaptureUtil.getInstance().getScreen(MainActivity.this);
String content = "";
if (null != screen) {
show_screen.setVisibility(View.VISIBLE);
showImage.setImageBitmap(screen);
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanimation);
animation.setFillAfter(true);
showImage.startAnimation(animation);
content = "截屏成功";
} else {
content = "截屏失败";
}
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
break;重点:
1)加载动画,并且显示(先判断是否保存成功即bitmap是否等于null)
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanimation);
animation.setFillAfter(true);
showImage.startAnimation(animation);2)动画scaleanimation.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<scale android:duration="1000" android:fromXScale="1.0" android:fromYScale="1.0" android:interpolator="@android:anim/accelerate_interpolator" android:pivotX="5%" android:pivotY="5%" android:toXScale="0.0" android:toYScale="0.0" />
</set>记得添加读写权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />边栏推荐
- 电脑开机显示器黑屏是什么原因,电脑显示器黑屏怎么办
- 云原生数据库是未来
- 技能自检 | 想当测试Leader,这6项技能你会吗?
- ArcTime 制作中英文字幕视频
- MySQL character set
- 雷达图canvas
- C primer plus学习笔记 —— 2、常量与格式化IO(输入/输出)
- AHA C language Chapter 8 game time is up (lesson 29)
- Wechat applet: future wife query generator
- What is the magic of digital collections? Which reliable teams are currently developing
猜你喜欢

Behind the hot digital collections, a strong technical team is needed to support the northern technical team

Lihongyi, machine learning 5 Tips for neural network design

华为软硬件生态圈成型,从根子上改变美国对软硬件体系的领导地位

Wechat applet: elderly blessing short video

技能自检 | 想当测试Leader,这6项技能你会吗?

Composite API

Wechat applet: future wife query generator

A bit of knowledge - folding forging and Damascus steel

软件设计开发笔记2:基于QT设计串口调试工具

Digital collections - new investment opportunities
随机推荐
MySQL Foundation
MySQL character set
Software project management 8.4 Software project quality plan
C prime plus notes d'apprentissage - 2, constantes et formatage io (I / o)
Common wireless charging and transmitting IC chips
Use of visdom
知识点滴 - 折叠锻打和大马士革钢
Current situation and development of containerization technology under the cloud native trend
手机无线充电双线圈15W方案SOC英集芯IP6809
Yingjixin ip5566 with type-C port 3A charging and discharging fast charging mobile power supply 5W wireless charging in one SOC
LeetCode-1757. Recyclable and low-fat products_ SQL
STC 32比特8051單片機開發實例教程 一 開發環境搭建
Oracle异常
[OWT] OWT client native P2P E2E test vs2017 build 6: modify script automatic generation vs Project
mysql字符集
How to move the software downloaded from win11 app store to the desktop
Wechat applet: wechat can also send flash photos to create wechat applet source code download and customize flash time
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
树莓派assert初步使用练习
Skill self check | do you know these 6 skills if you want to be a test leader?