Ich habe ein VectorDrawable mit mehreren Pfaden erstellt, von denen jeder einen eindeutigen Namen hat.
Ich habe versucht, m i t V e c t o r D r a w a b l e C o m p a t a u f d i e F a r b e d e s P f a d s z u z u g r e i f e n u n d d i e s e z u ä n d e r n . < / p > < b r / > < p r e c l a s s = " l a n g - x m l P r e t t y p r i n t - o v e r r i d e " > < c o d e > & l t ; v e c t o r x m l n s : a n d r o i d = & q u o t ; h t t p : / / s c h e m a s . a n d r o i d . c o m / a p k / r e s / a n d r o i d & q u o t ; < b r / > a n d r o i d : w i d t h = & q u o t ; 1 1 8 . 4 1 d p & q u o t ; < b r / > a n d r o i d : h e i g h t = & q u o t ; 1 3 7 . 7 8 d p & q u o t ; < b r / > a n d r o i d : v i e w p o r t W i d t h = & q u o t ; 1 1 8 . 4 1 & q u o t ; < b r / > a n d r o i d : v i e w p o r t H e i g h t = & q u o t ; 1 3 7 . 7 8 & q u o t ; & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; m 4 2 . 0 8 , 8 5 . 3 5 c - 0 . 8 1 , 4 . 6 - 6 . 6 4 , 5 . 9 5 - 9 . 2 3 , 2 . 1 4 l - 8 . 1 2 , - 1 1 . 9 2 c - 2 . 2 4 , - 3 . 2 9 - 6 . 6 8 , - 4 . 0 8 - 9 . 9 2 , - 1 . 7 5 - 2 . 9 7 , 2 . 1 4 - 3 . 9 3 , 6 . 2 2 - 2 . 2 2 , 9 . 4 7 l 1 9 . 4 3 , 3 6 . 9 9 c 5 . 2 1 , 7 . 6 6 1 3 . 3 , 1 3 . 2 5 2 3 . 0 1 , 1 4 . 9 6 1 9 . 4 3 , 3 . 4 2 3 8 . 0 3 , - 9 . 9 7 4 1 . 5 4 , - 2 9 . 9 1 l 6 . 7 9 , - 3 8 . 5 2 c 0 . 7 , - 3 . 9 9 - 1 . 8 7 , - 7 . 7 8 - 5 . 7 6 , - 8 . 4 7 - 3 . 8 9 , - 0 . 6 8 - 7 . 6 , 1 . 9 9 - 8 . 3 1 , 5 . 9 8 l - 3 . 8 2 , 2 1 . 6 7 6 . 3 7 , - 3 6 . 1 1 c 0 . 7 , - 3 . 9 9 - 1 . 8 8 , - 7 . 7 8 - 5 . 7 6 , - 8 . 4 7 - 3 . 8 9 , - 0 . 6 9 - 7 . 6 , 1 . 9 9 - 8 . 3 1 , 5 . 9 8 l - 5 . 5 2 , 3 1 . 3 0 . 8 5 , - 4 . 8 1 6 . 3 7 , - 3 6 . 1 2 c 0 . 7 1 , - 3 . 9 9 - 1 . 8 7 , - 7 . 7 8 - 5 . 7 6 , - 8 . 4 7 - 3 . 8 9 , - 0 . 6 8 - 7 . 6 , 2 - 8 . 3 1 , 5 . 9 8 l - 6 . 3 7 , 3 6 . 1 1 - 0 . 8 5 , 4 . 8 2 6 . 3 7 , - 3 6 . 1 2 c 0 . 7 , - 3 . 9 9 - 1 . 8 8 , - 7 . 7 8 - 5 . 7 6 , - 8 . 4 6 - 3 . 8 9 , - 0 . 6 9 - 7 . 6 , 1 . 9 9 - 8 . 3 1 , 5 . 9 8 l - 6 . 3 7 , 3 6 . 1 1 z & q u o t ; < b r / > a n d r o i d : s t r o k e L i n e J o i n = & q u o t ; r o u n d & q u o t ; < b r / > a n d r o i d : s t r o k e W i d t h = & q u o t ; 4 & q u o t ; < b r / > a n d r o i d : f i l l C o l o r = & q u o t ; # f f f f f f & q u o t ; < b r / > a n d r o i d : s t r o k e C o l o r = & q u o t ; # 1 e 3 f 4 d & q u o t ; < b r / > a n d r o i d : s t r o k e L i n e C a p = & q u o t ; r o u n d & q u o t ; / & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; M 0 . 7 4 , 5 5 . 4 3 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , 1 4 . 2 8 0 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , - 1 4 . 2 8 0 z & q u o t ; < b r / > a n d r o i d : s t r o k e W i d t h = & q u o t ; 1 . 4 7 & q u o t ; < b r / > a n d r o i d : f i l l C o l o r = & q u o t ; # f f f f f f & q u o t ; < b r / > a n d r o i d : n a m e = & q u o t ; R 1 & q u o t ; < b r / > a n d r o i d : s t r o k e C o l o r = & q u o t ; # 1 e 3 e 4 d & q u o t ; / & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; M 4 8 . 1 , 1 0 . 1 9 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , 1 4 . 2 8 0 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , - 1 4 . 2 8 0 z & q u o t ; < b r / > a n d r o i d : s t r o k e W i d t h = & q u o t ; 1 . 4 7 & q u o t ; < b r / > a n d r o i d : f i l l C o l o r = & q u o t ; # f f f f f f & q u o t ; < b r / > a n d r o i d : s t r o k e C o l o r = & q u o t ; # 1 e 3 e 4 d & q u o t ; / & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; M 7 0 . 0 6 , 8 . 0 7 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , 1 4 . 2 8 0 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , - 1 4 . 2 8 0 z & q u o t ; < b r / > a n d r o i d : s t r o k e W i d t h = & q u o t ; 1 . 4 7 & q u o t ; < b r / > a n d r o i d : f i l l C o l o r = & q u o t ; # f f f f f f & q u o t ; < b r / > a n d r o i d : s t r o k e C o l o r = & q u o t ; # 1 e 3 e 4 d & q u o t ; / & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; M 8 7 . 7 8 , 1 8 . 6 5 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , 1 4 . 2 8 0 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , - 1 4 . 2 8 0 z & q u o t ; < b r / > a n d r o i d : s t r o k e W i d t h = & q u o t ; 1 . 4 7 & q u o t ; < b r / > a n d r o i d : f i l l C o l o r = & q u o t ; # f f f f f f & q u o t ; < b r / > a n d r o i d : s t r o k e C o l o r = & q u o t ; # 1 e 3 e 4 d & q u o t ; / & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; M 1 0 3 . 3 9 , 4 0 . 0 8 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , 1 4 . 2 8 0 a 7 . 1 4 , 7 . 3 3 0 , 1 0 , - 1 4 . 2 8 0 z & q u o t ; < b r / > a n d r o i d : s t r o k e W i d t h = & q u o t ; 1 . 4 7 & q u o t ; < b r / > a n d r o i d : f i l l C o l o r = & q u o t ; # f f f f f f & q u o t ; < b r / > a n d r o i d : s t r o k e C o l o r = & q u o t ; # 1 e 3 e 4 d & q u o t ; / & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; m 6 . 5 6 , 5 3 . 8 2 q 0 . 4 2 , 0 0 . 6 2 , 0 . 1 6 0 . 2 , 0 . 1 6 0 . 2 , 0 . 4 8 0 , 0 . 1 8 - 0 . 0 7 , 0 . 3 - 0 . 0 7 , 0 . 1 2 - 0 . 1 7 , 0 . 1 9 - 0 . 1 , 0 . 0 7 - 0 . 2 2 , 0 . 1 1 l 0 . 6 2 , 1 . 0 2 h - 0 . 3 3 l - 0 . 5 5 , - 0 . 9 4 h - 0 . 4 5 v 0 . 9 4 h - 0 . 2 9 v - 2 . 2 7 z M 6 . 5 5 , 5 4 . 0 7 h - 0 . 3 2 v 0 . 8 4 h 0 . 3 4 q 0 . 2 8 , 0 0 . 4 , - 0 . 1 1 0 . 1 3 , - 0 . 1 1 0 . 1 3 , - 0 . 3 2 0 , - 0 . 2 2 - 0 . 1 3 , - 0 . 3 1 - 0 . 1 3 , - 0 . 0 9 - 0 . 4 1 , - 0 . 0 9 z M 8 . 7 3 , 5 6 . 0 9 h - 0 . 2 7 v - 1 . 5 8 q 0 , - 0 . 1 4 0 , - 0 . 2 2 0 , - 0 . 0 8 0 . 0 1 , - 0 . 1 7 - 0 . 0 5 , 0 . 0 5 - 0 . 0 9 , 0 . 0 9 - 0 . 0 4 , 0 . 0 4 - 0 . 1 , 0 . 0 9 l - 0 . 2 4 , 0 . 2 - 0 . 1 5 , - 0 . 1 9 0 . 6 1 , - 0 . 4 8 h 0 . 2 3 z & q u o t ; < b r / > a n d r o i d : s t r o k e A l p h a = & q u o t ; 0 . 9 8 & q u o t ; < b r / > a n d r o i d : s t r o k e W i d t h = & q u o t ; 0 . 2 7 & q u o t ; < b r / > a n d r o i d : f i l l C o l o r = & q u o t ; # e 0 8 6 c a & q u o t ; < b r / > a n d r o i d : s t r o k e C o l o r = & q u o t ; # 1 e 3 e 4 d & q u o t ; < b r / > a n d r o i d : f i l l A l p h a = & q u o t ; 0 . 9 8 & q u o t ; / & g t ; < b r / > & l t ; p a t h < b r / > a n d r o i d : p a t h D a t a = & q u o t ; m 5 4 . 6 3 , 9 . 4 3 q 0 . 4 2 , 0 0 . 6 2 , 0 . 1 6 0 . 2 , 0 . 1 6 0 . 2 , 0 . 4 8 0 , 0 . 1 8 - 0 . 0 7 , 0 . 3 - 0 . 0 7 , 0 . 1 2 - 0 . 1 7 , 0 . 1 9 - 0 . 1 , 0 . 0 7 - 0 . 2 2 , 0 . 1 1 l 0 . 6 2 , 1 . 0 2 h - 0 . 3 3 l - 0 . 5 5 , - 0 . 9 4 h - 0 . 4 5 v 0 . 9 4 h - 0 . 2 9 v - 2 . 2 7 z M 5 4 . 6 1 , 9 . 6 8 h - 0 . 3 2 v 0 . 8 4 h 0 . 3 4 q 0 . 2 8 , 0 0 . 4 , - 0 . 1 1 0 . 1 3 , - 0 . 1 1 0 . 1 3 , - 0 . 3 2 0 , - 0 . 2 2 - 0 . 1 3 , - 0 . 3 1 - 0 . 1 3 , - 0 . 0 9 - 0 . 4 1 , - 0 . 0 9 z M 5 7 . 3 2 , 1 1 . 7 h - 1 . 5 v - 0 . 2 3 l 0 . 5 9 , - 0 . 6 q 0 . 1 7 , - 0 . 1 7 0 . 2 9 , - 0 . 3 0 . 1 2 , - 0 . 1 3 0 . 1 8 , - 0 . 2 6 0 . 0 6 , - 0 . 1 3 0 . 0 6 , - 0 . 2 8 0 , - 0 . 1 9 - 0 . 1 1 , - 0 . 2 8 - 0 . 1 1 , - 0 . 1 - 0 . 2 9 , - 0 . 1 - 0 . 1 7 , 0 - 0 . 2 9 , 0 . 0 6 - 0 . 1 2 , 0 . 0 6 - 0 . 2 5 , 0 . 1 6 l - 0 . 1 5 , -0.19q0.13,-0.11 0.3,-0.19 0.17,-0.08 0.39,-0.08 0.32,0 0.5,0.16 0.18,0.16 0.18,0.44 0,0.18 -0.07,0.33 -0.07,0.16 -0.2,0.31 -0.13,0.15 -0.3,0.32l-0.47,0.47v0.01h1.15z"
android:strokeAlpha="0.98"
android:strokeWidth="0.27"
android:fillColor="#e086ca"
android:strokeColor="#1e3e4d"
android:fillAlpha="0.98"/>
android:pathData="m76.63,7.5q0.42,0 0.62,0.16 0.2,0.16 0.2,0.48 0,0.18 -0.07,0.3 -0.07,0.12 -0.17,0.19 -0.1,0.07 -0.22,0.11l0.62,1.02h-0.33l-0.55,-0.94h-0.45v0.94h-0.29v-2.27zM76.62,7.75h-0.32v0.84h0.34q0.28,0 0.4,-0.11 0.13,-0.11 0.13,-0.32 0,-0.22 -0.13,-0.31 -0.13,-0.09 -0.41,-0.09zM79.24,8.03q0,0.23 -0.13,0.37 -0.13,0.13 -0.34,0.18v0.01q0.27,0.03 0.41,0.17 0.13,0.14 0.13,0.37 0,0.2 -0.09,0.35 -0.09,0.15 -0.29,0.24 -0.19,0.09 -0.49,0.09 -0.18,0 -0.33,-0.03 -0.15,-0.03 -0.29,-0.09v-0.26q0.14,0.07 0.31,0.11 0.17,0.04 0.32,0.04 0.3,0 0.44,-0.12 0.14,-0.12 0.14,-0.33 0,-0.21 -0.17,-0.3 -0.17,-0.09 -0.47,-0.09h-0.22v-0.24h0.22q0.28,0 0.42,-0.12 0.15,-0.12 0.15,-0.31 0,-0.17 -0.11,-0.25 -0.11,-0.09 -0.3,-0.09 -0.18,0 -0.31,0.05 -0.13,0.05 -0.26,0.14l-0.14,-0.19q0.12,-0.09 0.3,-0.17 0.18,-0.07 0.41,-0.07 0.36,0 0.53,0.16 0.17,0.16 0.17,0.4z"
android:strokeAlpha="0.98"
android:strokeWidth="0.27"
android:fillColor="#e086ca"
android:strokeColor="#1e3e4d"
android:fillAlpha="0.98"/>
Code: Select all
import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.VectorDrawable
import androidx.appcompat.content.res.AppCompatResources
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.requiredSize
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.text.BasicText
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.em
import androidx.compose.ui.unit.sp
import androidx.core.content.res.ResourcesCompat
import androidx.vectordrawable.graphics.drawable.VectorDrawableCompat
fun changeVectorPathColor(context: Context, drawableResId: Int, pathName: String, color: Int): VectorDrawableCompat? {
val vectorDrawable = AppCompatResources.getDrawable(context, drawableResId) as? VectorDrawableCompat
vectorDrawable?.apply {
val path = this.findPathByName(pathName)
path?.fillColor = ColorStateList.valueOf(color)
}
return vectorDrawable
}
// Extension function to find a path by its name
public fun VectorDrawableCompat.findPathByName(name: String): VectorDrawableCompat.VPath? {
return this.mVectorState.mVPathRenderer.mRootGroup.mChildren
.filterIsInstance()
.find { it.name == name }
}
@Composable
fun LeftHand() {
val context = LocalContext.current
val vectorDrawable = ResourcesCompat.getDrawable(
context.resources,
R.drawable.right_hand_with_labels,
context.theme
) as? VectorDrawable
if (vectorDrawable is VectorDrawable) {
val paths = vectorDrawable.pathData
for (i in paths.indices) {
if (paths[i].name == "R1") {
val paint = vectorDrawable.paint
paint.color = Color(desiredColor) // Replace desiredColor with your actual color
vectorDrawable.mutate() // Make sure to mutate the drawable before applying changes
break
}
}
}
Column(
modifier = Modifier.fillMaxSize(),
) {
Row {
Image(
modifier = Modifier,
painter = painterResource(id = R.drawable.right_hand_with_labels),
contentDescription = "left hand"
)
Image(
modifier = Modifier,
painter = painterResource(id = R.drawable.right_hand_with_labels),
contentDescription = "left hand"
)
}
}
}
@Composable
fun Label(
modifier: Modifier,
policy: String,
offsetX: Dp,
offsetY: Dp,
isSelected: Boolean,
labelSize: String = "60"
) {
Box(
contentAlignment = Alignment.Center,
modifier = modifier
.requiredSize(size = labelSize.toInt().dp)
.offset(x = offsetX, y = offsetY)
.clip(CircleShape)
.border(BorderStroke(4.dp, GlobalID), CircleShape)
) {
BasicText(
text = policy,
style = TextStyle(
fontSize = 16.sp,
letterSpacing = (-0.4).sp,
textAlign = TextAlign.Center,
lineHeight = 1.75.em
),
modifier = Modifier
.fillMaxSize()
.wrapContentHeight(align = Alignment.CenterVertically)
)
}
}
@Preview(showBackground = true, device = "id:pixel_8_pro")
@Composable
fun LeftHandPreview() {
LeftHand()
}