Android基于Path的addRoundRect,Canvas剪切clipPath简洁的圆形图实现,Kotlin(2)

Android基于Path的addRoundRect,Canvas剪切clipPath简洁的圆形图实现,Kotlin(2)

Kotlin 复制代码
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Path
import android.util.AttributeSet
import androidx.appcompat.widget.AppCompatImageView


class MyView : AppCompatImageView {
    private var mRadius = 0f //半径
    private val mPath = Path()

    constructor(ctx: Context, attributeSet: AttributeSet) : super(ctx, attributeSet) {
        val mBmpSrc = BitmapFactory.decodeResource(resources, R.mipmap.pic, null)
        setImageBitmap(mBmpSrc)
    }

    override fun onDraw(canvas: Canvas) {
        canvas.save()

        val w = measuredWidth.toFloat()
        val h = measuredHeight.toFloat()

        val mini = Math.min(w, h)
        mRadius = mini / 2f

        val left = (w - mini) / 2f
        val top = (h - mini) / 2f
        val right = (w + mini) / 2f
        val bottom = (h + mini) / 2f

        mPath.addRoundRect(left, top, right, bottom, mRadius, mRadius, Path.Direction.CW)
        canvas.clipPath(mPath)

        super.onDraw(canvas)
        canvas.restore()
    }
}

Android基于Path的addRoundRect,Canvas剪切clipPath简洁的圆角矩形实现,Kotlin(1)-CSDN博客文章浏览阅读850次,点赞12次,收藏8次。Android RoundedBitmapDrawable:Android官方的圆角图形图象实现方案RoundedBitmapDrawable是Android在support v4的扩展包中新增的实现圆角图形的关键类,借助RoundedBitmapDrawable的帮助,可以轻松的以Android标准方式实现圆角图形图象。头像有标准的四方形,也有圆形(如QQ)。Android水平渐变色圆角矩形一个Android水平渐变色圆角矩形,如图:其实实现很简单,主要感觉颜色渐变,圆角弧度比较漂亮,故记录下来。https://blog.csdn.net/zhangphil/article/details/144586015

相关推荐
灯火不休ᝰ14 分钟前
[kotlin] 从Java到Kotlin:掌握基础语法差异的跃迁指南
java·kotlin·安卓
墨月白18 分钟前
[QT]QProcess的相关使用
android·开发语言·qt
enbug1 小时前
编译安卓内核:以坚果R1、魔趣MK100(Android 10)系统为例
android·linux
、BeYourself1 小时前
应用专属文件与应用偏好设置(SharedPreferences)
android
2501_948120151 小时前
基于模糊数学的风险评估模型
android
MengFly_1 小时前
Compose 脚手架 Scaffold 完全指南
android·java·数据库
·云扬·2 小时前
MySQL Binlog三种记录格式详解
android·数据库·mysql
月明泉清3 小时前
Android中对于点击事件的深度梳理(二)
android
遇见火星3 小时前
Linux 服务可用性监控实战:端口、进程、接口怎么监控?
android·linux·运维
njsgcs3 小时前
基于memos和agentscope的ai工具和记忆调用助手
android