当前位置: 首页 > news >正文

使用js脚本自动生成android项目的app icon图标

因为我的一个项目是使用GitHub action来实现编译和部署的,而且这个android项目的icon很有可能会发生变化,所以需要在脚本中实现自动化生成icon的功能,所以这里就写了一个js脚本来做这个事情

这个脚本可以输入一个图片地址,然后生成android项目的icon图标。

脚本文件内容:

const sharp = require('sharp')
const fs = require('fs-extra')
const path = require('path')
const { program } = require('commander')

const DENSITIES = {
    mdpi: 48,
    hdpi: 72,
    xhdpi: 96,
    xxhdpi: 144,
    xxxhdpi: 192,
}

program
    .requiredOption('-i, --input <file>', 'Path to source PNG')
    .option('-o, --output <dir>', 'Output directory', 'res')
    .option('--rounded', 'Apply circular mask to icons')
    .option('--copy-to <androidResDir>', 'Copy icons to Android res directory')

program.parse(process.argv)
const options = program.opts()

async function generateAdaptiveIcons(input, outputDir) {
    for (const [dpi, size] of Object.entries(DENSITIES)) {
        const mipmapDir = path.join(outputDir, `mipmap-${dpi}`)
        await fs.ensureDir(mipmapDir)
        const foregroundPath = path.join(
            mipmapDir,
            'ic_launcher_foreground.png'
        )
        const backgroundPath = path.join(
            mipmapDir,
            'ic_launcher_background.png'
        )
        const legacyPath = path.join(mipmapDir, 'ic_launcher.png')

        const img = sharp(input).resize(size, size)
        const rounded = options.rounded
            ? img.composite([
                  {
                      input: Buffer.from(
                          `<svg><circle cx="${size / 2}" cy="${size / 2}" r="${
                              size / 2
                          }" fill="white"/></svg>`
                      ),
                      blend: 'dest-in',
                  },
              ])
            : img

        await rounded.toFile(foregroundPath)
        await sharp({
            create: {
                width: size,
                height: size,
                channels: 4,
                background: '#FFFFFF',
            },
        })
            .png()
            .toFile(backgroundPath)

        // legacy fallback
        await rounded.toFile(legacyPath)
    }

    // Generate XML
    const xmlPath = path.join(outputDir, 'mipmap-anydpi-v26')
    await fs.ensureDir(xmlPath)
    await fs.writeFile(
        path.join(xmlPath, 'ic_launcher.xml'),
        `
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@mipmap/ic_launcher_background"/>
    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
  `.trim()
    )

    await fs.writeFile(
        path.join(xmlPath, 'ic_launcher_round.xml'),
        `
<adaptive-icon xmlns:android="http://schemas.android.com/apk/res/android">
    <background android:drawable="@mipmap/ic_launcher_background"/>
    <foreground android:drawable="@mipmap/ic_launcher_foreground"/>
</adaptive-icon>
  `.trim()
    )

    console.log('✅ Adaptive icons generated.')
}

;(async () => {
    const { input, output, copyTo } = options
    const outPath = path.resolve(output)
    await generateAdaptiveIcons(input, outPath)

    if (copyTo) {
        const dest = path.resolve(copyTo)
        await fs.copy(outPath, dest, { overwrite: true })
        console.log(`📦 Icons copied to Android res dir: ${dest}`)
    }
})()

使用说明:

node ./generateIcons.cjs -i pakeplus.png --copy-to app/src/main/res

生成圆角图标:

node ./generateIcons.cjs -i pakeplus.png --rounded --copy-to app/src/main/res

最后生成的结果:

相关文章:

  • UML综合实验四
  • 【Linux】进程控制:创建、终止、等待与替换全解析
  • TurtleBot3 Package turtlebot3_drive source code read
  • web前端: 什么是web?
  • Linux shell脚本编程
  • <数据集>苹果识别数据集<目标检测>
  • Python标准库:sys模块深入解析
  • 增长黑客:技术与业务融合,驱动业务增长
  • 电商核心指标解析与行业趋势:数据驱动的增长策略【大模型总结】
  • ZeroLogon(CVE-2020-1472)漏洞复现
  • [蓝桥杯 2022 省 B] 李白打酒加强版
  • 工业制造各个系统术语
  • 提升Windows安全的一些措施
  • 死锁 手撕死锁检测工具
  • DDR中的delay line
  • doris基础使用
  • 汽车CAN总线采样点和采样率详解
  • openEuler-22.03-LTS-SP3-x86_64 离线编译安装 nginx 1.20.1
  • Linux 学习笔记(5)路径知识详解:绝对路径、相对路径与特殊路径符(期末、期中复习必备)
  • 对比 HashMap 和 ConcurrentHashMap 扩容逻辑的差异
  • 福田附件网站建设公司/今天重大新闻头条新闻军事
  • 苏州做网站费用/注册推广
  • 信誉好的广州做网站/怎样建立一个网络销售平台
  • 如何在电商网站做市场调研/企业网站seo贵不贵
  • 微网站有什么用/珠海网站seo
  • asp.net 网站安全/百度上怎么发布信息啊