自定义View 笑脸评分(kotlin语言)

自定义View 笑脸评分(kotlin语言)

效果

可以0分,0.5分,1分

class MyView (context: Context,attributeSet: AttributeSet) : View(context,attributeSet) {
    var picw : Int =0
    var picy : Int =0
    var flag : Boolean = false
    var x : Float? = 0.0f
    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        val paint = Paint()
        val bitmap = BitmapFactory.decodeResource(resources, R.mipmap.zero)//零分笑脸
         val full = BitmapFactory.decodeResource(resources, R.mipmap.full)//满分笑脸
        val half = BitmapFactory.decodeResource(resources, R.mipmap.half)//半分笑脸
        val width = bitmap.width;
        val height = bitmap.height
        picw=width
        picy=height
        for (i in 0..4){
            canvas!!.drawBitmap(bitmap,i*width.toFloat(),10.0f,paint)
        }
        val count = x!!.toInt() / picw
        val halfcount = x!! / picw
        val bigDecimal = BigDecimal(halfcount.toDouble())
        val toDouble = bigDecimal.setScale(1, BigDecimal.ROUND_HALF_UP).toDouble()
        val split = toDouble.toString().split(".")
        if (flag){
            for (i in 0..count-1){
                canvas!!.drawBitmap(full,i*width.toFloat(),10.0f,paint)
            }
            if (split[1].toInt()>=5){
                canvas!!.drawBitmap(half,count*width.toFloat(),10.0f,paint)
            }
        }
    }
    override fun onTouchEvent(event: MotionEvent?): Boolean {
        x= event!!.getX()
        flag = true
        postInvalidate()
        return true
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值