如何获取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)
        }
    }
}
相关推荐
非专业程序员Ping4 小时前
HarfBuzz概览
android·ios·swift·font
Jeled5 小时前
「高级 Android 架构师成长路线」的第 1 阶段 —— 强化体系与架构思维(Clean Architecture 实战)
android·kotlin·android studio·1024程序员节
明道源码7 小时前
Kotlin 控制流、函数、Lambda、高阶函数
android·开发语言·kotlin
消失的旧时光-19439 小时前
Kotlin × Gson:为什么遍历 JsonObject 要用 entrySet()
android·kotlin·数据处理·1024程序员节
G果11 小时前
安卓APP页面之间传参(Android studio 开发)
android·java·android studio
曾凡宇先生12 小时前
无法远程连接 MySQL
android·开发语言·数据库·sql·tcp/ip·mysql·adb
zhangphil14 小时前
Android GPU的RenderThread Texture upload上传Bitmap优化prepareToDraw
android
柿蒂15 小时前
聊聊SliverPersistentHeader优先消费滑动的设计
android·flutter
假装多好12316 小时前
android三方调试几个常用命令
android·1024程序员节·三方,gms
侧耳42916 小时前
android11禁止安装apk
android·java·1024程序员节