Android 5.1 上的 CardView - 为什么显示菱形而不是圆?

CardView on Android 5.1 - why show rhombus instead of a circle?

我有一个带有 ImageView 的 CardView:

<android.support.v7.widget.CardView
        android:id="@+id/message_avatar_card"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        app:cardCornerRadius="40dp"
        android:innerRadius="0dp"
        android:thicknessRatio="1.9"
        android:background="@color/fui_transparent">

        <ImageView
            android:id="@+id/message_avatar"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_person_black_24dp"
            android:layout_marginEnd="4dp"
            android:scaleType="centerCrop"
            android:background="@color/fui_transparent"/>
    </android.support.v7.widget.CardView>

在Android 9.0 一切正常,看截图

但是在 Android 5.1 上我看到了菱形,而不是圆

如何解决?

正确的代码

<android.support.v7.widget.CardView
        android:id="@+id/message_avatar_card"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginEnd="4dp"
        android:layout_marginStart="4dp"
        app:cardCornerRadius="20dp"
        android:innerRadius="0dp"
        android:thicknessRatio="1.9"
        android:background="@color/fui_transparent">

        <ImageView
            android:id="@+id/message_avatar"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_person_black_24dp"
            android:layout_marginEnd="4dp"
            android:scaleType="centerCrop"
            android:background="@color/fui_transparent"/>
    </android.support.v7.widget.CardView>

使用CircleImageView库:

implementation 'de.hdodenhof:circleimageview:2.2.0'

在 XML 中:

<de.hdodenhof.circleimageview.CircleImageView
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/message_avatar"
        android:layout_width="75dp"
        android:layout_height="75dp"
        android:src="@drawable/avatar"
        app:civ_border_width="2dp"
        android:layout_gravity="center"
        app:civ_border_color="YOUR_COLOR"/> // if you don't want give any border remove this line

在java中:

CircleImageView message_avatar = (CircleImageView) findViewById(R.id.message_avatar);

如果您动态设置图像,请使用 Picasso 库:

implementation 'com.squareup.picasso:picasso:2.5.2'

在Java

Picasso.with(context).load(IMAGE_PATH).into(message_avatar);