1616
1717package androidx.compose.ui.graphics
1818
19+ import android.graphics.Paint.Cap
20+ import android.graphics.Paint.Style
1921import android.graphics.PorterDuffXfermode
2022import android.os.Build
2123import androidx.annotation.RequiresApi
2224
25+ @Deprecated(
26+ message = " Use android.graphics.Paint directly instead" ,
27+ replaceWith = ReplaceWith (" android.graphics.Paint" ),
28+ )
2329actual typealias NativePaint = android.graphics.Paint
2430
25- actual fun Paint (): Paint = AndroidPaint ()
31+ actual fun Paint (): Paint =
32+ @Suppress(" DEPRECATION" ) // Valid usage of AndroidPaint
33+ AndroidPaint ()
2634
2735/* * Convert an [android.graphics.Paint] instance into a Compose-compatible Paint */
28- fun android.graphics.Paint.asComposePaint (): Paint = AndroidPaint (this )
36+ fun android.graphics.Paint.asComposePaint (): Paint =
37+ @Suppress(" DEPRECATION" ) // Valid usage of AndroidPaint
38+ AndroidPaint (this )
39+
40+ fun Paint.asAndroidPaint (): android.graphics.Paint =
41+ @Suppress(" DEPRECATION" ) // Valid usage of AndroidPaint
42+ (this as AndroidPaint ).internalPaint
2943
3044/* *
3145 * Create a Compose [Paint] instance backed by an [android.graphics.Paint] object to be consumed by
3246 * Compose applications running on the Android platform
3347 *
3448 * @param internalPaint [android.graphics.Paint] to be wrapped by the [AndroidPaint] instance
3549 */
36- class AndroidPaint (private var internalPaint : android.graphics.Paint ) : Paint {
50+ @Deprecated(
51+ message = " This type is not supposed to be used directly" ,
52+ replaceWith = ReplaceWith (" androidx.compose.ui.graphics.Paint" ),
53+ )
54+ class AndroidPaint (internal var internalPaint : android.graphics.Paint ) : Paint {
3755
3856 /* * Create a new [AndroidPaint] instance backed by a newly created [android.graphics.Paint] */
3957 constructor () : this (makeNativePaint())
@@ -42,7 +60,11 @@ class AndroidPaint(private var internalPaint: android.graphics.Paint) : Paint {
4260 private var internalShader: Shader ? = null
4361 private var internalColorFilter: ColorFilter ? = null
4462
45- override fun asFrameworkPaint (): NativePaint = internalPaint
63+ @Deprecated(
64+ message = " Use asAndroidPaint() extension instead" ,
65+ replaceWith = ReplaceWith (" asAndroidPaint()" ),
66+ )
67+ override fun asFrameworkPaint (): android.graphics.Paint = internalPaint
4668
4769 override var alpha: Float
4870 get() = internalPaint.getNativeAlpha()
@@ -136,7 +158,7 @@ internal fun makeNativePaint() =
136158 android.graphics.Paint .FILTER_BITMAP_FLAG
137159 )
138160
139- internal fun NativePaint .setNativeBlendMode (mode : BlendMode ) {
161+ internal fun android.graphics.Paint .setNativeBlendMode (mode : BlendMode ) {
140162 if (Build .VERSION .SDK_INT >= 29 ) {
141163 // All blend modes supported in Q
142164 WrapperVerificationHelperMethods .setBlendMode(this , mode)
@@ -146,76 +168,76 @@ internal fun NativePaint.setNativeBlendMode(mode: BlendMode) {
146168 }
147169}
148170
149- internal fun NativePaint .setNativeColorFilter (value : ColorFilter ? ) {
171+ internal fun android.graphics.Paint .setNativeColorFilter (value : ColorFilter ? ) {
150172 colorFilter = value?.asAndroidColorFilter()
151173}
152174
153- internal fun NativePaint .getNativeAlpha () = this .alpha / 255f
175+ internal fun android.graphics.Paint .getNativeAlpha () = this .alpha / 255f
154176
155- internal fun NativePaint .setNativeAlpha (value : Float ) {
177+ internal fun android.graphics.Paint .setNativeAlpha (value : Float ) {
156178 this .alpha = kotlin.math.round(value * 255.0f ).toInt()
157179}
158180
159- internal fun NativePaint .getNativeAntiAlias (): Boolean = this .isAntiAlias
181+ internal fun android.graphics.Paint .getNativeAntiAlias (): Boolean = this .isAntiAlias
160182
161- internal fun NativePaint .setNativeAntiAlias (value : Boolean ) {
183+ internal fun android.graphics.Paint .setNativeAntiAlias (value : Boolean ) {
162184 this .isAntiAlias = value
163185}
164186
165- internal fun NativePaint .getNativeColor (): Color = Color (this .color)
187+ internal fun android.graphics.Paint .getNativeColor (): Color = Color (this .color)
166188
167- internal fun NativePaint .setNativeColor (value : Color ) {
189+ internal fun android.graphics.Paint .setNativeColor (value : Color ) {
168190 this .color = value.toArgb()
169191}
170192
171- internal fun NativePaint .setNativeStyle (value : PaintingStyle ) {
193+ internal fun android.graphics.Paint .setNativeStyle (value : PaintingStyle ) {
172194 // TODO(njawad): Platform also supports Paint.Style.FILL_AND_STROKE)
173195 this .style =
174196 when (value) {
175- PaintingStyle .Stroke -> android.graphics. Paint . Style .STROKE
176- else -> android.graphics. Paint . Style .FILL
197+ PaintingStyle .Stroke -> Style .STROKE
198+ else -> Style .FILL
177199 }
178200}
179201
180- internal fun NativePaint .getNativeStyle () =
202+ internal fun android.graphics.Paint .getNativeStyle () =
181203 when (this .style) {
182- android.graphics. Paint . Style .STROKE -> PaintingStyle .Stroke
204+ Style .STROKE -> PaintingStyle .Stroke
183205 else -> PaintingStyle .Fill
184206 }
185207
186- internal fun NativePaint .getNativeStrokeWidth (): Float = this .strokeWidth
208+ internal fun android.graphics.Paint .getNativeStrokeWidth (): Float = this .strokeWidth
187209
188- internal fun NativePaint .setNativeStrokeWidth (value : Float ) {
210+ internal fun android.graphics.Paint .setNativeStrokeWidth (value : Float ) {
189211 this .strokeWidth = value
190212}
191213
192- internal fun NativePaint .getNativeStrokeCap (): StrokeCap =
214+ internal fun android.graphics.Paint .getNativeStrokeCap (): StrokeCap =
193215 when (this .strokeCap) {
194- android.graphics. Paint . Cap .BUTT -> StrokeCap .Butt
195- android.graphics. Paint . Cap .ROUND -> StrokeCap .Round
196- android.graphics. Paint . Cap .SQUARE -> StrokeCap .Square
216+ Cap .BUTT -> StrokeCap .Butt
217+ Cap .ROUND -> StrokeCap .Round
218+ Cap .SQUARE -> StrokeCap .Square
197219 else -> StrokeCap .Butt
198220 }
199221
200- internal fun NativePaint .setNativeStrokeCap (value : StrokeCap ) {
222+ internal fun android.graphics.Paint .setNativeStrokeCap (value : StrokeCap ) {
201223 this .strokeCap =
202224 when (value) {
203- StrokeCap .Square -> android.graphics. Paint . Cap .SQUARE
204- StrokeCap .Round -> android.graphics. Paint . Cap .ROUND
205- StrokeCap .Butt -> android.graphics. Paint . Cap .BUTT
206- else -> android.graphics. Paint . Cap .BUTT
225+ StrokeCap .Square -> Cap .SQUARE
226+ StrokeCap .Round -> Cap .ROUND
227+ StrokeCap .Butt -> Cap .BUTT
228+ else -> Cap .BUTT
207229 }
208230}
209231
210- internal fun NativePaint .getNativeStrokeJoin (): StrokeJoin =
232+ internal fun android.graphics.Paint .getNativeStrokeJoin (): StrokeJoin =
211233 when (this .strokeJoin) {
212234 android.graphics.Paint .Join .MITER -> StrokeJoin .Miter
213235 android.graphics.Paint .Join .BEVEL -> StrokeJoin .Bevel
214236 android.graphics.Paint .Join .ROUND -> StrokeJoin .Round
215237 else -> StrokeJoin .Miter
216238 }
217239
218- internal fun NativePaint .setNativeStrokeJoin (value : StrokeJoin ) {
240+ internal fun android.graphics.Paint .setNativeStrokeJoin (value : StrokeJoin ) {
219241 this .strokeJoin =
220242 when (value) {
221243 StrokeJoin .Miter -> android.graphics.Paint .Join .MITER
@@ -225,13 +247,13 @@ internal fun NativePaint.setNativeStrokeJoin(value: StrokeJoin) {
225247 }
226248}
227249
228- internal fun NativePaint .getNativeStrokeMiterLimit (): Float = this .strokeMiter
250+ internal fun android.graphics.Paint .getNativeStrokeMiterLimit (): Float = this .strokeMiter
229251
230- internal fun NativePaint .setNativeStrokeMiterLimit (value : Float ) {
252+ internal fun android.graphics.Paint .setNativeStrokeMiterLimit (value : Float ) {
231253 this .strokeMiter = value
232254}
233255
234- internal fun NativePaint .getNativeFilterQuality (): FilterQuality =
256+ internal fun android.graphics.Paint .getNativeFilterQuality (): FilterQuality =
235257 if (! this .isFilterBitmap) {
236258 FilterQuality .None
237259 } else {
@@ -243,15 +265,15 @@ internal fun NativePaint.getNativeFilterQuality(): FilterQuality =
243265 FilterQuality .Low
244266 }
245267
246- internal fun NativePaint .setNativeFilterQuality (value : FilterQuality ) {
268+ internal fun android.graphics.Paint .setNativeFilterQuality (value : FilterQuality ) {
247269 this .isFilterBitmap = value != FilterQuality .None
248270}
249271
250- internal fun NativePaint .setNativeShader (value : Shader ? ) {
272+ internal fun android.graphics.Paint .setNativeShader (value : Shader ? ) {
251273 this .shader = value
252274}
253275
254- internal fun NativePaint .setNativePathEffect (value : PathEffect ? ) {
276+ internal fun android.graphics.Paint .setNativePathEffect (value : PathEffect ? ) {
255277 this .pathEffect = (value as AndroidPathEffect ? )?.nativePathEffect
256278}
257279
@@ -262,7 +284,7 @@ internal fun NativePaint.setNativePathEffect(value: PathEffect?) {
262284 */
263285@RequiresApi(Build .VERSION_CODES .Q )
264286internal object WrapperVerificationHelperMethods {
265- fun setBlendMode (paint : NativePaint , mode : BlendMode ) {
287+ fun setBlendMode (paint : android.graphics. Paint , mode : BlendMode ) {
266288 paint.blendMode = mode.toAndroidBlendMode()
267289 }
268290}
0 commit comments