diff options
author | Diego Perez <diegoperez@google.com> | 2015-05-07 03:02:34 +0000 |
---|---|---|
committer | Android Git Automerger <android-git-automerger@android.com> | 2015-05-07 03:02:34 +0000 |
commit | 0fe14486211e3ade7f29ea76e16211853fe9cd7a (patch) | |
tree | 831f0dbe76c656729374afd41e20b1b00c818e15 | |
parent | c2ba7391704cd4025c890b37148c16bf32b55a93 (diff) | |
parent | 2021c8773150dd47d8781375af94143cb22afd9d (diff) | |
download | frameworks_base-0fe14486211e3ade7f29ea76e16211853fe9cd7a.zip frameworks_base-0fe14486211e3ade7f29ea76e16211853fe9cd7a.tar.gz frameworks_base-0fe14486211e3ade7f29ea76e16211853fe9cd7a.tar.bz2 |
am 2021c877: am 33e54563: am 4b125836: Merge "Added function to render a drawable in all available states" into lmp-mr1-dev
* commit '2021c8773150dd47d8781375af94143cb22afd9d':
Added function to render a drawable in all available states
-rw-r--r-- | tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java | 2 | ||||
-rw-r--r-- | tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderDrawable.java | 60 |
2 files changed, 56 insertions, 6 deletions
diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java index 4d2c2fc..c6d60f8 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java @@ -181,7 +181,7 @@ public final class Bridge extends com.android.ide.common.rendering.api.Bridge { */ private static LayoutLog sCurrentLog = sDefaultLog; - private static final int LAST_SUPPORTED_FEATURE = Features.PREFERENCES_RENDERING; + private static final int LAST_SUPPORTED_FEATURE = Features.RENDER_ALL_DRAWABLE_STATES; @Override public int getApiLevel() { diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderDrawable.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderDrawable.java index 6513c5f..9e26502 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderDrawable.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderDrawable.java @@ -22,12 +22,14 @@ import com.android.ide.common.rendering.api.ResourceValue; import com.android.ide.common.rendering.api.Result; import com.android.ide.common.rendering.api.Result.Status; import com.android.layoutlib.bridge.android.BridgeContext; +import com.android.layoutlib.bridge.android.RenderParamsFlags; import com.android.resources.ResourceType; import android.graphics.Bitmap; import android.graphics.Bitmap_Delegate; import android.graphics.Canvas; import android.graphics.drawable.Drawable; +import android.graphics.drawable.StateListDrawable; import android.view.AttachInfo_Accessor; import android.view.View.MeasureSpec; import android.widget.FrameLayout; @@ -37,6 +39,10 @@ import java.awt.Color; import java.awt.Graphics2D; import java.awt.image.BufferedImage; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + /** * Action to render a given Drawable provided through {@link DrawableParams#getDrawable()}. * @@ -68,11 +74,37 @@ public class RenderDrawable extends RenderAction<DrawableParams> { 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); + } + } + + private BufferedImage renderImage(HardwareConfig hardwareConfig, Drawable d, + BridgeContext context) { // create a simple FrameLayout FrameLayout content = new FrameLayout(context); // get the actual Drawable object to draw - Drawable d = ResourceHelper.getDrawable(drawableResource, context); content.setBackground(d); // set the AttachInfo on the root view. @@ -80,8 +112,27 @@ public class RenderDrawable extends RenderAction<DrawableParams> { // measure - int w = hardwareConfig.getScreenWidth(); - int h = hardwareConfig.getScreenHeight(); + int w = d.getIntrinsicWidth(); + int h = d.getIntrinsicHeight(); + + final int screenWidth = hardwareConfig.getScreenWidth(); + final int screenHeight = hardwareConfig.getScreenHeight(); + + if (w == -1 || h == -1) { + // Use screen size when either intrinsic width or height isn't available + w = screenWidth; + h = screenHeight; + } else if (w > screenWidth || h > screenHeight) { + // If image wouldn't fit to the screen, resize it to avoid cropping. + + // We need to find scale such that scale * w <= screenWidth, scale * h <= screenHeight + double scale = Math.min((double) screenWidth / w, (double) screenHeight / h); + + // scale * w / scale * h = w / h, so, proportions are preserved. + w = (int) Math.floor(scale * w); + h = (int) Math.floor(scale * h); + } + int w_spec = MeasureSpec.makeMeasureSpec(w, MeasureSpec.EXACTLY); int h_spec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY); content.measure(w_spec, h_spec); @@ -105,8 +156,7 @@ public class RenderDrawable extends RenderAction<DrawableParams> { // and draw content.draw(canvas); - - return Status.SUCCESS.createResult(image); + return image; } protected BufferedImage getImage(int w, int h) { |