笔者在Mac下进行开发,UI一般不会具体给到每一个尺寸的图标,自己动手丰衣足食咯。
具体的尺寸,先前的文章有给出,Android开发命名规范及部分图标准则。
原理是利用sips命令将图片居中放缩处理(没有做边距处理,需要原图有边距),涉及简单的 mkdir、awk、xargs 命令。
创建一个makelogo.sh文件,写入下面的代码:
#! /bin/bash
#测试Demo ./makelogo.sh 1 drawable ic_launcher-web.png ic_launcher.png
# 1:APK LOGO 2:通知栏图标 3:ActionBar图标
type=$1
dirname=$2
pic=$3
out=$4
mkdir ./res 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-xxhdpi 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-xhdpi 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-hdpi 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-mdpi 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-ldpi 1>/dev/null 2>/dev/null
echo '原图片信息如下:'
ls -lh $pic
echo '生成图片信息如下:'
if [[ type -eq "1" ]]; then
mkdir ./res/${dirname}-xxxhdpi 1>/dev/null 2>/dev/null
# LOGO
sips $pic -Z 192 --out ./res/${dirname}-xxxhdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 144 --out ./res/${dirname}-xxhdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 96 --out ./res/${dirname}-xhdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 72 --out ./res/${dirname}-hdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 48 --out ./res/${dirname}-mdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 36 --out ./res/${dirname}-ldpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
elif [[ type -eq "2" ]]; then
#Notification Icons
mkdir ./res/${dirname}-xxhdpi-v9 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-xxhdpi-v11 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-xhdpi-v9 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-xhdpi-v11 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-mdpi-v9 1>/dev/null 2>/dev/null
mkdir ./res/${dirname}-mdpi-v11 1>/dev/null 2>/dev/null
sips $pic -z 75 75 -p 75 75 --out ./res/${dirname}-xxhdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 48 48 -p 75 48 --out ./res/${dirname}-xxhdpi-v9/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 72 72 -p 72 72 --out ./res/${dirname}-xxhdpi-v11/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 50 50 -p 50 50 --out ./res/${dirname}-xhdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 32 32 -p 50 32 --out ./res/${dirname}-xhdpi-v9/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 48 48 -p 48 48 --out ./res/${dirname}-xhdpi-v11/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 38 38 -p 38 38 --out ./res/${dirname}-hdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 24 24 -p 38 24 --out ./res/${dirname}-hdpi-v9/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 36 36 -p 36 36 --out ./res/${dirname}-hdpi-v11/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 25 25 -p 25 25 --out ./res/${dirname}-mdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 16 16 -p 25 16 --out ./res/${dirname}-mdpi-v9/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -z 24 24 -p 24 24 --out ./res/${dirname}-mdpi-v11/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
elif [[ type -eq "3" ]]; then
# Actionbar 图标
sips $pic -Z 96 --out ./res/${dirname}-xxhdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 64 --out ./res/${dirname}-xhdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 48 --out ./res/${dirname}-hdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
sips $pic -Z 32 --out ./res/${dirname}-mdpi/$out |awk '{print $1}'|sed -n '2p'|xargs ls -lh
fi
赋予执行权限:
chmod +x ./makelogo.sh
测试前文件如下:
接下来测试一下,创建通知栏图标,一般放在drawable,需要在res的父目录执行此文件,将“actionbar_camera.png”重命名为“noti_camera.png”放入res各级目录中,执行如下命令:
./makelogo.sh 2 drawable actionbar_camera.png noti_camera.png
执行结果:
执行后目录结构: