Android實現(xiàn)長圖文截圖功能實例代碼
前言
截圖是我們?nèi)粘i_發(fā)經(jīng)常會遇到的一個功能,最近工作遇到的需求又升級了,需要實現(xiàn)長圖文的截圖功能,經(jīng)常查找相關(guān)資料終于實現(xiàn)了,支持截取微博、知乎、今日頭條等第三方APP......
先瞅瞅效果圖:
效果圖
再瞅瞅最終的長截圖:
上一周腦子突然冒出長截圖這個功能,想著如何截取如微博,知乎,頭條等這些第三方APP的界面呢?出于好奇心,花了一周業(yè)余時間,擼一個看看。
不就是截屏+拼圖,還能有什么難度么?這個。。。好像確實是。
Question:
1.如何截屏?
Android 5.0 API 21之前,想要系統(tǒng)截屏,是需要root,不過Android 5.0開始開放了響應的截屏接口:
MediaProjection (added in API level 21.)
A token granting applications the ability to capture screen contents and/or record system audio. The exact capabilities
granted depend on the type of MediaProjection.
2.如何優(yōu)雅的截圖?
懸浮窗那么小,難道每次我都得滑一定的距離,然后點一次懸浮窗么,理論上可以,但體驗不好。估計更多人傾向只要觸摸屏幕就可以截圖,所以選擇監(jiān)聽懸浮窗外的觸屏事件。
3.如何監(jiān)聽懸浮窗口外部的TouchEvent?
懸浮窗外的觸屏事件都已經(jīng)脫離整個應用了,如何監(jiān)聽呢?這里確實卡了些時間,因為確實找不到如何捕獲這個事件的好,我感覺這個問題也是最煩的一個,后來來了點靈感,我設置一個全屏的透明背景,然后給這個背景設置onTouch事件,哦?。?!恍然大悟,以為這樣就可以了?錯?。∵@樣會出現(xiàn)整個手機的事件都將被這個透明背景攔截,無法傳遞到手機桌面,如果非開發(fā)人員安裝了這個軟件。。,告訴他,重新開機吧。。。所以翻了下WindowManager的源碼,看到flag參數(shù),把各種flag參數(shù)的注釋看了遍,最后定位在如下幾個flag參數(shù)值上。
/** Window flag: this window won't ever get key input focus, so the * user can not send key or other button events to it. Those will * instead go to whatever focusable window is behind it. This flag * will also enable {@link #FLAG_NOT_TOUCH_MODAL} whether or not that * is explicitly set. * * <p>Setting this flag also implies that the window will not need to * interact with * a soft input method, so it will be Z-ordered and positioned * independently of any active input method (typically this means it * gets Z-ordered on top of the input method, so it can use the full * screen for its content and cover the input method if needed. You * can use {@link #FLAG_ALT_FOCUSABLE_IM} to modify this behavior. */ public static final int FLAG_NOT_FOCUSABLE = 0x00000008; /** Window flag: this window can never receive touch events. */ public static final int FLAG_NOT_TOUCHABLE = 0x00000010; /** Window flag: even when this window is focusable (its * {@link #FLAG_NOT_FOCUSABLE} is not set), allow any pointer events * outside of the window to be sent to the windows behind it. Otherwise * it will consume all pointer events itself, regardless of whether they * are inside of the window. */ public static final int FLAG_NOT_TOUCH_MODAL = 0x00000020; /** Window flag: if you have set {@link #FLAG_NOT_TOUCH_MODAL}, you * can set this flag to receive a single special MotionEvent with * the action * {@link MotionEvent#ACTION_OUTSIDE MotionEvent.ACTION_OUTSIDE} for * touches that occur outside of your window. Note that you will not * receive the full down/move/up gesture, only the location of the * first down as an ACTION_OUTSIDE. */ public static final int FLAG_WATCH_OUTSIDE_TOUCH = 0x00040000;
在全屏透明背景的環(huán)境下,本以為可以監(jiān)聽桌面的Down、Move、Up事件,但是出現(xiàn)事件全部被攔截死在這個透明背景上,無法傳到手機桌面,再發(fā)現(xiàn)組合這些參數(shù),總結(jié)這種思路不可取。
查看注釋可以知道設置FLAG_WATCH_OUTSIDE_TOUCH可以在窗口外部(即App外部)接收一個指定事件MotionEvent#ACTION_OUTSIDE,但同時,你將無法接收到任何的Down、Move、Up事件。所以,也只能這樣了。。有其它高招的兄弟指點下哈。
4.如何控制截屏頻次?
在步驟3的基礎上,基本可以做一個截圖策略了,比如,每接收一次ACTION_OUTSIDE就截一次圖,又或者,每次監(jiān)聽一次ACTION_OUTSIDE,就進行一次計數(shù)器的累加,為了保證截圖能承上啟下連貫,可以設置閾值為2這樣。
5.如何拼圖?
這里因人而異了,但目的都一樣,將上述步驟所截的圖對比出不同的地方,然后把不同的地方拼接起來。出于運算效率考慮,這里我是用JNI來實現(xiàn)的,主函數(shù):
JNIEXPORT void JNICALL Java_com_zfw_screenshot_utils_SewUtils_merge( JNIEnv *env, jobject thiz, jobject bmp0, jobject bmp1, jobject bmp2, int h0, int h1, int h2, int samePart, int len) { int *pixels_0 = lockPixel(env, bmp0); int *pixels_1 = lockPixel(env, bmp1); int *pixels_2 = lockPixel(env, bmp2); /* -------------------- merge the difference ----------------------- */ int index = 0; while(index < h0) { if(index < h1) { getRowPixels(pixels_0, index, pixels_1, index, len); } else { getRowPixels(pixels_0, index, pixels_2, index - h1 + samePart, len); } index++; } /* -------------------- merge the difference ----------------------- */ unlockPixel(env, bmp0); unlockPixel(env, bmp1); unlockPixel(env, bmp2); }
功能實現(xiàn)上沒什么難度,感覺更多的是得選好實現(xiàn)的策略,比如如何優(yōu)雅的監(jiān)聽懸浮窗外的Touch事件,如何優(yōu)雅的實現(xiàn)一個“定點”截圖的策略,如何優(yōu)雅的對比兩個Bitmap的不同地方,進行拼接。
源碼傳送門:https://github.com/zengfw/LongScreenShot (本地下載)
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
Android編程之Button控件配合Toast控件用法分析
這篇文章主要介紹了Android編程之Button控件配合Toast控件用法,結(jié)合實例形式分析了Button控件及Toast控件的功能及具體使用技巧,需要的朋友可以參考下2015-12-12Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常
這篇文章主要介紹了Android 加載大圖及多圖避免程序出現(xiàn)OOM(OutOfMemory)異常的相關(guān)資料,需要的朋友可以參考下2017-03-03Android利用Espresso進行UI自動化測試的方法詳解
因為我是搞android開發(fā)的,所以被分到了自動化測試小組,所以了解了一些UI自動化測試。下面這篇文章主要給大家介紹了關(guān)于Android利用Espresso進行UI自動化測試的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-12-12android中px和dp,px和sp之間的轉(zhuǎn)換方法
在Android開發(fā)中dp和px,sp和px之間的轉(zhuǎn)換時必不可少的。下面腳本之家小編給大家?guī)砹薬ndroid中px和dp,px和sp之間的轉(zhuǎn)換方法,感興趣的朋友一起看看吧2018-06-06解析Android應用啟動后自動創(chuàng)建桌面快捷方式的實現(xiàn)方法
和IOS開發(fā)和Windows Phone開發(fā)相比,Android是開放的,Android上的開發(fā)也相對更加靈活,能夠做很多事情。有的朋友會發(fā)現(xiàn),在某些Android應用安裝以后,第一次運行,就會在桌面創(chuàng)建快捷方式。這是如何做到的呢2013-05-05Android自定義View onDraw()方法會調(diào)用兩次的問題解決
這篇文章主要介紹了Android自定義View onDraw()方法會調(diào)用兩次的問題解決,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01