歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android--將布局保存成圖像

Android--將布局保存成圖像

日期:2017/3/1 10:01:35   编辑:Linux編程

//將布局轉換為View類型對象
View view = getLayoutInflater().inflate(R.layout.main, null);
//打開圖像緩存
view.setDrawingCacheEnabled(true);
//必須調用measure和layout方法才能成功保存可視組件的截圖到png圖像文件
//測量View大小
view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
//發送位置和尺寸到View及其所有的子View
view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
try{
//獲得可視組件的截圖
Bitmap bitmap = view.getDrawingCache();
//將截圖保存在SD卡根目錄的test.png圖像文件中
FileOutputStream fos = new FileOutputStream("/sdcard/test.png");
//將Bitmap對象中的圖像數據壓縮成png格式的圖像數據,並將這些數據保存在test.png文件中
bitmap.compress(CompressFormat.PNG, 100, fos);
//關閉文件輸出流
fos.close();
}catch (Exception e) {

}

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved