Search in sources :

Example 6 with HardwareConfig

use of com.android.ide.common.rendering.api.HardwareConfig in project android_frameworks_base by DirtyUnicorns.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    // get the drawable resource value
    DrawableParams params = getParams();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ResourceValue drawableResource = params.getDrawable();
    // resolve it
    BridgeContext context = getContext();
    drawableResource = context.getRenderResources().resolveResValue(drawableResource);
    if (drawableResource == null) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    ResourceType resourceType = drawableResource.getResourceType();
    if (resourceType != ResourceType.DRAWABLE && resourceType != ResourceType.MIPMAP) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    Drawable d = ResourceHelper.getDrawable(drawableResource, context);
    final Boolean allStates = params.getFlag(RenderParamsFlags.FLAG_KEY_RENDER_ALL_DRAWABLE_STATES);
    if (allStates == Boolean.TRUE) {
        final List<BufferedImage> result;
        if (d instanceof StateListDrawable) {
            result = new ArrayList<BufferedImage>();
            final StateListDrawable stateList = (StateListDrawable) d;
            for (int i = 0; i < stateList.getStateCount(); i++) {
                final Drawable stateDrawable = stateList.getStateDrawable(i);
                result.add(renderImage(hardwareConfig, stateDrawable, context));
            }
        } else {
            result = Collections.singletonList(renderImage(hardwareConfig, d, context));
        }
        return Status.SUCCESS.createResult(result);
    } else {
        BufferedImage image = renderImage(hardwareConfig, d, context);
        return Status.SUCCESS.createResult(image);
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) StateListDrawable(android.graphics.drawable.StateListDrawable) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Example 7 with HardwareConfig

use of com.android.ide.common.rendering.api.HardwareConfig in project android_frameworks_base by DirtyUnicorns.

the class RenderSessionImpl method measure.

/**
     * Measures the the current layout if needed (see {@link #invalidateRenderingSize}).
     */
private void measure(@NonNull SessionParams params) {
    // only do the screen measure when needed.
    if (mMeasuredScreenWidth != -1) {
        return;
    }
    RenderingMode renderingMode = params.getRenderingMode();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    mNewRenderSize = true;
    mMeasuredScreenWidth = hardwareConfig.getScreenWidth();
    mMeasuredScreenHeight = hardwareConfig.getScreenHeight();
    if (renderingMode != RenderingMode.NORMAL) {
        int widthMeasureSpecMode = renderingMode.isHorizExpand() ? // this lets us know the actual needed size
        MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
        int heightMeasureSpecMode = renderingMode.isVertExpand() ? // this lets us know the actual needed size
        MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
        // We used to compare the measured size of the content to the screen size but
        // this does not work anymore due to the 2 following issues:
        // - If the content is in a decor (system bar, title/action bar), the root view
        //   will not resize even with the UNSPECIFIED because of the embedded layout.
        // - If there is no decor, but a dialog frame, then the dialog padding prevents
        //   comparing the size of the content to the screen frame (as it would not
        //   take into account the dialog padding).
        // The solution is to first get the content size in a normal rendering, inside
        // the decor or the dialog padding.
        // Then measure only the content with UNSPECIFIED to see the size difference
        // and apply this to the screen size.
        // first measure the full layout, with EXACTLY to get the size of the
        // content as it is inside the decor/dialog
        @SuppressWarnings("deprecation") Pair<Integer, Integer> exactMeasure = measureView(mViewRoot, mContentRoot.getChildAt(0), mMeasuredScreenWidth, MeasureSpec.EXACTLY, mMeasuredScreenHeight, MeasureSpec.EXACTLY);
        // now measure the content only using UNSPECIFIED (where applicable, based on
        // the rendering mode). This will give us the size the content needs.
        @SuppressWarnings("deprecation") Pair<Integer, Integer> result = measureView(mContentRoot, mContentRoot.getChildAt(0), mMeasuredScreenWidth, widthMeasureSpecMode, mMeasuredScreenHeight, heightMeasureSpecMode);
        // now look at the difference and add what is needed.
        if (renderingMode.isHorizExpand()) {
            int measuredWidth = exactMeasure.getFirst();
            int neededWidth = result.getFirst();
            if (neededWidth > measuredWidth) {
                mMeasuredScreenWidth += neededWidth - measuredWidth;
            }
            if (mMeasuredScreenWidth < measuredWidth) {
                // If the screen width is less than the exact measured width,
                // expand to match.
                mMeasuredScreenWidth = measuredWidth;
            }
        }
        if (renderingMode.isVertExpand()) {
            int measuredHeight = exactMeasure.getSecond();
            int neededHeight = result.getSecond();
            if (neededHeight > measuredHeight) {
                mMeasuredScreenHeight += neededHeight - measuredHeight;
            }
            if (mMeasuredScreenHeight < measuredHeight) {
                // If the screen height is less than the exact measured height,
                // expand to match.
                mMeasuredScreenHeight = measuredHeight;
            }
        }
    }
}
Also used : RenderingMode(com.android.ide.common.rendering.api.SessionParams.RenderingMode) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig)

Example 8 with HardwareConfig

use of com.android.ide.common.rendering.api.HardwareConfig in project android_frameworks_base by AOSPA.

the class RenderDrawable method render.

public Result render() {
    checkLock();
    // get the drawable resource value
    DrawableParams params = getParams();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    ResourceValue drawableResource = params.getDrawable();
    // resolve it
    BridgeContext context = getContext();
    drawableResource = context.getRenderResources().resolveResValue(drawableResource);
    if (drawableResource == null) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    ResourceType resourceType = drawableResource.getResourceType();
    if (resourceType != ResourceType.DRAWABLE && resourceType != ResourceType.MIPMAP) {
        return Status.ERROR_NOT_A_DRAWABLE.createResult();
    }
    Drawable d = ResourceHelper.getDrawable(drawableResource, context);
    final Boolean allStates = params.getFlag(RenderParamsFlags.FLAG_KEY_RENDER_ALL_DRAWABLE_STATES);
    if (allStates == Boolean.TRUE) {
        final List<BufferedImage> result;
        if (d instanceof StateListDrawable) {
            result = new ArrayList<BufferedImage>();
            final StateListDrawable stateList = (StateListDrawable) d;
            for (int i = 0; i < stateList.getStateCount(); i++) {
                final Drawable stateDrawable = stateList.getStateDrawable(i);
                result.add(renderImage(hardwareConfig, stateDrawable, context));
            }
        } else {
            result = Collections.singletonList(renderImage(hardwareConfig, d, context));
        }
        return Status.SUCCESS.createResult(result);
    } else {
        BufferedImage image = renderImage(hardwareConfig, d, context);
        return Status.SUCCESS.createResult(image);
    }
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) ResourceValue(com.android.ide.common.rendering.api.ResourceValue) Drawable(android.graphics.drawable.Drawable) StateListDrawable(android.graphics.drawable.StateListDrawable) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) ResourceType(com.android.resources.ResourceType) StateListDrawable(android.graphics.drawable.StateListDrawable) BufferedImage(java.awt.image.BufferedImage) DrawableParams(com.android.ide.common.rendering.api.DrawableParams)

Example 9 with HardwareConfig

use of com.android.ide.common.rendering.api.HardwareConfig in project android_frameworks_base by AOSPA.

the class RenderSessionImpl method measure.

/**
     * Measures the the current layout if needed (see {@link #invalidateRenderingSize}).
     */
private void measure(@NonNull SessionParams params) {
    // only do the screen measure when needed.
    if (mMeasuredScreenWidth != -1) {
        return;
    }
    RenderingMode renderingMode = params.getRenderingMode();
    HardwareConfig hardwareConfig = params.getHardwareConfig();
    mNewRenderSize = true;
    mMeasuredScreenWidth = hardwareConfig.getScreenWidth();
    mMeasuredScreenHeight = hardwareConfig.getScreenHeight();
    if (renderingMode != RenderingMode.NORMAL) {
        int widthMeasureSpecMode = renderingMode.isHorizExpand() ? // this lets us know the actual needed size
        MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
        int heightMeasureSpecMode = renderingMode.isVertExpand() ? // this lets us know the actual needed size
        MeasureSpec.UNSPECIFIED : MeasureSpec.EXACTLY;
        // We used to compare the measured size of the content to the screen size but
        // this does not work anymore due to the 2 following issues:
        // - If the content is in a decor (system bar, title/action bar), the root view
        //   will not resize even with the UNSPECIFIED because of the embedded layout.
        // - If there is no decor, but a dialog frame, then the dialog padding prevents
        //   comparing the size of the content to the screen frame (as it would not
        //   take into account the dialog padding).
        // The solution is to first get the content size in a normal rendering, inside
        // the decor or the dialog padding.
        // Then measure only the content with UNSPECIFIED to see the size difference
        // and apply this to the screen size.
        // first measure the full layout, with EXACTLY to get the size of the
        // content as it is inside the decor/dialog
        @SuppressWarnings("deprecation") Pair<Integer, Integer> exactMeasure = measureView(mViewRoot, mContentRoot.getChildAt(0), mMeasuredScreenWidth, MeasureSpec.EXACTLY, mMeasuredScreenHeight, MeasureSpec.EXACTLY);
        // now measure the content only using UNSPECIFIED (where applicable, based on
        // the rendering mode). This will give us the size the content needs.
        @SuppressWarnings("deprecation") Pair<Integer, Integer> result = measureView(mContentRoot, mContentRoot.getChildAt(0), mMeasuredScreenWidth, widthMeasureSpecMode, mMeasuredScreenHeight, heightMeasureSpecMode);
        // now look at the difference and add what is needed.
        if (renderingMode.isHorizExpand()) {
            int measuredWidth = exactMeasure.getFirst();
            int neededWidth = result.getFirst();
            if (neededWidth > measuredWidth) {
                mMeasuredScreenWidth += neededWidth - measuredWidth;
            }
            if (mMeasuredScreenWidth < measuredWidth) {
                // If the screen width is less than the exact measured width,
                // expand to match.
                mMeasuredScreenWidth = measuredWidth;
            }
        }
        if (renderingMode.isVertExpand()) {
            int measuredHeight = exactMeasure.getSecond();
            int neededHeight = result.getSecond();
            if (neededHeight > measuredHeight) {
                mMeasuredScreenHeight += neededHeight - measuredHeight;
            }
            if (mMeasuredScreenHeight < measuredHeight) {
                // If the screen height is less than the exact measured height,
                // expand to match.
                mMeasuredScreenHeight = measuredHeight;
            }
        }
    }
}
Also used : RenderingMode(com.android.ide.common.rendering.api.SessionParams.RenderingMode) HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig)

Example 10 with HardwareConfig

use of com.android.ide.common.rendering.api.HardwareConfig in project android_frameworks_base by AOSPA.

the class RenderAction method init.

/**
     * Initializes and acquires the scene, creating various Android objects such as context,
     * inflater, and parser.
     *
     * @param timeout the time to wait if another rendering is happening.
     *
     * @return whether the scene was prepared
     *
     * @see #acquire(long)
     * @see #release()
     */
public Result init(long timeout) {
    // acquire the lock. if the result is null, lock was just acquired, otherwise, return
    // the result.
    Result result = acquireLock(timeout);
    if (result != null) {
        return result;
    }
    HardwareConfig hardwareConfig = mParams.getHardwareConfig();
    // setup the display Metrics.
    DisplayMetrics metrics = new DisplayMetrics();
    metrics.densityDpi = metrics.noncompatDensityDpi = hardwareConfig.getDensity().getDpiValue();
    metrics.density = metrics.noncompatDensity = metrics.densityDpi / (float) DisplayMetrics.DENSITY_DEFAULT;
    metrics.scaledDensity = metrics.noncompatScaledDensity = metrics.density;
    metrics.widthPixels = metrics.noncompatWidthPixels = hardwareConfig.getScreenWidth();
    metrics.heightPixels = metrics.noncompatHeightPixels = hardwareConfig.getScreenHeight();
    metrics.xdpi = metrics.noncompatXdpi = hardwareConfig.getXdpi();
    metrics.ydpi = metrics.noncompatYdpi = hardwareConfig.getYdpi();
    RenderResources resources = mParams.getResources();
    // build the context
    mContext = new BridgeContext(mParams.getProjectKey(), metrics, resources, mParams.getAssets(), mParams.getLayoutlibCallback(), getConfiguration(mParams), mParams.getTargetSdkVersion(), mParams.isRtlSupported());
    setUp();
    return SUCCESS.createResult();
}
Also used : HardwareConfig(com.android.ide.common.rendering.api.HardwareConfig) RenderResources(com.android.ide.common.rendering.api.RenderResources) BridgeContext(com.android.layoutlib.bridge.android.BridgeContext) DisplayMetrics(android.util.DisplayMetrics) Result(com.android.ide.common.rendering.api.Result)

Aggregations

HardwareConfig (com.android.ide.common.rendering.api.HardwareConfig)33 BridgeContext (com.android.layoutlib.bridge.android.BridgeContext)13 BufferedImage (java.awt.image.BufferedImage)12 Result (com.android.ide.common.rendering.api.Result)11 Bitmap (android.graphics.Bitmap)7 Canvas (android.graphics.Canvas)7 Drawable (android.graphics.drawable.Drawable)7 ResourceValue (com.android.ide.common.rendering.api.ResourceValue)7 SessionParams (com.android.ide.common.rendering.api.SessionParams)7 Configuration (android.content.res.Configuration)6 DisplayMetrics (android.util.DisplayMetrics)6 DrawableParams (com.android.ide.common.rendering.api.DrawableParams)6 RenderResources (com.android.ide.common.rendering.api.RenderResources)6 RenderingMode (com.android.ide.common.rendering.api.SessionParams.RenderingMode)6 Density (com.android.resources.Density)6 ScreenOrientation (com.android.resources.ScreenOrientation)6 ScreenSize (com.android.resources.ScreenSize)6 Color (java.awt.Color)6 Graphics2D (java.awt.Graphics2D)6 StateListDrawable (android.graphics.drawable.StateListDrawable)5