Android自动化工具Monkeyrunner使用(三)
本章开始介绍下Monkeyrunner的API, 以便了解其具体的使用。
MonkeyImage takeSnapshot(): 截取当前设备的屏幕, 并获得当前图片, 返回一个monkryimage对象。
Captures the entire screen buffer of this device, yielding a screen capture of the current display.
Returns
- A MonkeyImage object containing the image of the current display.
E.g. 截取当前界面,返回一个对象
short_begin = device.takeSnapshot()
这个对象有5中方法可以调用:
Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
string | convertToBytes ( string format) Converts the current image to a particular format and returns it as a string that you can then access as an iterable of binary bytes. | ||||||||||
tuple | getRawPixel ( integer x, integer y) Returns the single pixel at the image location (x,y), as an a tuple of integer, in the form (a,r,g,b). | ||||||||||
integer | getRawPixelInt ( integer x, integer y) Returns the single pixel at the image location (x,y), as a 32-bit integer. | ||||||||||
MonkeyImage | getSubImage ( tuple rect) Creates a new MonkeyImage object from a rectangular selection of the current image. | ||||||||||
boolean | sameAs ( MonkeyImage other, float percent) Compares this MonkeyImage object to another and returns the result of the comparison. The percent argument specifies the percentage difference that is allowed for the two images to be "equal". | ||||||||||
void | writeToFile ( string path, string format) Writes the current image to the file specified by filename , in the format specified by format . |
其中经常使用的为 sameAs(MonkeyImage other, float percent) , writeToFile(string path, string format)
sameAs(MonkeyImage other, float percent): 是对截取的image对象的比较, 不足之处是不能把手机界面和电脑上的图片进行比较
Compares this MonkeyImage
object to another and returns the result of the comparison. The percent
argument specifies the percentage difference that is allowed for the two images to be "equal".
short_begin = device.takeSnapshot()
short_end = device.takeSnapshot()
print short_end.sameAs(short_begin, 1.0)
writeToFile (string filename, string format): 保存当前截取的图片, 需要选择路径和格式。格式可以选择png, bmp, jpg等。
short_begin.writeToFile('d:\shotbegin.png','png')
更多相关文章
- Android(安卓)图片OutOfMemory异常bitmap size exceeds VM budge
- Android中简单的日期格式化
- 自定义圆形的ProgressBar
- android绘制圆形图片、圆圈以及图片缩放
- Android(安卓)拍照,从相册获取图片TakePhoto框架,一句话完成
- 【Android】ExpandableListView 默认展开
- Android(安卓)Service的两种启动方式
- Android(安卓)Drawable 对象复制、拷贝
- Android(安卓)APP中跳转至微信,分享图文给好友或者朋友圈(加跳转QQ