Android:應用內(nèi)部截屏的實現(xiàn)

/**
 * 作者:CnPeng
 * <p>
 * 時間:2017/6/27:下午3:06
 * <p>
 * 說明:應用程序內(nèi)部截屏的實現(xiàn)(不截狀態(tài)欄,兼容5.0 以上及以下版本)
 * -- 申請寫入SD的權(quán)限
 * -- 實現(xiàn)應用內(nèi)截取屏幕的功能(不截狀態(tài)欄)
 * -- 計算狀態(tài)欄的高度(兩種方式)
 * -- 發(fā)送廣播通知相冊/圖庫刷新數(shù)據(jù)
 */

public class ScreenShotActivity extends AppCompatActivity {

    private Button    bt_screenShot;
    private ImageView iv_showScreenShot;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen_shot);

        initView();
        initEvent();
    }

    private void initView() {
        bt_screenShot = (Button) findViewById(R.id.bt_clickToScreenShot);
        iv_showScreenShot = (ImageView) findViewById(R.id.iv_showScreenShot);
    }

    private void initEvent() {
        bt_screenShot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {       //點擊之后,保存截圖,更新相冊
                Bitmap bitmap = getScreenShotBmp();
                boolean saveSuccess = saveScreenShotToSD(bitmap);
                String hint = saveSuccess ? "保存成功" : "保存失敗";
                Toast.makeText(ScreenShotActivity.this, hint, Toast.LENGTH_SHORT).show();

                if (saveSuccess) {
                    iv_showScreenShot.setImageBitmap(bitmap);
                }
            }
        });
    }

    /**
     * 發(fā)送廣播更新相冊,不更新的話,在相冊中將無法查看到截取的圖片
     */
    private void updateGallery(File file) {
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri uri = Uri.fromFile(file);
        intent.setData(uri);
        sendBroadcast(intent);
    }

    /**
     * 保存截圖到本地
     *
     * @param bitmap 截取到的圖片
     */
    private boolean saveScreenShotToSD(Bitmap bitmap) {
        if (bitmap != null) {
            if (Environment.getExternalStorageState().equals(MEDIA_MOUNTED)) {  //如果SD存儲設備可用
                String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "校園集結(jié)號";
                File dir = new File(path);
                if (!dir.exists()) {
                    dir.mkdir();   //創(chuàng)建目錄
                }

                File file = new File(dir, "截圖成功.png");
                FileOutputStream fos = null;
                try {
                    fos = new FileOutputStream(file);

                    //根據(jù)指定的格式、質(zhì)量、輸出流 將bitmap保存到本地,并返回是否保存成功
                    boolean saveSuccess = bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);

                    if (saveSuccess) {
                        updateGallery(file);    //刷新相冊
                    }

                    return saveSuccess;
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                    return false;
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();    //關(guān)閉流防止溢出
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                }
            } else {
                Toast.makeText(this, "本地存儲空間不可用", Toast.LENGTH_SHORT).show();
            }
        }
        return false;
    }

    /**
     * 獲取屏幕截圖
     */
    private Bitmap getScreenShotBmp() {
        View decorView = getWindow().getDecorView();    //獲取當前activity所在的最頂層的view--DecorView
        decorView.setDrawingCacheEnabled(true);         //啟用繪圖緩存
        decorView.buildDrawingCache();                  //強制構(gòu)建繪圖緩存(防止上面啟用繪圖緩存的操作失?。?        Bitmap bitmap = decorView.getDrawingCache();     //獲取繪圖緩存中的 bitmap

        //        int statusBarHeight = getStatusBarHeight();
        int statusBarHeight = getStatusBarHeight(decorView);

        int newBmpHeight = bitmap.getHeight() - statusBarHeight;    //最終截取的圖片的高度(取出狀態(tài)欄之后的高度)

        bitmap = Bitmap.createBitmap(bitmap, 0, statusBarHeight, bitmap.getWidth(), newBmpHeight);

        decorView.setDrawingCacheEnabled(false);    //createBitmap完成之后一定要置為false,否則短時間內(nèi)多次截圖時內(nèi)容不會變化!
        return bitmap;
    }

    /**
     * 獲取狀態(tài)欄高度方式2
     *
     * @param decorView 要獲取狀態(tài)欄高度的頁面所在的頂層布局
     */
    private int getStatusBarHeight(View decorView) {
        Rect rect = new Rect();
        decorView.getWindowVisibleDisplayFrame(rect);
        return rect.top;
    }

    /**
     * 獲取狀態(tài)欄的高度--方式1
     */
    public int getStatusBarHeight() {
        int statusBarHeight = 0;
        int resourceID = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceID != 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceID);
        }
        return statusBarHeight;
    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容