如何获取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)
        }
    }
}
相关推荐
YB13758 小时前
jetpack compose 副作用 SideEffect
android·kotlin
CHB10 小时前
uni-app x 蒸汽模式 性能测试基准报告 Benchmark【Android版】
android·ios·uni-app
带娃的IT创业者11 小时前
拆掉那堵墙:LibrePods 如何让 AirPods 在 Android 上重获新生
android·开源项目·airpods·librepods·生态解锁
_阿南_13 小时前
flutter 展示的字体突然奔放了
android·flutter·ios
文人sec15 小时前
MySQL事务与索引做了什么?
android·笔记·mysql
刘洋浪子16 小时前
AndroidStudio保存日志菜单
android
朱涛的自习室16 小时前
从 Prompt 到 Graph:AI 工程的进化史
android·前端·人工智能
2501_9327502617 小时前
Android 中 Serializable 与 Parcelable 的对比与选择
android
FungLeo18 小时前
Flutter 两个反直觉布局坑:ListTile 水波纹 / VerticalDivider 踩坑实录
android·flutter
1570925113418 小时前
ViewBinding与DataBinding区别与选型
android