自定义View之icon集合

本文介绍了如何使用自定义View来绘制电池、卡车和购物车等图标,详细讲解了每个图标的绘制思路和关键代码,虽然在实际开发中不推荐这种方法,但作为一个学习和实践的过程。文章提供了一个Kotlin和两个Java的实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

此篇主要是关于一些icon图使用自定义view的方式绘制出来
实际开发中不建议这么做,因为影响开发效率,而我写这一篇,纯粹是闲的:)
不涉及适配,部分属性因为全部是在一个view里绘制,所以对于单个而言会有重复.
部分icon地址

效果图

name

1. 电池

思路: Rect+Path+Rect+Text
语言: Kotlin
关键代码:

  override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        rectPaint?.style = Paint.Style.FILL
        resetPaintColor(rectPaint)
        canvas.drawRect(Rect(allStartX + topRectStartX, allStartY, allStartX + topRectX + topRectStartX, topRectY + allStartY), rectPaint!!)   //画实心
        rectPaint?.style = Paint.Style.STROKE
        canvas.drawPath(path!!, rectPaint!!)
        resetPaintColor(textPaint)
        if (completeProgress <= lowBattery) {
            textPaint?.textSize = (rectX * 8 / 5).toFloat()
            canvas.drawText("!", (allStartX + rectX / 2 - baseInside).toFloat(), (rectY - baseInside + allStartY).toFloat(), textPaint!!)
        } else {
            rectPaint?.style = Paint.Style.FILL
            rectPaint?.color = Color.GREEN
            val num = (rectY - 2 * baseInside) * (100 - completeProgress) / 100 + allStartY + topRectY + baseInside
            canvas.drawRect(Rect(allStartX + baseInside, num, allStartX + rectX - baseInside, rectY + topRectY - baseInside + allStartY), rectPaint!!)
        }
        textPaint?.textSize = textSize.toFloat()
        canvas.drawText(completeProgress.toString() + "%", rectX * 1.4f + allStartX, (distanceH / 2 + baseInside * 2).toFloat(), textPaint!!)
    }

2. 卡车

思路: RoundRect+Path+Circle(白色背景)+Circle
语言: Java
关键代码:

 private void drawCar(Canvas canvas) {
        mPaint.setStyle(Paint.Style.STROKE);
        int startPath = startRect + baseWidth;
        canvas.drawRoundRect(new RectF(startRect, startRect, startPath, startPath), 5, 5, mPaint);
        mPath.reset();
        mPath.moveTo(startPath, startRect + baseWidth / 3);
        mPath.lineTo(startPath + baseWidth / 3, startRect + baseWidth / 3);
        mPath.lineTo(startPath + baseWidth / 2, startRect + baseWidth / 5 * 4);
        mPath.lineTo(startPath + baseWidth / 2, startRect + baseWidth);
        mPath.lineTo(startPath, startRect + baseWidth);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
        int raidus = baseWidth / 5;
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(startPath + raidus, startRect + baseWidth, raidus, mPaint);
        canvas.drawCircle(startRect + raidus * 1.5f, startRect + baseWidth, raidus, mPaint);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setColor(ContextCompat.getColor(getContext(), R.color.colorPrimaryDark));
        canvas.drawCircle(startPath + raidus, startRect + baseWidth, raidus, mPaint);
        canvas.drawCircle(startRect + raidus * 1.5f, startRect + baseWidth, raidus, mPaint);
    }

3. 购物车

思路: Path+Path+Circle
语言: Java
关键代码:

 private void drawShopCart(Canvas canvas) {
        int y = startRect ;
        int x = startRect + baseWidth * 3;
        mPath.reset();
        mPath.moveTo(x, y);
        mPath.lineTo(x + baseWidth / 4, y);
        mPath.lineTo(x + baseWidth / 2, y + baseWidth);
        mPath.lineTo(x + baseWidth / 4, y + baseWidth / 4 * 5);
        mPath.lineTo(x + baseWidth / 4 * 5, y + baseWidth / 4 * 5);
        canvas.drawPath(mPath, mPaint);
        mPath.reset();
        mPath.moveTo(x + baseWidth / 4 + 5, y + baseWidth / 12 * 5);
        mPath.lineTo(x + baseWidth * 5 / 4, y + baseWidth / 12 * 5);
        mPath.lineTo(x + baseWidth, y + baseWidth);
        mPath.lineTo(x + baseWidth / 2, y + baseWidth);
        mPath.close();
        canvas.drawPath(mPath, mPaint);
        int raidus = baseWidth / 8;
        mPaint.setStyle(Paint.Style.FILL);
        canvas.drawCircle(x + baseWidth / 4 * 5 - raidus, y + baseWidth / 4 * 5 + raidus * 2, raidus, mPaint);
        canvas.drawCircle(x + baseWidth / 2 - raidus, y + baseWidth / 4 * 5 + raidus * 2, raidus, mPaint);
    }

最后

Github 项目地址
有不对的地方欢迎留言指正,不胜感激.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值