本章开始介绍下MonkeyrunnerAPI, 以便了解其具体的使用。

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')


更多相关文章

  1. Android(安卓)图片OutOfMemory异常bitmap size exceeds VM budge
  2. Android中简单的日期格式化
  3. 自定义圆形的ProgressBar
  4. android绘制圆形图片、圆圈以及图片缩放
  5. Android(安卓)拍照,从相册获取图片TakePhoto框架,一句话完成
  6. 【Android】ExpandableListView 默认展开
  7. Android(安卓)Service的两种启动方式
  8. Android(安卓)Drawable 对象复制、拷贝
  9. Android(安卓)APP中跳转至微信,分享图文给好友或者朋友圈(加跳转QQ

随机推荐

  1. Android:EditText 多行显示、属性汇总、族
  2. 【android 其他】转载:Android(安卓)简史
  3. Android(安卓)TextView内容过长加省略号,
  4. android的对话框
  5. ActionBar
  6. Android(安卓)activity属性设置大全
  7. Android开发EditText属性
  8. 【Android】Android(安卓)apk默认安装位
  9. android底层开发-android基础架构
  10. Android_传感器综述