OSDN Git Service

Added function to render a drawable in all available states
authorAndrew Shulaev <ddrone@google.com>
Thu, 5 Mar 2015 11:51:38 +0000 (11:51 +0000)
committerAndrew Shulaev <ddrone@google.com>
Thu, 5 Mar 2015 11:51:38 +0000 (11:51 +0000)
This reverts commit 01cdf8b135be3bf67b9386dc929109c3db82c730.

Change-Id: I596855047d78c3c718744ad61432fc45e1239634

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/Bridge.java
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/RenderParamsFlags.java [moved from tools/layoutlib/bridge/src/com/android/layoutlib/bridge/android/SessionParamsFlags.java with 58% similarity]
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderDrawable.java
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/RenderSessionImpl.java

index 4d2c2fc..c6d60f8 100644 (file)
@@ -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() {
 
 package com.android.layoutlib.bridge.android;
 
-import com.android.ide.common.rendering.api.SessionParams;
+import com.android.ide.common.rendering.api.SessionParams.Key;
 
 /**
- * This contains all known keys for the {@link SessionParams#getFlag(SessionParams.Key)}.
+ * This contains all known keys for the {@link RenderParams#getFlag(Key)}.
  * <p/>
  * The IDE has its own copy of this class which may be newer or older than this one.
  * <p/>
  * Constants should never be modified or removed from this class.
  */
-public final class SessionParamsFlags {
+public final class RenderParamsFlags {
 
-    public static final SessionParams.Key<String> FLAG_KEY_ROOT_TAG =
-            new SessionParams.Key<String>("rootTag", String.class);
-    public static final SessionParams.Key<Boolean> FLAG_KEY_DISABLE_BITMAP_CACHING =
-            new SessionParams.Key<Boolean>("disableBitmapCaching", Boolean.class);
+    public static final Key<String> FLAG_KEY_ROOT_TAG =
+            new Key<String>("rootTag", String.class);
+    public static final Key<Boolean> FLAG_KEY_DISABLE_BITMAP_CACHING =
+            new Key<Boolean>("disableBitmapCaching", Boolean.class);
+    public static final Key<Boolean> FLAG_KEY_RENDER_ALL_DRAWABLE_STATES =
+            new Key<Boolean>("renderAllDrawableStates", Boolean.class);
 
     // Disallow instances.
-    private SessionParamsFlags() {}
+    private RenderParamsFlags() {}
 }
index 669e6b5..3dee1e2 100644 (file)
 
 package com.android.layoutlib.bridge.impl;
 
-import static com.android.ide.common.rendering.api.Result.Status.ERROR_UNKNOWN;
-
 import com.android.ide.common.rendering.api.DrawableParams;
 import com.android.ide.common.rendering.api.HardwareConfig;
 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;
@@ -38,7 +38,9 @@ import java.awt.AlphaComposite;
 import java.awt.Color;
 import java.awt.Graphics2D;
 import java.awt.image.BufferedImage;
-import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
 
 /**
  * Action to render a given Drawable provided through {@link DrawableParams#getDrawable()}.
@@ -71,11 +73,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.
@@ -83,8 +111,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);
@@ -108,8 +155,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) {
index 875cc87..607563a 100644 (file)
@@ -50,7 +50,7 @@ import com.android.layoutlib.bridge.Bridge;
 import com.android.layoutlib.bridge.android.BridgeContext;
 import com.android.layoutlib.bridge.android.BridgeLayoutParamsMapAttributes;
 import com.android.layoutlib.bridge.android.BridgeXmlBlockParser;
-import com.android.layoutlib.bridge.android.SessionParamsFlags;
+import com.android.layoutlib.bridge.android.RenderParamsFlags;
 import com.android.layoutlib.bridge.bars.BridgeActionBar;
 import com.android.layoutlib.bridge.bars.AppCompatActionBar;
 import com.android.layoutlib.bridge.bars.Config;
@@ -400,7 +400,7 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
             // it can instantiate the custom Fragment.
             Fragment_Delegate.setProjectCallback(params.getProjectCallback());
 
-            String rootTag = params.getFlag(SessionParamsFlags.FLAG_KEY_ROOT_TAG);
+            String rootTag = params.getFlag(RenderParamsFlags.FLAG_KEY_ROOT_TAG);
             boolean isPreference = "PreferenceScreen".equals(rootTag);
             View view;
             if (isPreference) {
@@ -557,7 +557,7 @@ public class RenderSessionImpl extends RenderAction<SessionParams> {
                 // This is useful when mImage is just a wrapper of Graphics2D so
                 // it doesn't get cached.
                 boolean disableBitmapCaching = Boolean.TRUE.equals(params.getFlag(
-                    SessionParamsFlags.FLAG_KEY_DISABLE_BITMAP_CACHING));
+                    RenderParamsFlags.FLAG_KEY_DISABLE_BITMAP_CACHING));
                 if (newRenderSize || mCanvas == null || disableBitmapCaching) {
                     if (params.getImageFactory() != null) {
                         mImage = params.getImageFactory().getImage(