国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android截屏保存png圖片的實(shí)例代碼

2020-04-11 12:00:30
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;

public class ScreenShot {
    // 獲取指定Activity的截屏,保存到png文件
    private static Bitmap takeScreenShot(Activity activity) {
        // View是你需要截圖的View
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap b1 = view.getDrawingCache();

        // 獲取狀態(tài)欄高度
        Rect frame = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
        int statusBarHeight = frame.top;
        Log.i("TAG", "" + statusBarHeight);

        // 獲取屏幕長和高
        int width = activity.getWindowManager().getDefaultDisplay().getWidth();
        int height = activity.getWindowManager().getDefaultDisplay()
                .getHeight();
        // 去掉標(biāo)題欄
        // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455);
        Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height
                - statusBarHeight);
        view.destroyDrawingCache();
        return b;
    }

    // 保存到sdcard
    private static void savePic(Bitmap b, String strFileName) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(strFileName);
            if (null != fos) {
                b.compress(Bitmap.CompressFormat.PNG, 90, fos);
                fos.flush();
                fos.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 程序入口
    public static void shoot(Activity a) {
        ScreenShot.savePic(ScreenShot.takeScreenShot(a), "sdcard/xx.png");
    }
}



需要注意的是,shoot方法只能在view已經(jīng)被加載后方可調(diào)用。
或者在   
復(fù)制代碼 代碼如下:

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        // TODO Auto-generated method stub
        super.onWindowFocusChanged(hasFocus);
        ScreenShot.shoot(this);
    }中調(diào)用

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阿勒泰市| 土默特左旗| 巩义市| 华安县| 溧阳市| 青岛市| 肃宁县| 兴宁市| 泸定县| 隆昌县| 山丹县| 昔阳县| 印江| 汤原县| 金堂县| 许昌县| 洛宁县| 高淳县| 大关县| 安义县| 连山| 衢州市| 剑河县| 化德县| 华阴市| 磴口县| 桐乡市| 泗阳县| 安陆市| 谷城县| 溧阳市| 墨脱县| 巴塘县| 武城县| 濮阳市| 宜良县| 都江堰市| 积石山| 疏勒县| 盖州市| 海宁市|