前言
在Android开发过程中,ImageView
是最常用的控件之一,为了提升应用的美观性,通常会对图片进行圆角处理,本文将详细介绍几种实现ImageView
圆角效果的方法,包括使用第三方库、系统自带控件以及自定义视图等,通过这些方法,开发者可以根据具体需求选择最适合的圆角实现方式。
目录
1、[RoundedImageView](#1-roundedimageview)
2、[CardView](#2-cardview)
3、[Glide](#3-glide)
4、[ViewOutlineProvider](#4-viewoutlineprovider)
5、[自定义View](#5-自定义view)
6、[(#6-
1. RoundedImageView
简介
RoundedImageView
是一个常用的第三方库,专门用于实现图片的圆角效果,它简单易用,只需在项目的build.gradle
文件中添加依赖即可。
使用方法
步骤1: 在build.gradle(:app)
文件中添加依赖项:
implementation 'com.makeramen:roundedimageview:2.3.0'
步骤2: 在布局文件中使用RoundedImageView
控件,并设置圆角属性:
<com.makeramen.roundedimageview.RoundedImageView android:id="@+id/iv_beauty" android:layout_width="120dp" android:layout_height="120dp" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:src="@drawable/beauty1" app:riv_corner_radius_bottom_left="10dp" app:riv_corner_radius_bottom_right="10dp" app:riv_corner_radius_top_left="10dp" app:riv_corner_radius_top_right="10dp" />
示例代码
// MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
2. CardView
简介
CardView
是Android系统自带的一个控件,可以用来包裹其他控件,并提供圆角和阴影效果,通过将ImageView
嵌套在CardView
中,可以轻松实现圆角效果。
使用方法
步骤1: 在布局文件中使用CardView
包裹ImageView
:
<androidx.cardview.widget.CardView android:layout_width="120dp" android:layout_height="120dp" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" app:cardCornerRadius="10dp" app:cardElevation="0dp"> <ImageView android:id="@+id/iv_beauty" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/beauty1"/> </androidx.cardview.widget.CardView>
示例代码
// MainActivity.java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } }
3. Glide
简介
Glide
是一个流行的图片加载库,支持多种图片处理功能,包括圆角效果,通过配置RequestOptions
,可以方便地实现圆角图片加载。
使用方法
步骤1: 在build.gradle(:app)
文件中添加依赖项:
implementation 'com.github.bumptech.glide:glide:4.13.0' annotationProcessor 'com.github.bumptech.glide:compiler:4.13.0'
步骤2: 在布局文件中定义ImageView
:
<ImageView android:id="@+id/iv_beauty" android:layout_width="120dp" android:layout_height="120dp" android:layout_centerHorizontal="true" android:layout_marginTop="50dp"/>
步骤3: 在代码中使用Glide
加载图片并设置圆角:
// MainActivity.java import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.bumptech.glide.request.target.BitmapImageViewTarget; import android.os.Bundle; import androidx.appcompat.app.AppCompatActivity; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView imageView = findViewById(R.id.iv_beauty); RequestOptions options = new RequestOptions().transform(new RoundedCorners(ScreenUtil.dp2px(10))); Glide.with(this).load(R.drawable.beauty2).apply(options).into(imageView); } }
4. ViewOutlineProvider
简介
通过自定义ViewOutlineProvider
可以实现更复杂的圆角效果,这种方法适用于需要高度自定义的场景。
使用方法
步骤1: 创建一个自定义的ViewOutlineProvider
类:
// RoundViewOutlineProvider.java import android.graphics.Outline; import android.view.View; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Path; import android.graphics.Canvas; import androidx.annotation.NonNull; import androidx.annotation.RequiresApi; import androidx.core.view.ViewCompat; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider; import androidx.core.view.ViewOutlineProvider;