Android 實(shí)現(xiàn)截屏功能的實(shí)例
更新時間:2017年08月21日 14:11:14 作者:Jlins
這篇文章主要介紹了Android 實(shí)現(xiàn)截屏功能的實(shí)例的相關(guān)資料,這里實(shí)現(xiàn)截屏的實(shí)例在代碼中注釋非常清楚,希望能幫助到大家,需要的朋友可以參考下
Android 實(shí)現(xiàn)截屏功能的實(shí)例
實(shí)現(xiàn)代碼:
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)用。
或者在 以下方法這里調(diào)用。
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
ScreenShot.shoot(this);
}
以上就是Android截屏的實(shí)例,如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
flutter實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了flutter實(shí)現(xiàn)底部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03
Android自定義view實(shí)現(xiàn)太極效果實(shí)例代碼
這篇文章主要介紹了Android自定義view實(shí)現(xiàn)太極效果實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android之帶group指示器的ExpandableListView(自寫)
Android缺省的ExpandableListView的group header無法固定在界面上,在網(wǎng)上搜索了好多都不盡人意,于是乎在別人的基礎(chǔ)上改進(jìn)了一點(diǎn)點(diǎn),原理都差不多2013-06-06
Android Room數(shù)據(jù)庫自動升級與遷移的策略
在 Android 應(yīng)用開發(fā)中,Room 是 Google 提供的一個輕量級數(shù)據(jù)庫框架,用于簡化與 SQLite 的交互,本文將介紹 Room 數(shù)據(jù)庫升級的幾種場景和常見的處理方法,包括手動遷移和自動遷移的策略,需要的朋友可以參考下2024-09-09

