如何获取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)
        }
    }
}
相关推荐
成都大菠萝1 天前
Android Car CarProperty 车辆信号链路
android
敲代码的鱼1 天前
PDF 预览与签名批注写回 支持安卓 iOS 鸿蒙 UTS插件
android·前端·ios
时光足迹1 天前
uni-app 视频通话实战:康复师与患者视频问诊的 6 个致命 Bug 与解决方案
android·ios·uni-app
Coffeeee1 天前
闲聊几句,Android老哥们,你们多久没做技改需求了
android·程序员·代码规范
萝卜er1 天前
Fragment 生命周期与状态恢复-《Android深水区(四)》
android
萝卜er1 天前
Intent 显式、隐式与 PendingIntent-《Android深水区(五)》
android
Kapaseker2 天前
一文吃透 Kotlin 集合操作符
android·kotlin
三少爷的鞋2 天前
Main-safe:现代Android 架构真正的分水岭
android
沐怡旸2 天前
深入解析 Android Performance Analyzer (APA) 底层架构与技术原理
android
李斯维2 天前
从历史的角度看 Android 软件架构
android·架构·android jetpack