做效果展示時經常需要一些Gif圖來顯示動态效果,我目前使用的方法是手機錄屏生成視訊檔案,再通過第三方軟體用視訊檔案生成動圖。剛才制作了一個動圖效果圖如下:

這裡我使用的是adb的screenrecord指令,它是API Level 19以上才支援的,官方文檔原文如下:
The screenrecord command is a shell utility for recording the display of devices running Android 4.4 (API level 19) and higher. The utility records screen activity to an MPEG-4 file.
開始錄制
adb shell screenrecord /sdcard/demo.mp4
上面指令後開始錄制,預設情況下它會錄制3分鐘,按下ctrl+c會提前結束錄制。它支援 –size 、–bit-rate、–time-limit、–rotate等參數,最常用是前兩個。
導出視訊檔案
adb pull /sdcard/demo.mp4
使用第三方工具或者QQ直接傳到電腦上也行。
轉Gif檔案
有很多工具,我在Mac上使用的是GIFBrewery,傻瓜式的,可以指定GIF圖檔大小和幀數,就不截圖了。
還有一個網站線上制作,也很友善:http://ezgif.com/
注意
因為手機分辨率太高,我試用了一下使用模拟器截屏,發現會報錯:”unable to create encoder input surface (err=-38)”,後面查資料發現是系統bug,在高版本的模拟器上不再支援截屏指令,也就是目前隻能使用裝置截屏。
我在使用工具轉Gif時把分辨率改小,圖檔會模糊很多,錄屏時就設定分辨率效果會好很多(我的手機是1440*2560,生成320*480的圖,效果也隻能這樣了),指令如下:
adb shell screenrecord –size 848x480 /sdcard/demo.mp4
官方文檔:http://developer.android.com/tools/help/shell.html