如何获取svg图标中的路径 (漫反射图标效果实现)

如何获取一个 SVG 图标中的路径Path

效果图:

Step 1 - 引入 svgknife.aar

链接: https://pan.baidu.com/s/1s-Dr_5zmoho5Rwy4txzbaA 提取码: 9669 复制这段内容后打开百度网盘手机App,操作更方便哦

Step 2 - 自定义个View画解析出来的Path

kotlin 复制代码
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.RawRes
import com.looper.android.svgknife.SvgKnife.SvgPath

class BloomImageView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : View(context, attributeSet) {

    private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
        style = Paint.Style.STROKE
        color = Color.RED
        strokeWidth = 4f
        // 核心是设置一个 ShadowLayer
        setShadowLayer(10f, 0f, 0f, Color.RED)
    }

    private val svgKnife = SvgKnife(paint)

    private var bloomIconPaths = mutableListOf<SvgPath>()

    fun setBloomIcon(@RawRes rawRes: Int) {
        bloomIconPaths.clear()
        svgKnife.load(context, rawRes)
        invalidate()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        if (bloomIconPaths.isEmpty()) {
            bloomIconPaths.addAll(svgKnife.getPathsForViewport(width, height))
        }

        bloomIconPaths.forEach {
            canvas.drawPath(it.path, paint)
        }
    }
}
相关推荐
00后程序员张12 小时前
对比 Ipa Guard 与 Swift Shield 在 iOS 应用安全处理中的使用差异
android·开发语言·ios·小程序·uni-app·iphone·swift
悠哉清闲14 小时前
不同车型drawable不同
android·开发语言
00后程序员张16 小时前
在 iOS 设备上同时监控 CPU、GPU 与内存的方法
android·ios·小程序·https·uni-app·iphone·webview
测试_AI_一辰16 小时前
项目实践笔记 9:打卡/日报Agent项目Bug 修改与稳定性收口(v1.0)
android·开发语言·人工智能·功能测试·ai编程·ab测试
马 孔 多 在下雨16 小时前
Kotlin协程进阶王炸之作-Kotlin的协程到底是什么
android·开发语言·kotlin
冬奇Lab16 小时前
【Kotlin系列15】多平台开发实战:一次编写,多端运行
android·开发语言·kotlin
Dxy123931021617 小时前
告别默认排序:MySQL自定义排序的“炼金术”
android·数据库·mysql
请叫我大虾17 小时前
发现一个jdk中ArrayList的小BUG
android·java·bug
一起养小猫17 小时前
Flutter for OpenHarmony 实战:双控制系统实现(按钮+键盘)
android·flutter·计算机外设·harmonyos
_李小白18 小时前
【Android 美颜相机】第十八天:GPUImageChromaKeyBlendFilter 解析
android·数码相机