Adnroid activity截屏 保存显示到相册 View显示图片 动画消失

今天项目有个需求,截屏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" />
发布了43 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/qq_38355313/article/details/79115821