Android @1x,@2x,@3x 资源文件自动分包工具

时间:2021-12-13 17:04:09

version 1.2 
1.修改不用输入扩展名 
2.输出路径可选。默认会在输入路径下建文件夹

前沿:

现在开发中ios,android会使用一套图,但是ui设计师给的图命名是以@1x,@2x,@3x这样命名的,android 客户端使用起来就略嫌麻烦了,这个小工具可以实现简单的分包。

原理:

I/o流读取 testPicture中的@1x,@2x,@3x 文件进行整理,按序输出旧文件文字同时,需要输入一个新文件名 + 后缀名,然后动态进行分组到desPicture中的文件夹1,文件夹2,文件夹3。1,2,3文件夹分别对应@1x,@2x,@3x.

Eg:尽量配对出现 @1x,@2x,@3x.

1.首先需要两个文件夹,一个里面存放着待处理的文件,另一个存放处理后的文件

Android @1x,@2x,@3x 资源文件自动分包工具

testPicture

Android @1x,@2x,@3x 资源文件自动分包工具

desPicture

Android @1x,@2x,@3x 资源文件自动分包工具

步骤:

1.进入dos窗口

Window+r —–>cmd

2.进入mutiFile.jar 根目录 输入

java -jar MutiFile-x.x.x.jar srcFile outputDir

其中:MutiFile-x.x.x.jar ,是工具类 
srcFile 为待处理文件夹路径 
outputDir 为处理后的文件输出路径

Android @1x,@2x,@3x 资源文件自动分包工具

处理后的文件,已经进行分组成 @1x,@2x,@3x

Android @1x,@2x,@3x 资源文件自动分包工具

贴出来源码:

package com.nuoyuan.mutiFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner; /**
* version 1.2
* 1. 增加支持 不写后缀名字
* 2. 增加输入输出路径可选
* @author weichyang
*
*/ public class MutiFile { private static final String USAGE_TEXT = "Usage: java -jar MutiFile-x.x.x.jar srcFile outputDir "; private static Map<String, String> onePlus = new HashMap();
private static Map<String, String> twoPlus = new HashMap();
private static Map<String, String> threePlus = new HashMap(); private static List newFilePath = new ArrayList<String>(); private static String[] spiltName = { "@1x", "@2x", "@3x" }; private static ArrayList<String> filelist = new ArrayList<String>();
private static String tempFileName = "xx@xx";
private static String beforeName = "&6%"; public static void main(String[] args) throws IOException { if (args.length < ) {
println(USAGE_TEXT);
return;
}
String resPathString = "";
String desPathString = "";
if (args.length == ) {
resPathString = args[];
desPathString = args[];
} else {
resPathString = args[];
desPathString = args[];
} File resFile = new File(resPathString);
File desFile = new File(desPathString); if (!resFile.exists()) {
println(" file '" + desPathString
+ "' is not exists or not readable.");
println(USAGE_TEXT);
System.exit();
} if (!desFile.exists()) {
// desfile.createNewFile();
println(" file '" + desFile.getAbsolutePath()
+ "' is not exists or not readable.");
println(USAGE_TEXT);
System.exit();
return;
} else {
for (int i = ; i <= ; i++) {
String newPathString = desPathString + "\\" + i;
new File(newPathString).mkdirs();
newFilePath.add(newPathString + "\\");
}
} // 打印资源文件
println("res File: " + resFile.getAbsolutePath());
println("output File: " + desFile.getAbsolutePath()); // 资源存在是否为null
getFiles(resFile);
} /*
* 通过递归得到某一路径下所有的目录及其文件
*/
public static void getFiles(File filePath) throws FileNotFoundException,
IOException {
File[] files = filePath.listFiles();
if (files.length == ) {
println(filePath + " is have't file exit!!");
System.exit();
return;
}
for (File file : files) {
if (file.isDirectory()) {
/*
* 递归调用
*/
getFiles(file);
filelist.add(file.getAbsolutePath());
System.out.println("show " + filePath + "下所有子目录及其文件"
+ file.getAbsolutePath());
} else {
String currentFilePath = file.getAbsolutePath().toString();
String fileName = splitFileName(currentFilePath);
System.out.println("current file name is:" + fileName); if (!fileName.contains("@")) {
continue;
} if (fileName.contains(spiltName[])) { onePlus.put(fileName, currentFilePath);
String fileNameString = InputCode(fileName);
fromToAnotherDes(currentFilePath, newFilePath.get()
.toString() + fileNameString);
} else if (fileName.contains(spiltName[])) { twoPlus.put(fileName, newFilePath.get().toString()
+ fileName);
String fileNameString = InputCode(fileName);
fromToAnotherDes(currentFilePath, newFilePath.get()
.toString() + fileNameString);
} else if (fileName.contains(spiltName[])) { threePlus.put(fileName, newFilePath.get().toString()
+ fileName);
String fileNameString = InputCode(fileName);
fromToAnotherDes(currentFilePath, newFilePath.get()
.toString() + fileNameString); } } } System.out.print("/****** File rename Success *********/\n"); System.out.print("共修改@1x 文件 " + onePlus.size() + "个\n");
System.out.print("共修改@2x 文件 " + twoPlus.size() + "个\n");
System.out.print("共修改@3x 文件 " + threePlus.size() + "个\n");
System.out.print("总共修文件 " + threePlus.size() + onePlus.size()
+ twoPlus.size() + "个\n"); System.out.print("/**************************************/\n"); } /**
* 分割文件名字
*
* @param str
* @return
*/
public static String splitFileName(String str) {
String[] strs = str.split("\\\\");
StringBuffer sb = new StringBuffer();
for (int i = ; i < strs.length; i++) {
if (i == strs.length - )
sb.append(strs[i]);
}
return sb.toString();
} /**
* 拷貝一個文件到另一個問價中
*
* @param srcFile
* @param desFile
* @throws FileNotFoundException
* @throws IOException
*/
public static void fromToAnotherDes(String srcFile, String desFile) { try {
FileInputStream fis = new FileInputStream(srcFile); FileOutputStream fos = new FileOutputStream(desFile); int len = ;
byte[] buf = new byte[];
while ((len = fis.read(buf)) != -) {
fos.write(buf, , len);
}
fis.close();
fos.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /**
* 接受输入的数字进行设置文件名字 一次输入使用三次
*
* @return
*/
public static String InputCode(String fileName) {
String extNameString = getExtension(fileName);
String realNameString = fileName.split("@")[]; if (realNameString.equals(beforeName)) {
return tempFileName + extNameString;
}
Scanner scanner = new Scanner(System.in);// 创建输入流扫描器
System.out.print("please input file name:\n");// 提示用户输入
// 获取用户输入的一行文本
String line = scanner.nextLine();
// 打印对输入文本的描述
tempFileName = line;
beforeName = realNameString;
return line + extNameString; } public static void println(String msg) {
System.out.println(msg);
} /**
* 截取扩展名字
*
* @param filename
* @param defExt
* @return
*/
public static String getExtension(String filename) { if ((filename != null) && (filename.length() > )) { int i = filename.lastIndexOf('.'); if ((i > -) && (i < (filename.length() - ))) { return filename.substring(i); }
}
return "";
} }

下载地址 http://pan.baidu.com/s/1pK7qdLp

version 1.2 版本:http://pan.baidu.com/s/1boNnLoV