欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > Android经典实战之Kotlin中实现圆角图片和圆形图片

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

2025/2/23 20:18:42 来源:https://blog.csdn.net/myth13141314/article/details/141000629  浏览:    关键词: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.minclass RoundedImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : AppCompatImageView(context, attrs, defStyleAttr) {private val path = Path()private var cornerRadius = 0finit {// 初始化代码,可以在此读取自定义属性}override fun onDraw(canvas: Canvas) {val rect = canvas.clipBoundsval radius = min(rect.width(), rect.height()) / 2fpath.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 = radiusinvalidate()}
}

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

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

<com.example.yourpackage.RoundedImageViewandroid: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.ShapeableImageViewandroid: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.ShapeableImageViewval 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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词