在做打包自动化的过程中需要将有效的launcher加上渠道的角标如下所示:

使用代码将合成过程自动化
public class MaskIcon {
private static String sourceName;
// sourcePath sourceName waterMarkFile
// c:/res/ ic_launcher.png xxhdpi.png
public static void main(String[] args) throws IOException {
String sourcePath = args[0];
sourceName = args[1];
String waterMarkFile = args[2];
File ldpiFile = new File(sourcePath + "drawable-ldpi-v4" + File.separator
+ sourceName);
File mdpiFile = new File(sourcePath + "drawable-mdpi-v4" + File.separator
+ sourceName);
File hdpiFile = new File(sourcePath + "drawable-hdpi-v4" + File.separator
+ sourceName);
File xhdpiFile = new File(sourcePath + "drawable-xhdpi-v4" + File.separator
+ sourceName);
File xxhdpiFile = new File(sourcePath + "drawable-xxhdpi-v4"
+ File.separator + sourceName);
File waterMark = new File(waterMarkFile);
if(ldpiFile.exists()) {
mergeImage(ldpiFile, waterMark);
}
mergeImage(mdpiFile, waterMark);
mergeImage(hdpiFile, waterMark);
mergeImage(xhdpiFile, waterMark);
mergeImage(xxhdpiFile, waterMark);
}
public static void mergeImage(File source, File waterMark)
throws IOException {
BufferedImage biSource = ImageIO.read(source);
int sourceWidth = biSource.getWidth();
int sourceHeight = biSource.getHeight();
BufferedImage biWaterMark = ImageIO.read(waterMark);
// 将水印按照源图片大小缩放
Image waterMarkSizeLikeSource = biWaterMark.getScaledInstance(
sourceWidth, sourceHeight, 1);
BufferedImage combined = new BufferedImage(sourceWidth, sourceHeight,
BufferedImage.TYPE_INT_ARGB);
Graphics g = combined.getGraphics();
g.drawImage(biSource, 0, 0, sourceWidth, sourceHeight, null);
g.drawImage(waterMarkSizeLikeSource, 0, 0, sourceWidth, sourceHeight,
null);
g.dispose();// 释放资源
ImageIO.write(combined, "PNG", new File(source.getParent(), sourceName));
}
}

如图所示传入三个参数, 源图片的路径,源图片名, 角标图片,生成图片效果如下


打包成 mask.jar 在windows 下面执行

Linux 下面执行

网友评论