解析Android截取手機(jī)屏幕兩種實(shí)現(xiàn)方案
最近在開發(fā)的過程中,遇到了一個(gè)需要截取屏幕保存為圖片的需求,具體為截取webview的視圖保存圖片。
方法1:首先想到的思路是利用SDK提供的View.getDrawingCache()方法:
public void printScreen(View view) {
String imgPath = "/sdcard/test.png";
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap != null) {
try {
FileOutputStream out = new FileOutputStream(imgPath);
bitmap.compress(Bitmap.CompressFormat.PNG, 100,
out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
這個(gè)方法在很多情況下都是沒有問題的,比如說截取imageview,TextView,甚至otherview.getRootView();都沒問題,但在WebView上就會(huì)出現(xiàn)webview的部分截取完缺少頁面里的一些內(nèi)容的情況,比如說用webview打開這個(gè)(https://miqt.github.io/jellyfish/)界面,截取的圖片就會(huì)有問題,具體表現(xiàn)為網(wǎng)頁中游動(dòng)的水母沒有顯示在截取的圖片上。
方法2:使用Android系統(tǒng)提供的服務(wù)Context.MEDIA_PROJECTION_SERVICE,進(jìn)行截圖操作。
github地址:https://github.com/miqt/CapWindow
demo源碼下載地址:CapWindow_jb51.rar
關(guān)鍵部分代碼解析:↓
發(fā)送截圖請(qǐng)求
final MediaProjectionManager projectionManager = (MediaProjectionManager)
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
Intent intent = projectionManager.createScreenCaptureIntent();
startActivityForResult(intent, REQUEST_CODE);
接收返回的結(jié)果:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
handleScreenShotIntent(resultCode, data);
}
private void handleScreenShotIntent(int resultCode, Intent data) {
onScreenshotTaskBegan();
final MediaProjectionManager projectionManager = (MediaProjectionManager)
getSystemService(Context.MEDIA_PROJECTION_SERVICE);
final MediaProjection mProjection = projectionManager.getMediaProjection(resultCode, data);
Point size = Utils.getScreenSize(this);
final int mWidth = size.x;
final int mHeight = size.y;
final ImageReader mImageReader = ImageReader.newInstance(mWidth, mHeight, PixelFormat
.RGBA_8888, 2);
final VirtualDisplay display = mProjection.createVirtualDisplay("screen-mirror", mWidth,
mHeight, DisplayMetrics.DENSITY_MEDIUM,
DisplayManager.VIRTUAL_DISPLAY_FLAG_PRESENTATION, mImageReader.getSurface(),
null, null);
mImageReader.setOnImageAvailableListener(new ImageReader.OnImageAvailableListener() {
@Override
public void onImageAvailable(ImageReader mImageReader) {
Image image = null;
try {
image = mImageReader.acquireLatestImage();
if (image != null) {
final Image.Plane[] planes = image.getPlanes();
if (planes.length > 0) {
final ByteBuffer buffer = planes[0].getBuffer();
int pixelStride = planes[0].getPixelStride();
int rowStride = planes[0].getRowStride();
int rowPadding = rowStride - pixelStride * mWidth;
// create bitmap
Bitmap bmp = Bitmap.createBitmap(mWidth + rowPadding / pixelStride,
mHeight, Bitmap.Config.ARGB_8888);
bmp.copyPixelsFromBuffer(buffer);
Bitmap croppedBitmap = Bitmap.createBitmap(bmp, 0, 0, mWidth, mHeight);
saveBitmap(croppedBitmap);//保存圖片
if (croppedBitmap != null) {
croppedBitmap.recycle();
}
if (bmp != null) {
bmp.recycle();
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (image != null) {
image.close();
}
if (mImageReader != null) {
mImageReader.close();
}
if (display != null) {
display.release();
}
mImageReader.setOnImageAvailableListener(null, null);
mProjection.stop();
onScreenshotTaskOver();
}
}
}, getBackgroundHandler());
}
這個(gè)方法類似使用手機(jī)的系統(tǒng)截屏(音量下鍵+電源鍵),能夠完美的吧當(dāng)前原模原樣的屏幕截取下來,并且修改保存方法的話甚至可以屏幕錄像,但相比于第一種方法,它的缺點(diǎn)是完全和界面上的view沒有關(guān)系,并且在調(diào)用這個(gè)服務(wù)的時(shí)候,會(huì)彈出一個(gè)權(quán)限確認(rèn)的彈框。另外需要注意,這一方法只能在Android 5.0的系統(tǒng)設(shè)備上適用。
總結(jié):
總而言之,這兩種方法各有利弊,使用的時(shí)候要根據(jù)自己的實(shí)際需求做出選擇。以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android Bitmap的截取及狀態(tài)欄的隱藏和顯示功能
- Android實(shí)現(xiàn)bitmap指定區(qū)域滑動(dòng)截取功能
- android 手機(jī)截取長屏實(shí)例代碼
- Android實(shí)現(xiàn)拍照截取和相冊(cè)圖片截取
- Android個(gè)人中心的頭像上傳,圖片編碼及截取實(shí)例
- Android 仿QQ頭像自定義截取功能
- Android開發(fā)獲取短信的內(nèi)容并截取短信
- Android中截取當(dāng)前屏幕圖片的實(shí)例代碼
- Android截取視頻幀并轉(zhuǎn)化為Bitmap示例
- Android截取指定View為圖片的實(shí)現(xiàn)方法
相關(guān)文章
Android APK文件在電腦(PC虛擬機(jī))上面運(yùn)行方法
APK是Android系統(tǒng)的發(fā)布的工程包,很多時(shí)候我們想在電腦上而非Android手機(jī)上面運(yùn)行它,需要的朋友可以了解下2012-12-12
Android中Listview下拉刷新和上拉加載更多的多種實(shí)現(xiàn)方案
本文大概通過三種方案給大家介紹了Android中Listview下拉刷新和上拉加載更多知識(shí),非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下2016-12-12
Handler制作簡單相冊(cè)查看器的實(shí)例代碼
下面小編就為大家分享一篇Handler制作簡單相冊(cè)查看器的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-01-01
Android AIDL和遠(yuǎn)程Service調(diào)用示例代碼
本文主要介紹Android AIDL和遠(yuǎn)程Service,這里詳細(xì)介紹了相關(guān)知識(shí),并附實(shí)例代碼和實(shí)現(xiàn)效果圖,有興趣的朋友參考下2016-08-08
Android編程基礎(chǔ)之Menu功能菜單設(shè)計(jì)實(shí)例
這篇文章主要介紹了Android編程基礎(chǔ)之Menu功能菜單,結(jié)合實(shí)例形式分析了基本的Menu功能菜單原理、定義與響應(yīng)機(jī)制,需要的朋友可以參考下2016-10-10
Android編程實(shí)現(xiàn)小說閱讀器滑動(dòng)效果的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)小說閱讀器滑動(dòng)效果的方法,涉及onTouch事件滑動(dòng)效果的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10
Android實(shí)現(xiàn)底部切換標(biāo)簽
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)底部切換標(biāo)簽,嵌套Fragment,方便自定義布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
anndroid使用ViewPager實(shí)現(xiàn)三個(gè)fragment切換
這篇文章主要介紹了anndroid使用ViewPager實(shí)現(xiàn)三個(gè)fragment切換,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
Flutter輸入框TextField屬性及監(jiān)聽事件介紹
這篇文章主要介紹了Flutter輸入框TextField屬性及監(jiān)聽事件介紹,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2021-11-11

