美文网首页
Android-uiautomator-takescreensh

Android-uiautomator-takescreensh

作者: __Andy__ | 来源:发表于2019-04-09 15:51 被阅读0次

UiDevice中共有2个截图方法,如下:

//重载方法一,参数只要传输图片保存位置即可,质量为90%,调用方法二
    /**
     * Take a screenshot of current window and store it as PNG
     *
     * Default scale of 1.0f (original size) and 90% quality is used
     * The screenshot is adjusted per screen rotation
     *
     * @param storePath where the PNG should be written to
     * @return true if screen shot is created successfully, false otherwise
     * @since API Level 17
     */
    public boolean takeScreenshot(File storePath) {
        Tracer.trace(storePath);
        return takeScreenshot(storePath, 1.0f, 90);
    }
//重载方法二
    /**
     * Take a screenshot of current window and store it as PNG
     *
     * The screenshot is adjusted per screen rotation
     *
     * @param storePath where the PNG should be written to
     * @param scale scale the screenshot down if needed; 1.0f for original size
     * @param quality quality of the PNG compression; range: 0-100
     * @return true if screen shot is created successfully, false otherwise
     * @since API Level 17
     */
    public boolean takeScreenshot(File storePath, float scale, int quality) {
        Tracer.trace(storePath, scale, quality);
        Bitmap screenshot = getUiAutomation().takeScreenshot();
        if (screenshot == null) {
            return false;
        }
        BufferedOutputStream bos = null;
        try {
            bos = new BufferedOutputStream(new FileOutputStream(storePath));
            if (bos != null) {
                screenshot.compress(Bitmap.CompressFormat.PNG, quality, bos);
                bos.flush();
            }
        } catch (IOException ioe) {
            Log.e(LOG_TAG, "failed to save screen shot to file", ioe);
            return false;
        } finally {
            if (bos != null) {
                try {
                    bos.close();
                } catch (IOException ioe) {
                    // Ignore
                }
            }
            screenshot.recycle();
        }
        return true;
    }```

主要调用UIAutomation中的截图方法
/**
 * Takes a screenshot.
 *
 * @return The screenshot bitmap on success, null otherwise.
 */
public Bitmap takeScreenshot() {
    synchronized (mLock) {
        throwIfNotConnectedLocked();
    }
    //获取屏幕,使用的android.view的事情
    Display display = DisplayManagerGlobal.getInstance()
            .getRealDisplay(Display.DEFAULT_DISPLAY);
    Point displaySize = new Point();
    display.getRealSize(displaySize);

    int rotation = display.getRotation();

    // Take the screenshot
    Bitmap screenShot = null;
    try {
        // Calling out without a lock held.
        //
        screenShot = mUiAutomationConnection.takeScreenshot(
                new Rect(0, 0, displaySize.x, displaySize.y), rotation);
        if (screenShot == null) {
            return null;
        }
    } catch (RemoteException re) {
        Log.e(LOG_TAG, "Error while taking screnshot!", re);
        return null;
    }

    // Optimization
    screenShot.setHasAlpha(false);
    return screenShot;
}

相关文章

网友评论

      本文标题:Android-uiautomator-takescreensh

      本文链接:https://www.haomeiwen.com/subject/swzbiqtx.html