
在android开发项目中, webp格式的图片在android开发中, 确实有有效的降低apk的体积,但是webp格式是有损压缩, 在项目中是使用webp格式还是png格式,需要自己权衡。
如何使用:
- 将下面的shell脚本代码拷贝到文件,比如为
a.sh
- 执行脚本, sh a.sh 目录名
- 会将给定的目录下面的所有png转换成web
function compressToWeb(){
file=$1
fileName=${file%.*}
echo "${file##*.}"
if [[ ${file##*.} = "png" && ${fileName##*.} != "9" ]]
then
echo $fileName
cwebp -q 90 $2/$1 -o "$2/${fileName}.webp"
rm -f $2/$1
fi
}
function convertToWeb(){
for entry in $(ls $1)
do
if test -d $1/$entry;then
echo "convertToWeb $1/$entry"
convertToWeb $1/$entry
else
str=$1
substr=${str##*/}
echo ${substr}
if [[ ${substr} != "dist" && ${substr} != "paidnode" ]]; then
compressToWeb $entry $1
fi
fi
done
git add .
}
convertToWeb $PWD