当前位置:网站首页>Adnroid activity screenshot save display to album view display picture animation disappear
Adnroid activity screenshot save display to album view display picture animation disappear
2022-06-23 05:52:00 【Muzi 102】
Today's project has a requirement , screenshots Activity Interface , Save to local , Then you can find this picture and send it when you send it instantly
technological process : screenshots -> Save to local -> Send broadcast notification to update gallery
First paste the effect picture
Screen capture process 
File saved after screen capture 
Screenshot pictures 
1、 Write a ScreenCaptureUtil Tools , Screen capture and save , The code is as follows
/** * 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 {
// First save the picture
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;
}
}
}
a key :
1) obtain activity Screenshot generation Bitmap
View dView = activity.getWindow().getDecorView();
dView.setDrawingCacheEnabled(false);
dView.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(dView.getDrawingCache());2) Create file path preservation bitmap( Save to album root ->File.separator)
// First save the picture
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) Send a broadcast to notify the album to refresh and add this picture
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri uri = Uri.fromFile(file);
intent.setData(uri);
activity.sendBroadcast(intent);4) Destroy cache ( Not destroyed , Always save the contents of the first page )
dView.destroyDrawingCache();2、 stay activity Use in ( The project requirement is after the screen capture , Show me , Then the animation exits and disappears )
case R.id.btn_two:
// screenshots
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 = " Screen capture successful ";
} else {
content = " Screen capture failed ";
}
Toast.makeText(MainActivity.this, content, Toast.LENGTH_SHORT).show();
break; a key :
1) Loading animation , And display ( First, judge whether the save is successful bitmap Is it equal to null)
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scaleanimation);
animation.setFillAfter(true);
showImage.startAnimation(animation);2) Animation 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>Remember to add read and write permissions
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />边栏推荐
猜你喜欢

Activity启动模式和生命周期实测结果

MySQL面试真题(二十五)——常见的分组比较场景

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

visdom画多条动态损失曲线

What is the magic of digital collections? Which reliable teams are currently developing

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

Real MySQL interview question (23) -- pinduoduo ball game analysis

Wechat applet: Puzzle toolbox

Visdom draws multiple dynamic loss curves

MDM data cleaning function development description
随机推荐
Real MySQL interview questions (25) -- common group comparison scenarios
ORB_SLAM2运行
PAT 乙等 1016 C语言
啊哈C语言 第7章 有了它你能做更多的事(第27-28讲)
【斯坦福计网CS144项目】Lab2: TCPReceiver
数字藏品赋能实体产业释放了哪些利好?
Wechat applet: future wife query generator
云原生数据库是未来
Real MySQL interview question (XXVIII) -- case - Analysis of indicators of communication operators
阿里云 ACK One、ACK 云原生 AI 套件新发布,解决算力时代下场景化需求
MySQL面试真题(三十)——贝壳-房产订单分析
雷达图canvas
Heimdall Database Proxy横向扩展提高20倍
PAT 乙等 1019 C语言
Ansible 使用普通用户管理被控端
The official artifact of station B has cracked itself!
Digital collections - new investment opportunities
【owt】owt-client-native-p2p-e2e-test vs2017构建 6:修改脚本自动生成vs工程
Management system of borrowed books based on SSM framework
Wechat applet: Star Trek spaceship ticket production and generation