Android经典实战之Kotlin中实现圆角图片和圆形图片

本文首发于公众号“AntDream”,欢迎微信搜索“AntDream”或扫描文章底部二维码关注,和我一起每天进步一点点

实现圆角是一个很常见的需求,也有很多种方式,这里介绍2种,实现起来都不麻烦,很方便

方法一:自定义View

在 Kotlin 中实现圆角的 AppCompatImageView 可以通过自定义控件和使用 CanvasPath 进行剪裁来实现。下面是一个简单的实现方法,继承 AppCompatImageView 并自定义绘制方法,使其可以设置圆角属性。

自定义 AppCompatImageView

首先,创建一个自定义的 AppCompatImageView 类:

import android.content.Context
import android.graphics.Canvas
import android.graphics.Path
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView
import kotlin.math.min

class RoundedImageView @JvmOverloads constructor(
    context: Context, 
    attrs: AttributeSet? = null, 
    defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {

    private val path = Path()
    private var cornerRadius = 0f

    init {
        // 初始化代码,可以在此读取自定义属性
    }

    override fun onDraw(canvas: Canvas) {
        val rect = canvas.clipBounds
        val radius = min(rect.width(), rect.height()) / 2f
        path.addRoundRect(
            rect.left.toFloat(), rect.top.toFloat(), rect.right.toFloat(), rect.bottom.toFloat(),
            floatArrayOf(cornerRadius, cornerRadius, cornerRadius, cornerRadius, 
                         cornerRadius, cornerRadius, cornerRadius, cornerRadius), 
            Path.Direction.CW
        )
        canvas.clipPath(path)
        super.onDraw(canvas)
    }

    // 设置圆角半径
    fun setCornerRadius(radius: Float) {
        cornerRadius = radius
        invalidate()
    }
}

在布局文件中使用自定义的 ImageView

在 XML 布局文件中使用自定义的 RoundedImageView

<com.example.yourpackage.RoundedImageView
    android:id="@+id/rounded_image_view"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:src="@drawable/your_image"
    />

在代码中动态设置圆角

最后,在代码中动态设置圆角:

val roundedImageView = findViewById<RoundedImageView>(R.id.rounded_image_view)
roundedImageView.setCornerRadius(30f) // 设置圆角半径为30像素

完整实现

将这个方案分成两个主要部分:

1、 创建一个自定义的 RoundedImageView 类,并在 onDraw 方法中重写绘制逻辑。
2、 使用自定义的 RoundedImageView 并动态设置圆角。

通过这种方式,可以实现一个自定义的 AppCompatImageView,能够根据需要动态调整圆角半径。同时,也可以进一步扩展这个自定义控件,例如支持设置不同角的圆角半径,这取决于实际的需求和设计要求。

方法二:ShapeableImageView

另一个常用的方法是使用 ShapeableImageView 以及 material 库提供的功能,它提供了一些方便的属性来实现圆角效果。

使用 ShapeableImageView

ShapeableImageView 是 Android Material 库的一部分,可以非常方便地实现圆角和其他形状效果。

添加依赖

首先,在 build.gradle 文件中添加 Material 依赖:

dependencies {
    implementation 'com.google.android.material:material:1.9.0' // 确保使用最新版本
}
在布局文件中使用 ShapeableImageView

在 XML 布局文件中使用 ShapeableImageView 并设置圆角属性:

<com.google.android.material.imageview.ShapeableImageView
    android:id="@+id/rounded_image_view"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:scaleType="centerCrop"
    android:src="@drawable/your_image"
    app:shapeAppearanceOverlay="@style/RoundedImageViewStyle"/>
定义样式

res/values/styles.xml 中定义一个样式,用于设置 ShapeableImageView 的圆角:

<resources>
    <style name="RoundedImageViewStyle" parent="">
        <item name="cornerFamily">rounded</item>
        <item name="cornerSize">16dp</item>
    </style>
</resources>
动态设置圆角半径

在代码中,你还可以动态地设置圆角半径:

import com.google.android.material.shape.CornerFamily
import com.google.android.material.imageview.ShapeableImageView

val roundedImageView = findViewById<ShapeableImageView>(R.id.rounded_image_view)
val shapeAppearanceModel = roundedImageView.shapeAppearanceModel.toBuilder()
    .setAllCorners(CornerFamily.ROUNDED, 30f) // 设置所有圆角半径为 30px
    .build()
roundedImageView.shapeAppearanceModel = shapeAppearanceModel

这种方法利用了 Material Design 提供的现成功能,可以更轻松地应用和管理圆角效果,而无需自己实现复杂的绘制逻辑。

完整实现

将这两个部分结合起来:

1、 在 build.gradle 中添加 Material 依赖。
2、 在布局文件中使用 ShapeableImageView 并设置初始的圆角样式。
3、 在代码中动态调整圆角半径。

这样,你可以获得一个易于管理且高度可控的圆角 ImageView,同时也利用了 Material Design 的强大功能。此外,ShapeableImageView 还支持其他形状和效果,可以根据需要进一步扩展。


欢迎关注我的公众号AntDream查看更多精彩文章!

AntDream

  • 12
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值