Android改造CardView为圆形View,Kotlin

Android改造CardView为圆形View,Kotlin

可以利用androidx.cardview.widget.CardView的cardCornerRadius特性,将CardView改造成一个圆形的View,技术实现的关键首先设定CardView为一个宽高相等的View(正方形),然后将radius特意设置成宽度(或高度,一样,因为正方形,宽高相等)的1/2,此时CardView自然就变成一个圆。

(1)kotlin代码运行时生成:

Kotlin 复制代码
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val circularCardView = CircularCardView(this)
        circularCardView.setCardBackgroundColor(Color.BLUE)
        setContentView(circularCardView)
    }
}

class CircularCardView(ctx: Context) : CardView(ctx) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val width = measuredWidth
        setMeasuredDimension(width, width)
        radius = (width / 2).toFloat()
    }
}

(2)在xml布局中实现:

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.cardview.widget.CardView
        android:layout_width="200dp"
        android:layout_height="200dp"
        app:cardBackgroundColor="@android:color/holo_orange_light"
        app:cardCornerRadius="100dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.cardview.widget.CardView>

</androidx.constraintlayout.widget.ConstraintLayout>

Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案_android 官方圆角-CSDN博客Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案RoundedBitmapDrawable是Android在support v4的扩展包中新增的实现圆角图形的关键类,借助RoundedBitmapDrawable的帮助,可以轻松的以Android标准方式实现圆角图形图象。写一个简单的例子。写一个线性布局,竖直方向放三个Im_android 官方圆角https://blog.csdn.net/zhangphil/article/details/51829650Android CardView设置成普通的Framelayout-CSDN博客比如可以这样写代码,CardView就变成普通的FrameLayout:https://blog.csdn.net/zhangphil/article/details/52911922

相关推荐
恋猫de小郭12 分钟前
Android Studio Panda 3 发布,CMP 导致的 Gemini 输入问题
android·ide·flutter·ios·android studio
zh_xuan22 分钟前
Android compose 可见性动画未执行问题修复
android·compose
取码网2 小时前
2025最新口红机防篡改版本源码
android·java·javascript
2501_915918412 小时前
iOS 混淆流程 提升 IPA 分析难度 实现 IPA 深度加固
android·ios·小程序·https·uni-app·iphone·webview
4311媒体网2 小时前
织梦CMS二开实战:打造智能化的审核定时发布功能
android
亿坊电商2 小时前
亿坊·零售连锁门店管理系统|全渠道一体化-独立部署_提升经营管理效率!
android·零售
TO_WebNow2 小时前
tp5部署宝塔遇到的问题
android·ide·php·android studio
2501_915909064 小时前
React Native 上架 App Store:项目运行与审核构建的流程
android·ios·小程序·https·uni-app·iphone·webview
jinanwuhuaguo4 小时前
最新更新版本,OpenClaw v2026.4.2 深度解读剖析:Task Flow 重磅回归与安全架构的全面硬化
android·开发语言·人工智能·回归·kotlin·安全架构·openclaw
匆忙拥挤repeat4 小时前
Android Compose 状态:核心api,状态恢复,状态提升,状态容器
android