我正在使用“线圈”来获得圆形图像视图。我想为这个圆形图像视图绘制一个圆形边框。如何使用线圈来做到这一点?
这是我加载图像的地方:
binding.imageviewFaceDetectionSelfieCircle.load(R.drawable.ic_avatar) {
transformations(CircleCropTransformation())
}
这是图像 XML 的位置:
<ImageView
android:id="@+id/imageview_face_detection_selfie_circle"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="@id/imageview_face_detection_id_card"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="70dp"/>
Coil
中没有此选项。要绘制边框,您可以使用 ShapableImageView
中的
"com.google.android.material:material:1.2.0"
<com.google.android.material.imageview.ShapeableImageView
android:layout_width="50dp"
android:layout_height="50dp"
app:strokeColor="@color/blue"
app:strokeWidth="3dp" />
如果您检查 Coil 的拉取请求,您可以在here看到用户t-kurimura尝试实现您想要的功能。查看他的 commit 并创建您自己的 CircleCropTransformation 类。这非常简单而且有效!我在我的项目中做到了!
这是老问题,但没有具体答案。 我将分享我的变体
BorderedCircleCropTransformation
类添加到您的项目中
https://gist.github.com/sergestanislavsky/52c76a5a0b61cf5f76e39724916c6f88使用示例
if (isRoundedImage) {
transformations(
BorderedCircleCropTransformation(
dpToPx(LocalContext.current, borderImageWidth), borderImageColor.toArgb())
)
}
fun dpToPx(context: Context, dp: Int): Float {
return dp * context.resources.displayMetrics.density
}