当前位置:网站首页>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
 Picture description here
File saved after screen capture
 Picture description here
Screenshot pictures
 Picture description here

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" />
原网站

版权声明
本文为[Muzi 102]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230414189674.html