您好,登錄后才能下訂單哦!
鑒于網(wǎng)上使用MonkeyImage的實(shí)例除了方法sameAs外很難找到,所以本人把實(shí)踐各個(gè)API的過程記錄下來然自己有更感性的認(rèn)識,也為往后的工作打下更好的基礎(chǔ)。同時(shí)也和上一篇文章《MonkeyDevcie API 實(shí)踐全記錄》起到相互呼應(yīng)的作用。
因?yàn)椴]有MonkeyRunner的項(xiàng)目背景,所以這里更多的是描述各個(gè)API是怎么一回事,而不是描述在什么場景下需要用到。也就是說是去回答What,而不是How。
首先我們先看下官方給出的MonkeyImage的API描述,對比我現(xiàn)在反編譯的最新的源碼是一致的:
Return Type | Methods | Comment |
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. |
|
| getSubImage (tuple rect) Creates a new |
|
boolean | sameAs ( Compares this |
|
void | writeToFile (string path, string format) Writes the current image to the file specified by |
|
img = device.takeSnapshot() png1 = img.convertToBytes() png2 = img.convertToBytes() bmp = img.convertToBytes('bmp') jpg = img.convertToBytes('JPG') gif = img.convertToBytes('gif') raw = img.convertToBytes('raw') invalid = img.convertToBytes('xxx') #is the 2 pngs equal? print "Two png is equal in bytes:",png1 == png2 #is the png equals to bmp? print "png and bmp is equal in bytes:", png1 == bmp #is the jpg eqals to the raw? print "jpg and bmp is equals in bytes:",jpg == bmp #is the jpg eqals to the xxx? print "jpg is a valid argument:",jpg != invalid #is the gif eqals to the xxx? print "gif is a valid argument:",gif != invalid #is the bmp eqals to the xxx? print "bmp is a valid argument:",bmp != invalid #is the raw equas to xxxx? aims at checking whether argument 'raw' is invalid like 'xxx' print 'raw is a valid argument:',raw != invalid #would invalid argument drop to png by default? print 'Would invalid argument drop to png by default:',png1 == invalid輸出:
viewer = device.getHierarchyViewer() note = viewer.findViewById('id/title') text = viewer.getText(note) print text.encode('utf-8') point = viewer.getAbsoluteCenterOfView(note) x = point.x y = point.y img = device.takeSnapshot() pixelTuple = img.getRawPixel(x,y) pixelInt = img.getRawPixelInt(x,y) print "Pixel in tuple:",pixelTuple print "Pixel in int:", pixelInt輸出:
from com.android.monkeyrunner import MonkeyRunner,MonkeyDevice,MonkeyImage from com.android.monkeyrunner.easy import EasyMonkeyDevice,By from com.android.chimpchat.hierarchyviewer import HierarchyViewer from com.android.hierarchyviewerlib.models import ViewNode, Window from java.awt import Point #from com.android.hierarchyviewerlib.device import #Connect to the target targetDevice targetDevice = MonkeyRunner.waitForConnection() easy_device = EasyMonkeyDevice(targetDevice) #touch a button by id would need this targetDevice.startActivity(component="com.example.android.notepad/com.example.android.notepad.NotesList") #invoke the menu options MonkeyRunner.sleep(6) #targetDevice.press('KEYCODE_MENU', MonkeyDevice.DOWN_AND_UP); ''' public ViewNode findViewById(String id) * @param id id for the view. * @return view with the specified ID, or {@code null} if no view found. ''' #MonkeyRunner.alert("Continue?", "help", "Ok?") pic = targetDevice.takeSnapshot() pic = pic.getSubImage((0,38,480,762)) newPic = targetDevice.takeSnapshot() newPic = newPic.getSubImage((0,38,480,762)) print (newPic.sameAs(pic,1.0)) newPic.writeToFile('./shot1.png','png')