ShapeableImageView具有带有StrokeWidth的边框,四个边都进行了修剪

7
今天我刚尝试使用来自Material设计的ShapeableImageView
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/GuidelineTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.15" />

    <com.google.android.material.imageview.ShapeableImageView
        android:id="@+id/imgProfile"
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:scaleType="centerCrop"
        app:strokeColor="@color/grey400"
        app:strokeWidth="@dimen/dp3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toBottomOf="@+id/GuidelineTop"
        app:shapeAppearanceOverlay="@style/circleImageView" />
</androidx.constraintlayout.widget.ConstraintLayout>

我的圆形头像视图

<style name="circleImageView" parent="">
    <item name="cornerFamily">rounded</item>
    <item name="cornerSize">50%</item>
</style>

这是一个样例截图。

在此输入图片描述

正如您所见,描边在左侧、上方、右侧和底部被切掉了。我有什么遗漏吗?


2
你尝试过添加填充吗? - momvart
https://github.com/material-components/material-components-android/issues/1173 - 0xA
疯狂的、简单的填充方式起到了帮助作用。谢谢 @MohammadOmidvar。如果你想要署名,请将其添加为答案。 - Gunaseelan
谢谢。我会写一个答案。 - momvart
这是一个已知的错误,请检查 https://github.com/material-components/material-components-android/issues/1329 - Mohammed Alaa
1个回答

23
在Android中,由于Paint笔触总是居中绘制(一半在内部,一半在外部),因此您可以添加宽度为笔触宽度一半的填充。
<com.google.android.material.imageview.ShapeableImageView
  ...
  app:strokeWidth="@dimen/dp3"
  android:padding="@dimen/dp4"
/>

(其中dp4被认为是dp3的一半)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接