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

相关推荐
袁美丽..6 分钟前
Android studio的adb和终端的adb互相抢占端口
android·adb·android studio
鹏多多.1 小时前
flutter-使用fluttertoast制作丰富的高颜值toast
android·前端·flutter·ios
守城小轩1 小时前
Firefox Android 开发环境搭建全流程(四)
android·firefox·chrome devtools·指纹浏览器·浏览器开发
袁美丽..1 小时前
Android --- AOSP源码导入Android Studio
android·android studio
LiuYaoheng2 小时前
【Android】View 的基础知识
android·java·笔记·学习
出海小纸条2 小时前
Google Play 应用被拒-数据安全表单无效(设备上的应用)
android
和煦的春风2 小时前
简单讨论下lmkd 查杀机制
android
Android轮子哥2 小时前
月下载 40 万次的框架是怎么练成的
android
三少爷的鞋2 小时前
Kotlin 协程真的是线程框架吗?
android
三雒3 小时前
ART堆内存系列二:从堆中排除大对象
android·性能优化