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

相关推荐
梦幻通灵1 小时前
Mysql字段判空实用技巧
android·数据库·mysql
龘龍龙4 小时前
Python基础(九)
android·开发语言·python
gjc5924 小时前
MySQL隐蔽 BUG:组合条件查询无故返回空集?深度排查与规避方案
android·数据库·mysql·bug
梨落秋霜5 小时前
Python入门篇【元组】
android·数据库·python
zh_xuan5 小时前
kotlin定义函数和变量
android·开发语言·kotlin
Digitally7 小时前
Android 上的联系人备份和恢复:5 种可靠且方便的方法
android
默|笙7 小时前
【Linux】进程控制(3)进程程序替换
android·linux·运维
国家二级编程爱好者8 小时前
Android Lottie使用,如何自定义LottieView?
android·前端
bst@微胖子8 小时前
CrewAI+FastAPI实现营销战略协助智能体项目
android·数据库·fastapi
子林Android8 小时前
AndroidStudio修改.android、.gradle、.AndroidStudio路径,释放C盘空间
android·android studio