Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] kotlin – Can I use drawPath to both fill shape and draw border only one time in Jetpack Compose

Posted on November 11, 2022 By

Solution 1 :

What you ask is neither available in Compose nor Android Canvas. As you can check here but with Jetpack Compose Canvas.

You don’t need to use the ones with Paint unless you need some properties of Paint or functions like drawText

DrawScope already has functions such as

fun drawPath(
    path: Path,
    color: Color,
    /*@FloatRange(from = 0.0, to = 1.0)*/
    alpha: Float = 1.0f,
    style: DrawStyle = Fill,
    colorFilter: ColorFilter? = null,
    blendMode: BlendMode = DefaultBlendMode
)

And instead of passing DrawScope as parameter it would help creating extension functions of DrawScope as default Compose source code often does.

inline fun DrawScope.rotate(
    degrees: Float,
    pivot: Offset = center,
    block: DrawScope.() -> Unit
) = withTransform({ rotate(degrees, pivot) }, block)

inline fun DrawScope.clipRect(
    left: Float = 0.0f,
    top: Float = 0.0f,
    right: Float = size.width,
    bottom: Float = size.height,
    clipOp: ClipOp = ClipOp.Intersect,
    block: DrawScope.() -> Unit
) = withTransform({ clipRect(left, top, right, bottom, clipOp) }, block)

Problem :

I use Code A to draw a shape with a path, I hope to fill the shape in a color, and draw border with different color and width. I get the Image A as I expected.

I find Code A to launch drawPath operation two times, maybe it’s not good way, can I use drawPath to both fill shape and draw border only one time?

Code A

fun setProcess(drawScope: DrawScope, dataList: List<Double>){
  
    drawScope.drawIntoCanvas{
        val step = xAxisLength / maxPointCount

        val shadowPath = Path()

        shadowPath.moveTo(0f.toX, 0f.toY)

        for (i in dataList.indices) {
             ...
        }

        shadowPath.close()
        it.drawPath(shadowPath, paintTablePath)
        it.drawPath(shadowPath, paintTableBorder)
    }
}

val paintTablePath = Paint().also {
    it.isAntiAlias = true
    it.style = PaintingStyle.Fill
    it.strokeWidth = 1f
    it.color = Color(0xffdfecfe)
}

val paintTableBorder = Paint().also {
    it.isAntiAlias = true
    it.style = PaintingStyle.Stroke
    it.strokeWidth = 3f
    it.color = Color.Red
}

Image A

enter image description here

Comments

Comment posted by HelloCW

Thanks! but could you tell me how to fill shape and draw border in one operation?

READ  [FIXED] java - how to add a double type in kotlin hashmap?
Powered by Inline Related Posts

Comment posted by stackoverflow.com/questions/3723545/…

There is no one operation to fill a shape and draw border in different color in Android not only in Compose. You can create a DrawScope extension with 2 colors and border width if you wish to but it doesn’t exist.

Comment posted by Thracian

having 2 draws doesn’t have any impact on performance. Drawing apps do lots drawings with list of paths, blend modes and so on

Android Tags:android-jetpack-compose, kotlin

Post navigation

Previous Post: [FIXED] Android ACTION_VIEW intent is not opening urls in non-browser apps like twitter and instagram
Next Post: [FIXED] android – Check if activity is active, and if active pass data from service to activity?

Related Posts

[FIXED] How to make drop shadow for shape in Android? Android
[FIXED] react native – How to display action buttons in Android notification when app is closed? Android
[FIXED] MpAndroidChart (or another iOS/Swift chart tool) use gradient based on 3rd variable Android
[FIXED] android – When changing list item layout RecyclerView scrolls to top Android
[FIXED] android – ERROR: Failed to resolve: vectordrawable-animated Android
[FIXED] android – Apply span several times Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme