当前位置:网站首页>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" />边栏推荐
- Webrtc[47] - a common way for webrtc to save YUV data
- Wechat applet: a new interesting test
- fastjson中的@JSONField注解
- Wechat applet: Puzzle toolbox
- PAT 乙等 1019 C语言
- 1010 Radix
- Skill self check | do you know these 6 skills if you want to be a test leader?
- What benefits have digital collections enabled the real industry to release?
- Build a gocd environment
- 阿里云对象存储oss+picgo+typora实现构建图床
猜你喜欢

Current situation and development of containerization technology under the cloud native trend

How does win11 enable mobile hotspot? How to enable mobile hotspot in win11

C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)

Export PDF with watermark

How much disk IO will actually occur for a byte of the read file?

Visdom draws multiple dynamic loss curves

sprintf 格式代码使用不规范在不同平台下的表现

Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices

What benefits have digital collections enabled the real industry to release?

Real MySQL interview question (30) -- shell real estate order analysis
随机推荐
MySQL character set
Mobile phone wireless charging dual coil 15W scheme SOC IC ip6809
Management system of borrowed books based on SSM framework
CF [1700d] D. River locks (DP, bisection, Mathematics)
Wechat applet: an artifact for calculating the full amount of orders
1010 Radix
Use of visdom
使用链表实现两个多项式相加和相乘
MySQL Foundation
高等数学(第七版)同济大学 习题1-7 个人解答
Genetic engineering of AI art? Use # artbreeder to change any shape of the image
SIFT feature point extraction
Alibaba cloud object storage oss+picgo+typera implements the construction map
visdom的使用
iNFTnews | 加密之家从宇宙寄来的明信片,你会收到哪一张?
移动电源快充QC3.0方案芯片IP5318快充方案
制造业数字化转型存在问题及原因分析
C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)
Wechat applet: Puzzle toolbox
Pkav simple blasting