自定义VIEW:实现图形头像

记录demo中实现一个圆形头像的选择与上传

先自定义实现头像上传View 


/**
 * 带有自定义字体TextView。
 */
class EditAvatarUploadView : AppCompatTextView {
    lateinit var paint:Paint

    constructor(context: Context) : this(context, null){
        iniPaint()
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0){
        iniPaint()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        iniPaint()
    }


    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)

        iniPaint()
    }

    private fun iniPaint() {
        paint = Paint()
        paint.setAntiAlias(true)
        paint.setColor(Color.LTGRAY)
        paint.setStyle(Paint.Style.STROKE)
        paint.textSize= 20F
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //得到屏幕宽高
        var width = width
        var radius = width - 300 / 2
        var height = height


        canvas!!.drawCircle((width / 2).toFloat(), (height / 2).toFloat(), radius.toFloat(), paint)
        //绘制一条直线(两点确定一线)
       /* paint.setColor(Color.RED)
        canvas.drawLine(5F, (height/2+50).toFloat(), width.toFloat()-5, (height/2+50).toFloat(), paint)*/

        drawMask(canvas)
        drawCam(canvas)
        drawText(canvas)

    }

    fun drawMask(canvas: Canvas) {
        val bitmap =
            BitmapFactory.decodeResource(context.getResources(), R.mipmap.login_upload_half_mask)
        val dst = Rect(6, 60, 293, 160)
        /***********绘制圆弧
         * 矩形的宽度width=C(right)-A(left),高度height=D(bottom)-B(top)*/
        canvas.translate(0F, (width/2-10).toFloat())
        val rectf_head = RectF(1f, 0f, 291f, 207f) //确定外切矩形范围

       rectf_head.offset(5f, 90f) //使rectf_head所确定的矩形向右偏移100像素,向下偏移20像素
        //开始角度(以时钟3点的方向为0°,逆时针为正方向)
        //(以时钟3点的方向为0°,逆时针为正方向)
        //canvas.drawArc(rectf_head, 0F, 180F, false, paint) //绘制圆弧,不含圆心
        canvas.drawBitmap(bitmap, null, dst, null);

    }

    fun drawCam(canvas: Canvas) {
        val bitmap =
            BitmapFactory.decodeResource(context.getResources(), R.mipmap.login_edit_camera_icon)
        val dst = Rect(10, 60, 50, 100)
        dst.offset(70, 20)
        canvas.drawBitmap(bitmap, null, dst, null);

    }

    fun drawText(canvas: Canvas) {

        val text = "点击上传"
        paint.setColor(Color.WHITE)
        //x表示文本原点的x坐标;y表示文本基线的y坐标。
        canvas.drawText(text, 130F, 110F, paint)

    }

}

看看效果: 

BitmapShader实现圆形的渲染:

BitmapShader是Shader的子类,BitmapShader可以根据设置的方式将图片铺满所选区域: 
(1)CLAMP:拉伸,在x方向上是图片的最后一列像素重复平铺,而y方向是最后一行往下拉伸 
(2)REPEAT: 重复,很容易理解,图片重复平铺过去 
(3)MIRROR:镜像,就是将图片翻转

先初始化Paint对象:

mBitmapShader = BitmapShader(mBitmap!!, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        paint.setAntiAlias(true)
        paint.shader = mBitmapShader
        paint.setColor(Color.LTGRAY)

在onDraw方法中,用这个Paint画出来我们想要的圆形和边框:

  override fun onDraw(canvas: Canvas) {
        /*先绘制背景,再绘制图片,最后再绘制边框,*/
        if (drawable == null) {
            return
        }
        mDrawableRadius = (width - 300 / 2).toFloat()
        canvas.drawCircle(
            (width / 2).toFloat(),
            (height / 2).toFloat(),
            mDrawableRadius,
            paint
        )
        // 绘制边框
        if (mBorderWidth != 0) {
            canvas.drawCircle(
                (width / 2).toFloat(),
                (height / 2).toFloat(),
                mBorderRadius,
                mBorderPaint
            )
        }

        drawMask(canvas)
        drawCam(canvas)
        drawText(canvas)
    }

测试发现多次替换图片时由于缓存原因,图片更新不及时 

   val optionsa = RequestOptions()
            /*  optionsa.placeholder(R.mipmap.ic_launcher)
              optionsa.error(R.mipmap.ic_launcher_round) //异常显示图*/

            optionsa.diskCacheStrategy(DiskCacheStrategy.NONE) //禁用掉Glide的缓存功能

            optionsa.skipMemoryCache(true) //禁用掉Glide的内存缓存

            //file:/storage/emulated/0/Android/data/com.jiangxue.heartview/cache/avatar.png
            val loadSdCard = ImageUtil.loadSdCard(editAvatarView, pathList?.get(0))
            Glide.with(this).load(loadSdCard).apply(optionsa).into(editAvatarView)

代码地址

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

TouchOfSun

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值