diff options
author | Chris Craik <ccraik@google.com> | 2014-03-20 12:44:20 -0700 |
---|---|---|
committer | Chris Craik <ccraik@google.com> | 2014-03-24 11:37:06 -0700 |
commit | b49f446c98096c4790a11d9b5bc83a4e585278c9 (patch) | |
tree | 04be3e4844f1f39df2b9f598d5e50d6e969e1080 /graphics | |
parent | ac6e97a5a69738a1d32794fc76ca639201639501 (diff) | |
download | frameworks_base-b49f446c98096c4790a11d9b5bc83a4e585278c9.zip frameworks_base-b49f446c98096c4790a11d9b5bc83a4e585278c9.tar.gz frameworks_base-b49f446c98096c4790a11d9b5bc83a4e585278c9.tar.bz2 |
Rework Outline API, remove isolatedZVolume remnants
Change-Id: I30c2fe832dcb98fa6329b1a595b3d3aafbdcad6b
Diffstat (limited to 'graphics')
-rw-r--r-- | graphics/java/android/graphics/Outline.java | 108 | ||||
-rw-r--r-- | graphics/java/android/graphics/drawable/Drawable.java | 19 | ||||
-rw-r--r-- | graphics/java/android/graphics/drawable/GradientDrawable.java | 51 |
3 files changed, 172 insertions, 6 deletions
diff --git a/graphics/java/android/graphics/Outline.java b/graphics/java/android/graphics/Outline.java new file mode 100644 index 0000000..a65ac87 --- /dev/null +++ b/graphics/java/android/graphics/Outline.java @@ -0,0 +1,108 @@ +/* + * Copyright (C) 2014 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.graphics; + +/** + * Defines an area of content. + * + * Can be used with a View or Drawable to drive the shape of shadows cast by a + * View, and allowing Views to clip inner content. + * + * @see View#setOutline(Outline) + * @see View#setClipToOutline(boolean) + */ +public class Outline { + /** @hide */ + public Rect mRect; + + /** @hide */ + public float mRadius; + + /** @hide */ + public Path mPath; + + /** + * Constructs an invalid Outline. Call one of the setter methods to make + * the outline valid for use with a View. + */ + public Outline() {} + + /** + * Returns whether the Outline is valid for use with a View. + * <p> + * Outlines are invalid when constructed until a setter method is called. + */ + public final boolean isValid() { + return mRect != null || mPath != null; + } + + /** + * @hide + */ + public final boolean canClip() { + return mPath == null; + } + + /** + * Replace the contents of this Outline with the contents of src. + */ + public void set(Outline src) { + if (src.mPath != null) { + if (mPath == null) { + mPath = new Path(); + } + mPath.set(src.mPath); + mRect = null; + } + if (src.mRect != null) { + if (mRect == null) { + mRect = new Rect(); + } + mRect.set(src.mRect); + } + mRadius = src.mRadius; + } + + /** + * Sets the Outline to the rounded rect defined by the input rect, and corner radius. + * <p> + * Outlines produced by this method support + * {@link View#setClipToOutline(boolean) View clipping.} + */ + public void setRoundRect(int left, int top, int right, int bottom, float radius) { + if (mRect == null) mRect = new Rect(); + mRect.set(left, top, right, bottom); + mRadius = radius; + mPath = null; + } + + /** + * Sets the Constructs an Outline from a {@link android.graphics.Path#isConvex() convex path}. + * + * @hide + */ + public void setConvexPath(Path convexPath) { + if (!convexPath.isConvex()) { + throw new IllegalArgumentException("path must be convex"); + } + if (mPath == null) mPath = new Path(); + + mRect = null; + mRadius = -1.0f; + mPath.set(convexPath); + } +} diff --git a/graphics/java/android/graphics/drawable/Drawable.java b/graphics/java/android/graphics/drawable/Drawable.java index 84211ef..eb6b536 100644 --- a/graphics/java/android/graphics/drawable/Drawable.java +++ b/graphics/java/android/graphics/drawable/Drawable.java @@ -30,6 +30,7 @@ import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.NinePatch; +import android.graphics.Outline; import android.graphics.PixelFormat; import android.graphics.PorterDuff; import android.graphics.PorterDuffColorFilter; @@ -860,6 +861,24 @@ public abstract class Drawable { } /** + * Returns the outline for this drawable if defined, null if not. + * <p> + * This method will be called by a View on its background Drawable after + * bounds change, if the View's Outline isn't set explicitly. This allows + * the background Drawable to provide the shape of the shadow casting + * portion of the View. It can also serve to clip the area of the View if + * if {@link View#setClipToOutline(boolean)} is set on the View. + * <p> + * The Outline queried by the View will not be modified, and is treated as + * a static shape that only needs to be requeried when the drawable's bounds + * change. + * + * @see View#setOutline(android.view.Outline) + * @see View#setClipToOutline(boolean) + */ + public Outline getOutline() { return null; } + + /** * Make this drawable mutable. This operation cannot be reversed. A mutable * drawable is guaranteed to not share its state with any other drawable. * This is especially useful when you need to modify properties of drawables diff --git a/graphics/java/android/graphics/drawable/GradientDrawable.java b/graphics/java/android/graphics/drawable/GradientDrawable.java index 46d57ad..8f22add 100644 --- a/graphics/java/android/graphics/drawable/GradientDrawable.java +++ b/graphics/java/android/graphics/drawable/GradientDrawable.java @@ -24,6 +24,7 @@ import android.graphics.Color; import android.graphics.ColorFilter; import android.graphics.DashPathEffect; import android.graphics.LinearGradient; +import android.graphics.Outline; import android.graphics.Paint; import android.graphics.Path; import android.graphics.PixelFormat; @@ -133,6 +134,7 @@ public class GradientDrawable extends Drawable { private final Path mPath = new Path(); private final RectF mRect = new RectF(); + private Outline mOutline; private Paint mLayerPaint; // internal, used if we use saveLayer() private boolean mRectIsDirty; // internal state @@ -585,11 +587,8 @@ public class GradientDrawable extends Drawable { // to show it. If we did nothing, Skia would clamp the rad // independently along each axis, giving us a thin ellipse // if the rect were very wide but not very tall - float rad = st.mRadius; - float r = Math.min(mRect.width(), mRect.height()) * 0.5f; - if (rad > r) { - rad = r; - } + float rad = Math.min(st.mRadius, + Math.min(mRect.width(), mRect.height()) * 0.5f); canvas.drawRoundRect(mRect, rad, rad, mFillPaint); if (haveStroke) { canvas.drawRoundRect(mRect, rad, rad, mStrokePaint); @@ -661,7 +660,7 @@ public class GradientDrawable extends Drawable { if (mRingPath == null) { mRingPath = new Path(); } else { - mRingPath.reset(); + mRingPath.reset(); } final Path ringPath = mRingPath; @@ -1243,6 +1242,46 @@ public class GradientDrawable extends Drawable { } @Override + public Outline getOutline() { + final GradientState st = mGradientState; + final Rect bounds = getBounds(); + + switch (st.mShape) { + case RECTANGLE: + if (st.mRadiusArray != null) { + return null; + } + float rad = 0; + if (st.mRadius > 0.0f) { + // clamp the radius based on width & height, matching behavior in draw() + rad = Math.min(st.mRadius, + Math.min(bounds.width(), bounds.height()) * 0.5f); + } + if (mOutline == null) { + mOutline = new Outline(); + } + mOutline.setRoundRect(bounds.left, bounds.top, + bounds.right, bounds.bottom, rad); + return mOutline; + case LINE: { + float halfStrokeWidth = mStrokePaint.getStrokeWidth() * 0.5f; + float centerY = bounds.centerY(); + int top = (int) Math.floor(centerY - halfStrokeWidth); + int bottom = (int) Math.ceil(centerY + halfStrokeWidth); + + if (mOutline == null) { + mOutline = new Outline(); + } + mOutline.setRoundRect(bounds.left, top, bounds.right, bottom, 0); + return mOutline; + } + default: + // TODO: investigate + return null; + } + } + + @Override public Drawable mutate() { if (!mMutated && super.mutate() == this) { mGradientState = new GradientState(mGradientState); |