Added VectorPainter API
Relnote: "Added VectorPainter API to
replace existing subcomposition API for
vector graphics. Result of subcomposition
is a VectorPainter object instead of a
DrawModifier. Deprecated previous DrawVector
composables in favor of VectorPainter.
Renamed Image(Painter) API to PaintBox(Painter)
Created Vector composable that behaves like the
Image composable except with a VectorAsset instead
of an ImageAsset"
Test: Created VectorTest
Bug: b/149030271
Change-Id: I9af9a365eb744e0cdb343cf424f4df5160d6c2b4
diff --git a/ui/ui-foundation/api/0.1.0-dev07.txt b/ui/ui-foundation/api/0.1.0-dev07.txt
index 69e28a1..b48068e 100644
--- a/ui/ui-foundation/api/0.1.0-dev07.txt
+++ b/ui/ui-foundation/api/0.1.0-dev07.txt
@@ -99,7 +99,8 @@
}
public final class ImageKt {
- method public static void Image(androidx.ui.graphics.ImageAsset image, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.ImageAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.vector.VectorAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method public static void Image(androidx.ui.graphics.painter.Painter painter, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method @Deprecated public static void SimpleImage(androidx.ui.graphics.ImageAsset image, androidx.ui.graphics.Color? tint = null);
}
diff --git a/ui/ui-foundation/api/current.txt b/ui/ui-foundation/api/current.txt
index 69e28a1..b48068e 100644
--- a/ui/ui-foundation/api/current.txt
+++ b/ui/ui-foundation/api/current.txt
@@ -99,7 +99,8 @@
}
public final class ImageKt {
- method public static void Image(androidx.ui.graphics.ImageAsset image, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.ImageAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.vector.VectorAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method public static void Image(androidx.ui.graphics.painter.Painter painter, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method @Deprecated public static void SimpleImage(androidx.ui.graphics.ImageAsset image, androidx.ui.graphics.Color? tint = null);
}
diff --git a/ui/ui-foundation/api/public_plus_experimental_0.1.0-dev07.txt b/ui/ui-foundation/api/public_plus_experimental_0.1.0-dev07.txt
index 69e28a1..b48068e 100644
--- a/ui/ui-foundation/api/public_plus_experimental_0.1.0-dev07.txt
+++ b/ui/ui-foundation/api/public_plus_experimental_0.1.0-dev07.txt
@@ -99,7 +99,8 @@
}
public final class ImageKt {
- method public static void Image(androidx.ui.graphics.ImageAsset image, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.ImageAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.vector.VectorAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method public static void Image(androidx.ui.graphics.painter.Painter painter, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method @Deprecated public static void SimpleImage(androidx.ui.graphics.ImageAsset image, androidx.ui.graphics.Color? tint = null);
}
diff --git a/ui/ui-foundation/api/public_plus_experimental_current.txt b/ui/ui-foundation/api/public_plus_experimental_current.txt
index 69e28a1..b48068e 100644
--- a/ui/ui-foundation/api/public_plus_experimental_current.txt
+++ b/ui/ui-foundation/api/public_plus_experimental_current.txt
@@ -99,7 +99,8 @@
}
public final class ImageKt {
- method public static void Image(androidx.ui.graphics.ImageAsset image, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.ImageAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.vector.VectorAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method public static void Image(androidx.ui.graphics.painter.Painter painter, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method @Deprecated public static void SimpleImage(androidx.ui.graphics.ImageAsset image, androidx.ui.graphics.Color? tint = null);
}
diff --git a/ui/ui-foundation/api/restricted_0.1.0-dev07.txt b/ui/ui-foundation/api/restricted_0.1.0-dev07.txt
index 69e28a1..b48068e 100644
--- a/ui/ui-foundation/api/restricted_0.1.0-dev07.txt
+++ b/ui/ui-foundation/api/restricted_0.1.0-dev07.txt
@@ -99,7 +99,8 @@
}
public final class ImageKt {
- method public static void Image(androidx.ui.graphics.ImageAsset image, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.ImageAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.vector.VectorAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method public static void Image(androidx.ui.graphics.painter.Painter painter, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method @Deprecated public static void SimpleImage(androidx.ui.graphics.ImageAsset image, androidx.ui.graphics.Color? tint = null);
}
diff --git a/ui/ui-foundation/api/restricted_current.txt b/ui/ui-foundation/api/restricted_current.txt
index 69e28a1..b48068e 100644
--- a/ui/ui-foundation/api/restricted_current.txt
+++ b/ui/ui-foundation/api/restricted_current.txt
@@ -99,7 +99,8 @@
}
public final class ImageKt {
- method public static void Image(androidx.ui.graphics.ImageAsset image, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.ImageAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
+ method public static inline void Image(androidx.ui.graphics.vector.VectorAsset asset, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method public static void Image(androidx.ui.graphics.painter.Painter painter, androidx.ui.core.Modifier modifier = Modifier.None, androidx.ui.core.Alignment alignment = Alignment.Center, androidx.ui.graphics.ScaleFit scaleFit = ScaleFit.Fit, float alpha = 1.0f, androidx.ui.graphics.ColorFilter? colorFilter = null);
method @Deprecated public static void SimpleImage(androidx.ui.graphics.ImageAsset image, androidx.ui.graphics.Color? tint = null);
}
diff --git a/ui/ui-foundation/samples/src/main/java/androidx/ui/foundation/samples/ImageSamples.kt b/ui/ui-foundation/samples/src/main/java/androidx/ui/foundation/samples/ImageSamples.kt
index 5dbd5c9..929b7f2 100644
--- a/ui/ui-foundation/samples/src/main/java/androidx/ui/foundation/samples/ImageSamples.kt
+++ b/ui/ui-foundation/samples/src/main/java/androidx/ui/foundation/samples/ImageSamples.kt
@@ -18,19 +18,66 @@
import androidx.annotation.Sampled
import androidx.compose.Composable
+import androidx.compose.remember
import androidx.ui.foundation.Image
import androidx.ui.geometry.Offset
+import androidx.ui.geometry.Rect
import androidx.ui.graphics.Canvas
import androidx.ui.graphics.Color
+import androidx.ui.graphics.ColorFilter
import androidx.ui.graphics.ImageAsset
import androidx.ui.graphics.Paint
+import androidx.ui.graphics.ScaleFit
+import androidx.ui.graphics.painter.Painter
+import androidx.ui.layout.LayoutSize
+import androidx.ui.res.loadVectorResource
+import androidx.ui.unit.PxSize
+import androidx.ui.unit.dp
+import androidx.ui.unit.px
@Sampled
@Composable
fun ImageSample() {
val imageAsset = createTestImage()
// Lays out and draws an image sized to the dimensions of the ImageAsset
- Image(image = imageAsset)
+ Image(asset = imageAsset)
+}
+
+@Sampled
+@Composable
+fun ImageVectorAssetSample() {
+ val vectorAsset = loadVectorResource(R.drawable.ic_sample_vector)
+ vectorAsset.resource.resource?.let {
+ Image(
+ asset = it,
+ modifier = LayoutSize(200.dp, 200.dp),
+ scaleFit = ScaleFit.FillMinDimension,
+ colorFilter = ColorFilter.tint(Color.Cyan)
+ )
+ }
+}
+
+@Sampled
+@Composable
+fun ImagePainterSample() {
+ val customPainter = remember {
+ object : Painter() {
+
+ val paint = Paint().apply { this.color = Color.Cyan }
+
+ override val intrinsicSize: PxSize
+ get() = PxSize(100.px, 100.px)
+
+ override fun onDraw(canvas: Canvas, bounds: PxSize) {
+ canvas.drawRect(
+ Rect.fromLTWH(0.0f, 0.0f, bounds.width.value, bounds.height.value),
+ paint
+ )
+ }
+ }
+ }
+
+ Image(painter = customPainter, modifier = LayoutSize(100.dp, 100.dp))
}
/**
diff --git a/ui/ui-foundation/samples/src/main/res/drawable/ic_sample_vector.xml b/ui/ui-foundation/samples/src/main/res/drawable/ic_sample_vector.xml
new file mode 100644
index 0000000..d49a476
--- /dev/null
+++ b/ui/ui-foundation/samples/src/main/res/drawable/ic_sample_vector.xml
@@ -0,0 +1,25 @@
+<!--
+ Copyright 2020 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.
+ -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp"
+ android:height="24dp"
+ android:viewportWidth="24.0"
+ android:viewportHeight="24.0">
+ <path
+ android:fillColor="#000000"
+ android:pathData="M19,13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
+</vector>
diff --git a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ImageTest.kt b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ImageTest.kt
index 5cbfb60..62ac258 100644
--- a/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ImageTest.kt
+++ b/ui/ui-foundation/src/androidTest/java/androidx/ui/foundation/ImageTest.kt
@@ -22,6 +22,7 @@
import androidx.ui.core.Alignment
import androidx.ui.core.DensityAmbient
import androidx.ui.core.TestTag
+import androidx.ui.foundation.test.R
import androidx.ui.test.createComposeRule
import androidx.ui.geometry.Rect
import androidx.ui.graphics.Canvas
@@ -30,10 +31,10 @@
import androidx.ui.graphics.Paint
import androidx.ui.graphics.Path
import androidx.ui.graphics.ScaleFit
-import androidx.ui.graphics.painter.ColorPainter
import androidx.ui.graphics.toArgb
import androidx.ui.layout.LayoutAlign
import androidx.ui.layout.LayoutSize
+import androidx.ui.res.loadVectorResource
import androidx.ui.test.captureToBitmap
import androidx.ui.test.findByTag
import androidx.ui.unit.dp
@@ -92,7 +93,7 @@
val size = (containerSize / DensityAmbient.current.density).dp
Box(modifier = LayoutSize(size) + DrawBackground(Color.White) + LayoutAlign.Center) {
TestTag(contentTag) {
- Image(image = createImageAsset())
+ Image(asset = createImageAsset())
}
}
}
@@ -125,7 +126,7 @@
// The resultant Image composable should be twice the size of the underlying
// ImageAsset that is to be drawn and will stretch the content to fit
// the bounds
- Image(image = createImageAsset(),
+ Image(asset = createImageAsset(),
modifier = LayoutSize(
(imageComposableWidth / density).dp,
(imageComposableHeight / density).dp
@@ -163,7 +164,7 @@
TestTag(contentTag) {
// The resultant Image composable should be twice the size of the underlying
// ImageAsset that is to be drawn in the bottom end section of the composable
- Image(image = createImageAsset(),
+ Image(asset = createImageAsset(),
modifier = LayoutSize(
(imageComposableWidth / density).dp,
(imageComposableHeight / density).dp
@@ -192,20 +193,23 @@
}
@Test
- fun testImageMinSizeCentered() {
+ fun testVectorScaledCentered() {
+ val boxWidth = 240
+ val boxHeight = 240
rule.setContent {
val density = DensityAmbient.current.density
- val size = (containerSize * 2 / density).dp
- val minWidth = (imageWidth / density).dp
- val minHeight = (imageHeight / density).dp
+ val size = (boxWidth * 2 / density).dp
+ val minWidth = (boxWidth / density).dp
+ val minHeight = (boxHeight / density).dp
Box(modifier = LayoutSize(size) + DrawBackground(Color.White) + LayoutAlign.Center) {
TestTag(contentTag) {
- // The resultant Image composable should be sized to the minimum values here
- // as [ColorPainter] has no intrinsic width or height
- Image(painter = ColorPainter(Color.Red),
- modifier = LayoutSize.Min(minWidth, minHeight),
- alignment = Alignment.Center
- )
+ loadVectorResource(R.drawable.ic_vector_asset_test).resource.resource?.let {
+ Image(
+ it,
+ modifier = LayoutSize.Min(minWidth, minHeight),
+ scaleFit = ScaleFit.FillMinDimension
+ )
+ }
}
}
}
@@ -213,23 +217,23 @@
val imageColor = Color.Red.toArgb()
val containerBgColor = Color.White.toArgb()
findByTag(contentTag).captureToBitmap().apply {
- val imageStartX = width / 2 - imageWidth / 2
- val imageStartY = height / 2 - imageHeight / 2
+ val imageStartX = width / 2 - boxWidth / 2
+ val imageStartY = height / 2 - boxHeight / 2
Assert.assertEquals(containerBgColor, getPixel(imageStartX - 1, imageStartY - 1))
- Assert.assertEquals(containerBgColor, getPixel(imageStartX + imageWidth + 1,
+ Assert.assertEquals(containerBgColor, getPixel(imageStartX + boxWidth + 1,
imageStartY - 1))
- Assert.assertEquals(containerBgColor, getPixel(imageStartX + imageWidth + 1,
- imageStartY + imageHeight + 1))
+ Assert.assertEquals(containerBgColor, getPixel(imageStartX + boxWidth + 1,
+ imageStartY + boxHeight + 1))
Assert.assertEquals(containerBgColor, getPixel(imageStartX - 1, imageStartY +
- imageHeight + 1))
+ boxHeight + 1))
- Assert.assertEquals(imageColor, getPixel(imageStartX, imageStartY))
- Assert.assertEquals(imageColor, getPixel(imageStartX + imageWidth - 1,
- imageStartY))
- Assert.assertEquals(imageColor, getPixel(imageStartX + imageWidth - 1,
- imageStartY + imageHeight - 1))
+ Assert.assertEquals(imageColor, getPixel(imageStartX, imageStartY + 15))
+ Assert.assertEquals(containerBgColor, getPixel(imageStartX + boxWidth - 2,
+ imageStartY - 1))
+ Assert.assertEquals(imageColor, getPixel(imageStartX + boxWidth - 10,
+ imageStartY + boxHeight - 2))
Assert.assertEquals(imageColor, getPixel(imageStartX, imageStartY +
- imageHeight - 1))
+ boxHeight - 2))
}
}
}
\ No newline at end of file
diff --git a/ui/ui-foundation/src/androidTest/res/drawable/ic_vector_asset_test.xml b/ui/ui-foundation/src/androidTest/res/drawable/ic_vector_asset_test.xml
new file mode 100644
index 0000000..bbe937e
--- /dev/null
+++ b/ui/ui-foundation/src/androidTest/res/drawable/ic_vector_asset_test.xml
@@ -0,0 +1,25 @@
+<!--
+ Copyright 2020 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.
+ -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+ android:width="24dp"
+ android:height="24dp"
+ android:viewportWidth="24"
+ android:viewportHeight="24">
+ <path
+ android:fillColor="#FF0000"
+ android:pathData="L24,24,0,24z"/>
+</vector>
diff --git a/ui/ui-foundation/src/main/java/androidx/ui/foundation/Icon.kt b/ui/ui-foundation/src/main/java/androidx/ui/foundation/Icon.kt
index aa50260..af39c13 100644
--- a/ui/ui-foundation/src/main/java/androidx/ui/foundation/Icon.kt
+++ b/ui/ui-foundation/src/main/java/androidx/ui/foundation/Icon.kt
@@ -25,7 +25,7 @@
import androidx.ui.graphics.ColorFilter
import androidx.ui.graphics.painter.Painter
import androidx.ui.graphics.vector.VectorAsset
-import androidx.ui.graphics.vector.drawVector
+import androidx.ui.graphics.vector.VectorPainter
import androidx.ui.layout.LayoutHeight
import androidx.ui.layout.LayoutSize
import androidx.ui.layout.LayoutWidth
@@ -52,7 +52,7 @@
// TODO: b/149735981 semantics for content description
Box(
modifier = modifier + LayoutWidth(icon.defaultWidth) + LayoutHeight(icon.defaultHeight) +
- drawVector(vectorImage = icon, tintColor = tint)
+ VectorPainter(icon).asModifier(colorFilter = ColorFilter.tint(tint))
) {
}
}
diff --git a/ui/ui-foundation/src/main/java/androidx/ui/foundation/Image.kt b/ui/ui-foundation/src/main/java/androidx/ui/foundation/Image.kt
index 3801812..d50d21e 100644
--- a/ui/ui-foundation/src/main/java/androidx/ui/foundation/Image.kt
+++ b/ui/ui-foundation/src/main/java/androidx/ui/foundation/Image.kt
@@ -20,11 +20,10 @@
import androidx.compose.remember
import androidx.ui.core.Alignment
import androidx.ui.core.DensityAmbient
-import androidx.ui.core.DrawModifier
+import androidx.ui.core.DrawClipToBounds
import androidx.ui.core.Modifier
import androidx.ui.core.asModifier
import androidx.ui.graphics.BlendMode
-import androidx.ui.graphics.Canvas
import androidx.ui.graphics.Color
import androidx.ui.graphics.ColorFilter
import androidx.ui.graphics.DefaultAlpha
@@ -33,10 +32,9 @@
import androidx.ui.graphics.painter.ColorPainter
import androidx.ui.graphics.painter.ImagePainter
import androidx.ui.graphics.painter.Painter
+import androidx.ui.graphics.vector.VectorAsset
+import androidx.ui.graphics.vector.VectorPainter
import androidx.ui.layout.LayoutSize
-import androidx.ui.unit.Density
-import androidx.ui.unit.PxSize
-import androidx.ui.unit.toRect
/**
* A composable that lays out and draws a given [ImageAsset]. This will attempt to
@@ -47,7 +45,7 @@
*
* @sample androidx.ui.foundation.samples.ImageSample
*
- * @param image The [ImageAsset] to draw.
+ * @param asset The [ImageAsset] to draw.
* @param modifier Modifier used to adjust the layout algorithm or draw decoration content (ex.
* background)
* @param alignment Optional alignment parameter used to place the [ImageAsset] in the given
@@ -58,18 +56,17 @@
* @param colorFilter Optional ColorFilter to apply for the [ImageAsset] when it is rendered
* onscreen
*/
+@Suppress("NOTHING_TO_INLINE")
@Composable
-fun Image(
- image: ImageAsset,
+inline fun Image(
+ asset: ImageAsset,
modifier: Modifier = Modifier.None,
alignment: Alignment = Alignment.Center,
scaleFit: ScaleFit = ScaleFit.Fit,
alpha: Float = DefaultAlpha,
colorFilter: ColorFilter? = null
) {
- val imagePainter = remember(image) { ImagePainter(image) }
- // Min width/height are intentionally not provided in this call as they are consumed
- // from the ImagePainter directly
+ val imagePainter = remember(asset) { ImagePainter(asset) }
Image(
painter = imagePainter,
modifier = modifier,
@@ -81,6 +78,44 @@
}
/**
+ * A composable that lays out and draws a given [VectorAsset]. This will attempt to
+ * size the composable according to the [VectorAsset]'s given width and height. However, an
+ * optional [Modifier] parameter can be provided to adjust sizing or draw additional content (ex.
+ * background). Any unspecified dimension will leverage the [VectorAsset]'s size as a minimum
+ * constraint.
+ *
+ * @sample androidx.ui.foundation.samples.ImageVectorAssetSample
+ *
+ * @param asset The [VectorAsset] to draw.
+ * @param modifier Modifier used to adjust the layout algorithm or draw decoration content (ex.
+ * background)
+ * @param alignment Optional alignment parameter used to place the [VectorAsset] in the given
+ * bounds defined by the width and height.
+ * @param scaleFit Optional scale parameter used to determine the aspect ratio scaling to be used
+ * if the bounds are a different size from the intrinsic size of the [VectorAsset].
+ * @param alpha Optional opacity to be applied to the [VectorAsset] when it is rendered onscreen
+ * @param colorFilter Optional ColorFilter to apply for the [VectorAsset] when it is rendered
+ * onscreen
+ */
+@Suppress("NOTHING_TO_INLINE")
+@Composable
+inline fun Image(
+ asset: VectorAsset,
+ modifier: Modifier = Modifier.None,
+ alignment: Alignment = Alignment.Center,
+ scaleFit: ScaleFit = ScaleFit.Fit,
+ alpha: Float = DefaultAlpha,
+ colorFilter: ColorFilter? = null
+) = Image(
+ painter = VectorPainter(asset),
+ modifier = modifier,
+ alignment = alignment,
+ scaleFit = scaleFit,
+ alpha = alpha,
+ colorFilter = colorFilter
+)
+
+/**
* Creates a composable that lays out and draws a given [Painter]. This will attempt to size
* the composable according to the [Painter]'s intrinsic size. However, an optional [Modifier]
* parameter can be provided to adjust sizing or draw additional content (ex. background)
@@ -90,6 +125,8 @@
* of zero and will not draw any content. This can happen for Painter implementations that
* always attempt to fill the bounds like [ColorPainter]
*
+ * @sample androidx.ui.foundation.samples.ImagePainterSample
+ *
* @param painter to draw
* @param modifier Modifier used to adjust the layout algorithm or draw decoration content (ex.
* background)
@@ -117,7 +154,7 @@
colorFilter = colorFilter
)
- Box(modifier + ClipModifier + painterModifier)
+ Box(modifier + DrawClipToBounds + painterModifier)
}
/**
@@ -148,17 +185,10 @@
scaleFit = ScaleFit.FillMaxDimension,
colorFilter = tint?.let { ColorFilter(it, BlendMode.srcIn) }
)
- Box(LayoutSize(image.width.toDp(), image.height.toDp()) + ClipModifier + imageModifier)
- }
-}
-
-// TODO(mount, malkov) : remove when RepaintBoundary is a modifier: b/149982905
-// This is class and not val because if b/149985596
-private object ClipModifier : DrawModifier {
- override fun draw(density: Density, drawContent: () -> Unit, canvas: Canvas, size: PxSize) {
- canvas.save()
- canvas.clipRect(size.toRect())
- drawContent()
- canvas.restore()
+ Box(
+ LayoutSize(image.width.toDp(), image.height.toDp()) +
+ DrawClipToBounds +
+ imageModifier
+ )
}
}
\ No newline at end of file