Jetpack 在图像之上合成渐变图像
Jetpack compose gradient image on top of image
我在 xml 布局中有以下代码,我想移动它来编写它,但很难让它正确
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image"
app:layout_constraintDimensionRatio="375:258"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/image_gradient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/gradient"
app:layout_constraintBottom_toBottomOf="@id/image1" />
</androidx.constraintlayout.widget.ConstraintLayout>
我在 Compose 中尝试使用无效的 Box
Box {
Image(painter = painterResource(
id = R.drawable.image1),
contentDescription = null,
)
Image(painter = painterResource(
id = R.drawable.gradient),
contentDescription = null,
contentScale = ContentScale.FillBounds
)
}
我认为你应该以复合的方式创建渐变
Column(Modifier.fillMaxSize()
.background(Color.White)) {
Box(
modifier = Modifier
.fillMaxWidth()
.background(color = Color.White)
.height(200.dp)
) {
Image(
painter = painterResource(
id = R.drawable.ty
),
contentDescription = null,
modifier = Modifier
.fillMaxWidth(),
contentScale = ContentScale.Crop
)
Box(
modifier = Modifier
.background(
brush = Brush.verticalGradient(
colors = listOf(
MaterialTheme.colors.primary.copy(alpha = 0.5f),
MaterialTheme.colors.primaryVariant.copy(alpha = 0.5f)
)
)
)
.fillMaxSize()
)
}
}
}
很可能您的渐变图像大小为零。
您可以使用 Modifier.matchParentSize
:它在 BoxScope
中可用。将其应用于渐变图像,使其大小等于主图像的大小。
Box {
Image(painter = painterResource(
id = R.drawable.image1),
contentDescription = null,
)
Image(painter = painterResource(
id = R.drawable.gradient),
contentDescription = null,
contentScale = ContentScale.FillBounds,
modifier = Modifier.matchParentSize()
)
}
我在 xml 布局中有以下代码,我想移动它来编写它,但很难让它正确
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white">
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/image"
app:layout_constraintDimensionRatio="375:258"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/image_gradient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="@drawable/gradient"
app:layout_constraintBottom_toBottomOf="@id/image1" />
</androidx.constraintlayout.widget.ConstraintLayout>
我在 Compose 中尝试使用无效的 Box
Box {
Image(painter = painterResource(
id = R.drawable.image1),
contentDescription = null,
)
Image(painter = painterResource(
id = R.drawable.gradient),
contentDescription = null,
contentScale = ContentScale.FillBounds
)
}
我认为你应该以复合的方式创建渐变
Column(Modifier.fillMaxSize()
.background(Color.White)) {
Box(
modifier = Modifier
.fillMaxWidth()
.background(color = Color.White)
.height(200.dp)
) {
Image(
painter = painterResource(
id = R.drawable.ty
),
contentDescription = null,
modifier = Modifier
.fillMaxWidth(),
contentScale = ContentScale.Crop
)
Box(
modifier = Modifier
.background(
brush = Brush.verticalGradient(
colors = listOf(
MaterialTheme.colors.primary.copy(alpha = 0.5f),
MaterialTheme.colors.primaryVariant.copy(alpha = 0.5f)
)
)
)
.fillMaxSize()
)
}
}
}
很可能您的渐变图像大小为零。
您可以使用 Modifier.matchParentSize
:它在 BoxScope
中可用。将其应用于渐变图像,使其大小等于主图像的大小。
Box {
Image(painter = painterResource(
id = R.drawable.image1),
contentDescription = null,
)
Image(painter = painterResource(
id = R.drawable.gradient),
contentDescription = null,
contentScale = ContentScale.FillBounds,
modifier = Modifier.matchParentSize()
)
}