UIAutomator2.0详解(UIDevice篇----截屏)

来源:互联网 发布:数据库 offset 编辑:程序博客网 时间:2024/05/29 19:24

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;    }    @Test    public 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

阅读全文
0 0
原创粉丝点击