解析Android截取手機屏幕兩種實現(xiàn)方案
最近在開發(fā)的過程中,遇到了一個需要截取屏幕保存為圖片的需求,具體為截取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();
}
}
}
這個方法在很多情況下都是沒有問題的,比如說截取imageview,TextView,甚至otherview.getRootView();都沒問題,但在WebView上就會出現(xiàn)webview的部分截取完缺少頁面里的一些內(nèi)容的情況,比如說用webview打開這個(https://miqt.github.io/jellyfish/)界面,截取的圖片就會有問題,具體表現(xiàn)為網(wǎ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ā)送截圖請求
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());
}
這個方法類似使用手機的系統(tǒng)截屏(音量下鍵+電源鍵),能夠完美的吧當(dāng)前原模原樣的屏幕截取下來,并且修改保存方法的話甚至可以屏幕錄像,但相比于第一種方法,它的缺點是完全和界面上的view沒有關(guān)系,并且在調(diào)用這個服務(wù)的時候,會彈出一個權(quán)限確認(rèn)的彈框。另外需要注意,這一方法只能在Android 5.0的系統(tǒng)設(shè)備上適用。
總結(jié):
總而言之,這兩種方法各有利弊,使用的時候要根據(jù)自己的實際需求做出選擇。以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)中TextView各種常見使用方法小結(jié)
這篇文章主要介紹了Android開發(fā)中TextView各種常見使用方法,結(jié)合實例形式總結(jié)分析了Android開發(fā)中TextView各種常見布局與功能實現(xiàn)技巧,需要的朋友可以參考下2019-04-04
Kotlin協(xié)程Job生命周期結(jié)構(gòu)化并發(fā)詳解
這篇文章主要為大家介紹了Kotlin協(xié)程Job生命周期結(jié)構(gòu)化并發(fā)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android實現(xiàn)支持所有View的通用的下拉刷新控件
這篇文章主要介紹了Android實現(xiàn)支持所有View的通用的下拉刷新控件的相關(guān)資料,需要的朋友可以參考下2016-06-06
Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法
這篇文章主要介紹了Android 5.0及以上編程實現(xiàn)屏幕截圖功能的方法,結(jié)合實例形式分析了Android5.0以上實現(xiàn)截圖功能的相關(guān)類、函數(shù)及權(quán)限控制等操作技巧,需要的朋友可以參考下2018-01-01
Android編程使用LinearLayout和PullRefreshView實現(xiàn)上下翻頁功能的方法
這篇文章主要介紹了Android編程使用LinearLayout和PullRefreshView實現(xiàn)上下翻頁功能的方法,涉及Android界面布局與邏輯處理相關(guān)操作技巧,需要的朋友可以參考下2017-08-08
Android使用開源組件PagerBottomTabStrip實現(xiàn)底部菜單和頂部導(dǎo)航功能
這篇文章主要介紹了Android使用PagerBottomTabStrip實現(xiàn)底部菜單和頂部導(dǎo)航功能,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2018-08-08

