Android Imageview不显示圆角
我试图使我的布局像这样,但我不能使我的imageview像这样圆角。它在Android studio的布局编辑器中完美地显示出来,但在真正的设备中,我得到的是平坦的角落。我厌倦了XML和代码,但没有一个适合我。请帮助我使我的布局像这个样本图像 我的布局文件Android Imageview不显示圆角,android,xml,android-layout,android-imageview,Android,Xml,Android Layout,Android Imageview,我试图使我的布局像这样,但我不能使我的imageview像这样圆角。它在Android studio的布局编辑器中完美地显示出来,但在真正的设备中,我得到的是平坦的角落。我厌倦了XML和代码,但没有一个适合我。请帮助我使我的布局像这个样本图像 我的布局文件 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="true"
android:background="?android:attr/colorBackground"
android:focusable="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="170dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/User_CoverPhoto"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:layout_width="150dp"
android:layout_height="match_parent"
android:layout_marginEnd="8dp"
android:background="@drawable/round_shape_only"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/User_CoverPhoto"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/User_ProfilePictures"
app:layout_constraintTop_toTopOf="parent"
/>
<ImageView
android:layout_width="230dp"
android:layout_height="match_parent"
android:background="@drawable/round_shape_only"
android:scaleType="centerCrop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/User_ProfilePictures"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="#40000000"
android:backgroundTint="#CC000000"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Profile Picture"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@+id/User_ProfilePictures"
app:layout_constraintHorizontal_bias="0.01"
app:layout_constraintStart_toStartOf="@+id/User_ProfilePictures" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="#40000000"
android:backgroundTint="#CC000000"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="Cover Photo"
android:textColor="@color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@+id/User_CoverPhoto" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="13dp"
android:id="@+id/Grid_Recycler">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
圆形状的XML
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#CCCCCC"/>
<padding android:left="1dp"
android:top="1dp"
android:right="1dp"
android:bottom="1dp"/>
<corners android:radius="15dp"/>
</shape>
使用
background
和radius
时,您应该调用setClipToOutline
到true
,它只能以编程方式调用,不能通过XML设置
imageView.setClipToOutline(true);
对于圆角,可以使用以下自定义布局: Java:RoundedCornerImageLayout.Java 您可以更改
private final static float CORNER_RADIUS=10.0f代码>根据您的要求
在XMl文件中
我希望这对您有所帮助只需使用材质组件库中的ShapeableImageView
。
比如:
<com.google.android.material.imageview.ShapeableImageView
...
app:shapeAppearanceOverlay="@style/roundedCornersImageView"
app:srcCompat="@drawable/ic_image" />
与:
圆的
xxdp
*注意:它至少需要1.2.0-alpha03
您可以将您的ImageView
放到CardView
上,并使用应用程序:cardCornerRadius
设置图像角点您可以使用Glide
只需为ImageView编写扩展函数:
使用圆角制作矩形图像的步骤
fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
Glide.with(this)
.load(resId)
.transform(RoundedCorners(radius))
.into(this)
fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
Glide.with(this)
.load(resId)
.transform(CenterCrop(), RoundedCorners(radius))
.into(this)
使用圆角制作方形图像的步骤
fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
Glide.with(this)
.load(resId)
.transform(RoundedCorners(radius))
.into(this)
fun ImageView.loadImageWithCustomCorners(@DrawableRes resId: Int, radius: Int) =
Glide.with(this)
.load(resId)
.transform(CenterCrop(), RoundedCorners(radius))
.into(this)
在代码中使用
iv_service_avatar.loadImageWithCustomCorners(R.drawable.ic_default_avatar, 15)
iv_service_avatar它的ImageView ID你可以使用这个库,你的代码在哪里设置图像到ImageView?检查这个@PankajKumar我正在使用Glide加载图像。使用自定义ImageView
不比FrameLayout
iv_service_avatar.loadImageWithCustomCorners(R.drawable.ic_default_avatar, 15)