Android ImageView 设置圆角及外边框样式

这篇文章介绍了GitHub上的RoundImageView库,它允许开发者轻松设置ImageView的圆角、外边框颜色和宽度。提供了详细的XML使用示例和代码方法,展示了如何在布局文件和代码中定制控件的外观。
摘要由CSDN通过智能技术生成

github地址:GitHub - WeiLianYang/RoundImageView: 🔥🔥🔥用于设置 ImageView 的 圆角、外边框颜色、外边框宽度

添加依赖

repositories {
    mavenCentral()
}
implementation 'io.github.weilianyang:RoundImageView:1.0.2'

效果预览:

设置圆角

 

设置外边框

一、控件样式

<declare-styleable name="RoundImageView">

    <!-- 圆角大小,如果只设置了此值,则默认会使用其作为所有圆角的值 -->
    <attr name="riv_radius" format="dimension" />

    <!-- 顶部左侧圆角大小 -->
    <attr name="riv_topLeft_radius" format="dimension" />

    <!-- 顶部右侧圆角大小 -->
    <attr name="riv_topRight_radius" format="dimension" />

    <!-- 底部左侧圆角大小 -->
    <attr name="riv_bottomLeft_radius" format="dimension" />

    <!-- 底部右侧圆角大小 -->
    <attr name="riv_bottomRight_radius" format="dimension" />

    <!-- 作为圆形图片,和 riv_radius 一起使用。
         如果未设置 riv_radius,半径将取宽高最小值的一半 -->
    <attr name="riv_roundAsCircle" format="boolean" />

    <!-- 外边框颜色 -->
    <attr name="riv_borderColor" format="color" />

    <!-- 外边框宽度 -->
    <attr name="riv_borderWidth" format="dimension" />

</declare-styleable>

二、属性介绍

属性说明
riv_radius单位dp圆角大小,如果只设置了此值,则默认会使用其作为所有圆角的值
riv_topLeft_radius单位dp顶部左侧圆角大小
riv_topRight_radius单位dp顶部右侧圆角大小
riv_bottomLeft_radius单位dp底部左侧圆角大小
riv_bottomRight_radius单位dp底部右侧圆角大小
riv_roundAsCircletrue or false作为圆形图片,和 riv_radius 一起使用。如果未设置 riv_radius,半径将取宽高最小值的一半
riv_borderColor颜色值外边框颜色
riv_borderWidth单位dp外边框宽度

三、在 xml 中使用

1. 分别指定4个圆角的大小
<com.william.widget.RoundImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:scaleType="centerCrop"
    app:riv_bottomLeft_radius="32dp"
    app:riv_bottomRight_radius="25dp"
    app:riv_topLeft_radius="14dp"
    app:riv_topRight_radius="20dp" />
2. 作为圆形图片使用
<com.william.widget.RoundImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:scaleType="centerCrop"
    app:riv_roundAsCircle="true" />
3. 设置外边框宽度和颜色
<com.william.widget.RoundImageView
    android:layout_width="150dp"
    android:layout_height="150dp"
    android:scaleType="centerCrop"
    app:riv_borderColor="#ff00ff"
    app:riv_borderWidth="5dp"
    app:riv_radius="1dp" />

四、在 代码 中使用

1. 指定圆角大小及边框样式
/**
 * @param radius 圆角大小,当 asCircle 为 true 时,值作为圆形图片的半径,如果为0,则将取宽高最小值的一半
 * @param borderWidth 外边框宽度
 * @param borderColor 外边框颜色
 * @param asCircle 作为圆形图片使用,默认 false
 */
fun setRadiusAndBorder(
    radius: Float,
    borderWidth: Float = 0f,
    @ColorInt borderColor: Int = 0,
    asCircle: Boolean = false,
) {
    this.radius = radius
    this.borderWidth = borderWidth
    this.borderColor = borderColor
    this.roundAsCircle = asCircle

    updateBorderPaint()
}
2. 分别指定4个圆角的大小及边框样式
/**
 * @param topLeftRadius 顶部左侧圆角大小
 * @param topRightRadius 顶部右侧圆角大小
 * @param bottomLeftRadius 底部左侧圆角大小
 * @param bottomRightRadius 底部右侧圆角大小
 * @param borderWidth 外边框宽度
 * @param borderColor 外边框颜色
 */
fun setRadiusAndBorder(
    topLeftRadius: Float = 0f,
    topRightRadius: Float = 0f,
    bottomLeftRadius: Float = 0f,
    bottomRightRadius: Float = 0f,
    borderWidth: Float = 0f,
    @ColorInt borderColor: Int = 0
) {
    this.topLeftRadius = topLeftRadius
    this.topRightRadius = topRightRadius
    this.bottomLeftRadius = bottomLeftRadius
    this.bottomRightRadius = bottomRightRadius
    this.borderWidth = borderWidth
    this.borderColor = borderColor

    updateBorderPaint()
}
### 回答1: 可以通过以下步骤设置 Android ImageView圆角: 1. 创建一个 xml 文件,例如 "rounded_corner.xml",并将以下代码复制到文件中: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="10dp" /> </shape> ``` 2. 在布局文件中,将 ImageView 的背景设置为刚才创建的 xml 文件: ``` <ImageView android:id="@+id/my_image_view" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/rounded_corner" android:src="@drawable/my_image" /> ``` 3. 如果需要在代码中设置圆角,可以使用以下代码: ``` ImageView imageView = findViewById(R.id.my_image_view); GradientDrawable drawable = (GradientDrawable) imageView.getBackground(); drawable.setCornerRadius(10f); ``` 其中,10f 是圆角的半径,可以根据需要进行调整。 ### 回答2: AndroidImageView是常用的一种控件,用于展示图片。在实际开发中,我们可能需要对ImageView进行一些特殊的样式设置,比如给一个ImageView设置圆角。下面是一些关于Android ImageView如何设置圆角的方法。 方法一:使用drawable实现圆角效果 我们可以将图片包裹在一个drawable里面,并使用该drawable设置ImageView的背景。因为可以对drawable进行裁剪,所以我们就可以通过在drawable里面设置圆角来实现ImageView圆角效果。 1.创建一个xml文件 drawable/round_corners.xml: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <corners android:radius="20dp"/> <solid android:color="@color/yourColor"/> </shape> ``` 其中,corners属性设置的就是圆角的大小,solid属性设置的就是要填充的颜色。 2.使用该drawable设置ImageView的背景: ``` <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/round_corners" android:scaleType="centerCrop" /> ``` 需要注意的是,这种方法对ImageView的背景进行裁剪,而不是对展示的图片进行裁剪,因此可能会出现图片被拉伸的问题。 方法二:使用BitmapShader实现圆角效果 我们也可以通过创建一个圆形bitmap并将其设置ImageView的背景来实现圆角效果。 1.创建一个自定义的CircleImageView类: ``` public class CircleImageView extends AppCompatImageView { private Paint mPaint; private BitmapShader mShader; private RectF mRectF; private float mRadius; public CircleImageView(Context context) { this(context, null); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mPaint = new Paint(); mPaint.setAntiAlias(true); } /** * 重写onSizeChanged,在ImageView的大小改变时重新计算圆形的半径 */ @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mRectF = new RectF(0, 0, w, h); mRadius = Math.min(w, h) / 2; mShader = new BitmapShader(drawableToBitmap(getDrawable()), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP); invalidate(); } /** * 重写onDraw,绘制圆形bitmap */ @Override protected void onDraw(Canvas canvas) { mPaint.setShader(mShader); canvas.drawRoundRect(mRectF, mRadius, mRadius, mPaint); } /** * 将Drawable转换为Bitmap */ private Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { return ((BitmapDrawable) drawable).getBitmap(); } else if (drawable instanceof ColorDrawable) { Bitmap bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawColor(((ColorDrawable) drawable).getColor()); return bitmap; } else { throw new IllegalArgumentException("Unsupported drawable type"); } } } ``` 2.在布局文件中使用自定义的CircleImageView: ``` <your.package.name.CircleImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/yourImage" android:scaleType="centerCrop" /> ``` 这种方法可以直接对图片进行裁剪,不会出现拉伸的问题。 综上所述,以上是两种常见的实现ImageView圆角的方法,读者可以根据实际需要选择合适的方法。 ### 回答3: 在Android中,我们可以使用ImageView来展示图片,有时候需要对图片进行圆角处理,这一过程涉及到几个步骤: 第一步,创建一个ShapeDrawable对象,用来实现圆角效果,代码如下: ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RoundRectShape(new float[]{ radius, radius, radius, radius, radius, radius, radius, radius }, null, null)); 其中,RoundRectShape的构造方法中传入一个float数组来指定每一个角的半径大小,这里都设置为radius。 第二步,使用BitmapDrawable将图片转换为Bitmap,并设置给ShapeDrawable的Drawable属性: Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap); shapeDrawable.setDrawable(bitmapDrawable); 第三步,将我们创建的ShapeDrawable对象与ImageView关联起来: ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setBackground(shapeDrawable); 这样,我们就完成了对ImageView进行圆角处理,具体实现过程如下: ``` ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RoundRectShape(new float[]{ radius, radius, radius, radius, radius, radius, radius, radius }, null, null)); Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image); BitmapDrawable bitmapDrawable = new BitmapDrawable(getResources(), bitmap); shapeDrawable.setDrawable(bitmapDrawable); ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setBackground(shapeDrawable); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值