本文主要是介绍UIAutomator2.0详解(UIDevice篇----截屏),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
UIDevice为截屏提供了两个接口方法。分别是:
(1)public boolean takeScreenshot(File storePath)
(2)public boolean takeScreenshot(File storePath, float scale, int quality)
查看源码,可以发现方法(1)实质上是对方法(2)的调用,2,3传参分别为1.0和90。
scale为缩小放大比例,quality为压缩质量。quality有效值为【0,100】。
我们再来看一下方法(2)的源码。
可以发现
(1)参数scale并未传给底层。因此,不管scale为何值,其实都无效。
(2)最终实质是对Bitmap的nativeCompress方法的调用。
注:当前UIAutomator源码版本为v18-2.1.2,其他版本可能有差异。
我们再通过测试实验来看一下,对于quality不同值的不同效果。
案例代码如下:
package com.breakloop.u2demo.uidevice;import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.util.Log;import org.junit.Test;import java.io.File;
import java.io.IOException;/*** Created by user on 2017/11/14.*/public class ScreenShotTest extends UIDeviceTest {private String path;private void init(){Context context= InstrumentationRegistry.getTargetContext();path=context.getExternalCacheDir().getPath();Log.i(TAG, "init: path = " + path);}private File createFile(String path,String fileName){File file=new File(path,fileName);try {if (file.exists()){file.delete();}file.createNewFile();} catch (IOException e) {e.printStackTrace();return null;}return file;}@Testpublic void TestCase1(){init();File file =createFile(path,"1.png");Log.i(TAG, "TestCase1: file path = " + file.getPath());mDevice.takeScreenshot(file,1.0f,10);mDevice.waitForIdle();}
}
执行后,使用adb将图片导出。
1_0.1.png:scale为0.1,quality为100
1_1.png:scale为1,quality为100
1_1_10.png:scale为1,quality为10
1_1_50.png:scale为1,quality为50
1_10.png:scale为1,quality为100
从文件大小来看,竟然无任何差别!测试手机为华为mate9,android7.0
这篇关于UIAutomator2.0详解(UIDevice篇----截屏)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!