57 lines
2.0 KiB
JavaScript
57 lines
2.0 KiB
JavaScript
/**
|
||
* 从 public/icons/icon-512.png 生成 Android 各密度的启动图标
|
||
*
|
||
* 传统图标用完整图,圆形图标裁圆,自适应前景留 10% 余量避免被系统裁切
|
||
*
|
||
* 运行:node scripts/generate-android-icons.mjs
|
||
*/
|
||
import sharp from "sharp";
|
||
|
||
const SOURCE = "public/icons/icon-512.png";
|
||
const RES = "android/app/src/main/res";
|
||
|
||
/** 传统图标边长 */
|
||
const LEGACY = { mdpi: 48, hdpi: 72, xhdpi: 96, xxhdpi: 144, xxxhdpi: 192 };
|
||
|
||
/** 自适应前景边长,108dp 基准 */
|
||
const FOREGROUND = { mdpi: 108, hdpi: 162, xhdpi: 216, xxhdpi: 324, xxxhdpi: 432 };
|
||
|
||
/** 前景内容占画布的比例,低于安全区 66.7%,留余量 */
|
||
const FOREGROUND_RATIO = 0.9;
|
||
|
||
const { data } = await sharp(SOURCE).raw().toBuffer({ resolveWithObject: true });
|
||
const hex = `#${[data[0], data[1], data[2]].map(v => v.toString(16).padStart(2, "0")).join("")}`;
|
||
console.log(`图标底色:${hex}`);
|
||
|
||
for (const [density, size] of Object.entries(LEGACY)) {
|
||
const dir = `${RES}/mipmap-${density}`;
|
||
await sharp(SOURCE).resize(size, size, { fit: "cover" }).png().toFile(`${dir}/ic_launcher.png`);
|
||
|
||
const circle = Buffer.from(
|
||
`<svg width="${size}" height="${size}"><circle cx="${size / 2}" cy="${size / 2}" r="${size / 2}" fill="#fff"/></svg>`
|
||
);
|
||
await sharp(SOURCE)
|
||
.resize(size, size, { fit: "cover" })
|
||
.composite([{ input: circle, blend: "dest-in" }])
|
||
.png()
|
||
.toFile(`${dir}/ic_launcher_round.png`);
|
||
|
||
console.log(`${density}: 传统 ${size},圆形 ${size}`);
|
||
}
|
||
|
||
for (const [density, size] of Object.entries(FOREGROUND)) {
|
||
const inner = Math.round(size * FOREGROUND_RATIO);
|
||
const offset = Math.round((size - inner) / 2);
|
||
const scaled = await sharp(SOURCE).resize(inner, inner, { fit: "cover" }).png().toBuffer();
|
||
await sharp({
|
||
create: { width: size, height: size, channels: 4, background: { r: 0, g: 0, b: 0, alpha: 0 } }
|
||
})
|
||
.composite([{ input: scaled, top: offset, left: offset }])
|
||
.png()
|
||
.toFile(`${RES}/mipmap-${density}/ic_launcher_foreground.png`);
|
||
|
||
console.log(`${density}: 前景 ${size}(内容 ${inner})`);
|
||
}
|
||
|
||
console.log("完成");
|