如何获取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)
        }
    }
}
相关推荐
2501_916007471 小时前
前端开发工具都有哪些?常用前端开发工具清单与场景化推荐
android·ios·小程序·https·uni-app·iphone·webview
2501_915909064 小时前
iOS 应用上架全流程解析,苹果应用发布步骤、ipa 上传工具、TestFlight 测试与 App Store 审核经验
android·macos·ios·小程序·uni-app·cocoa·iphone
路上^_^4 小时前
安卓基础组件024-fagment
android
ljt27249606614 小时前
Compose笔记(五十一)--rememberTextMeasurer
android·笔记·android jetpack
阿蓝8585 小时前
Android代码架构
android
ZFJ_张福杰11 小时前
【Flutter】GetX最佳实践与避坑指南
android·flutter·ios·getx
一直向钱18 小时前
android 基于okhttp的socket封装
android·okhttp
夜晚中的人海19 小时前
【C++】异常介绍
android·java·c++
蜀中廖化1 天前
Android Studio 导入 opencv
android·opencv·android studio
奋斗的小鹰1 天前
ASM Bytecode Viewer 插件查看kotlin和java文件的字节码
android·kotlin·asm