} finally {
try {
if (fr != null) {
fr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
- 直接拼接出Adapter
*/
private static String contactAdapter(File file, File out, String name, HashMap<String, String> map) {
StringBuilder sb = new StringBuilder();
String path = out.getAbsolutePath();
path.split(“java”);
sb.append(“package " + path.split(“java\\”)[1].replaceAll(”\\", “.”) + “;\n”);
sb.append(“import android.content.Context;\n” +
“import android.support.annotation.NonNull;\n” +
“import android.support.constraint.ConstraintLayout;\n” +
“import android.support.v7.widget.RecyclerView;\n” +
“import android.view.LayoutInflater;\n” +
“import android.view.View;\n” +
“import android.view.ViewGroup;\n” +
“import android.widget.Button;\n” +
“import java.util.List;\n” +
“import android.widget.TextView;\n”);
sb.append(“public class " + name + " extends RecyclerView.Adapter<” + name + “.MyViewHolder> {\n”);
sb.append(“private Context mContext;\n”);
sb.append(“private List mData;\n”);
sb.append(“public " + name + “(List data) {\n” +
" mData = data;\n” +
“}”);
String layoutId = file.getName().substring(0, file.getName().indexOf(".x"));
sb.append("@NonNull\n" +
“@Override\n” +
“public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {\n” +
“mContext = parent.getContext();\n” +
" View view = LayoutInflater.from(mContext).inflate(R.layout." + layoutId + “, parent, false);\n” +
" return new MyViewHolder(view);\n" +
“}\n”);
sb.append("@Override \n" +
“public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {\n” +
“String str = mData.get(position);”
);
map.forEach((id, view) -> {
if (view.contains("\n")) {
view = view.split("\n")[0];
}
if (“Button”.equals(view)) {
sb.append(“holder.” + formatId2Field(id) + “.setOnClickListener(v -> {\n” +
" });\n");
}
if (“TextView”.equals(view)) {
sb.append(“holder.” + formatId2Field(id) + “.setText(str);\n”);
}
if (“ImageView”.equals(view)) {
sb.append(“holder.” + formatId2Field(id) + “.setImageBitmap(null);\n”);
}
});
sb.append("}\n" +
“@Override\n” +
“public int getItemCount() {\n” +
“return mData.size();\n” +
“}”);
sb.append(contactViewHolder(map));
return sb.toString();
}
/**
- 连接字符串:ViewHolder
*/
private static String contactViewHolder(HashMap<String, String> map) {
StringBuilder sb = new StringBuilder();
sb.append(“class MyViewHolder extends RecyclerView.ViewHolder {\r\n”);
map.forEach((id, view) -> {
if (view.contains("\n")) {
view = view.split("\n")[0];
}
sb.append(“public “).append(view).append(” “)
.append(formatId2Field(id)).append(”;”).append("\r\n");
});
sb.append(“public MyViewHolder(View itemView) {\n” +
“super(itemView);”);
map.forEach((id, view) -> {
sb.append(formatId2Field(id))
.append("= itemView.findViewById(R.id.")
.append(id).append(");").append("\r\n");
});
sb.append("}\n" +
“}\n}”);
return sb.toString();
}
private static String formatId2Field(String id) {
if (id.contains("")) {
String[] partStrArray = id.split("");
id = “”;
for (String part : partStrArray) {
String partStr = upAChar(part);
id += partStr;
}
}
return “m” + id;
}
/**
- 将字符串仅首字母大写
- @param str 待处理字符串
- @return 将字符串仅首字母大写
*/
public static String upAChar(String str) {
String a = str.substring(0, 1);
String tail = str.substring(1);
return a.toUpperCase() + tail;
}
private static HashMap<String, String> split(String res) {
String[] split = res.split("<");
HashMap<String, String> viewMap = new HashMap<>();
for (String s : split) {
if (s.contains(“android:id=”@+id") && !s.contains(“Guideline”)) {
String id = s.split("@")[1];
id = id.substring(id.indexOf("/") + 1, id.indexOf("""));
String view = s.split("\r\n")[0];
String[] viewNameArr = view.split("\.");
if (viewNameArr.length > 0) {
view = viewNameArr[viewNameArr.length - 1];
}
viewMap.put(id, view);
}
}
return viewMap;
}
}
2.Attrs2View.java
/**
- 作者:张风捷特烈
- 时间:2018/10/31 0031:8:47
- 邮箱:1981462002@qq.com
- 说明:安卓自定义属性,代码生成器
*/
public class Attrs2View {
@Test
public void main() {
//你的attr.xml所在路径
File file = new File(“I:\Java\Android\Unit\B\test\src\main\res\values\attrs.xml”);
//你的Adapter的java类放在哪个包里
File out = new File(“I:\Java\Android\Unit\B\asyn\src\main\java\com\toly1994\app”);
String preFix = “z_”;
//你的前缀
initAttr(preFix, file, out);
}
public static void initAttr(String preFix, File file, File out) {
HashMap<String, String> format = format(preFix, file);
String className = format.get(“className”);
String result = format.get(“result”);
StringBuilder sb = initTop(out, className, result);
sb.append(“TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.” + className + “);\r\n”);
format.forEach((s, s2) -> {
String styleableName = className + “" + preFix + s;
if (s.contains("”)) {
String[] partStrArray = s.split("_");
s = “”;
for (String part : partStrArray) {
String partStr = upAChar(part);
s += partStr;
}
}
if (s2.equals(“dimension”)) {
// mPbBgHeight = (int) a.getDimension(R.styleable.TolyProgressBar_z_pb_bg_height, mPbBgHeight);
sb.append(“m” + s + " = (int) a.getDimension(R.styleable." + styleableName + “, m” + s + “);\r\n”);
}
if (s2.equals(“color”)) {
// mPbTxtColor = a.getColor(R.styleable.TolyProgressBar_z_pb_txt_color, mPbTxtColor);
sb.append(“m” + s + " = a.getColor(R.styleable." + styleableName + “, m” + s + “);\r\n”);
}
if (s2.equals(“boolean”)) {
// mPbTxtColor = a.getColor(R.styleable.TolyProgressBar_z_pb_txt_color, mPbTxtColor);
sb.append(“m” + s + " = a.getBoolean(R.styleable." + styleableName + “, m” + s + “);\r\n”);
}
if (s2.equals(“string”)) {
// mPbTxtColor = a.getColor(R.styleable.TolyProgressBar_z_pb_txt_color, mPbTxtColor);
sb.append(“m” + s + " = a.getString(R.styleable." + styleableName + “);\r\n”);
}
});
sb.append(“a.recycle();\r\n”);
sb.append(“init();\n” +
" }");
sb.append(“private void init() {\n” +
“\n” +
" }\n}");
System.out.println(sb.toString());
FileWriter fw = null;
try {
//写出到磁盘
File outFile = new File(out, className + “.java”);
fw = new FileWriter(outFile);
fw.write(sb.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
*
- @param out
- @param name
*/
private static StringBuilder initTop(File out, String name, String field) {
StringBuilder sb = new StringBuilder();
String path = out.getAbsolutePath();
path.split(“java”);
sb.append(“package " + path.split(“java\\”)[1].replaceAll(”\\", “.”) + “;\n”);
sb.append(“public class " + name + " extends View {\n”);
sb.append(field);
sb.append(“public " + name + “(Context context) {\n” +
" this(context, null);\n” +
“}\n” +
“public " + name + “(Context context, AttributeSet attrs) {\n” +
" this(context, attrs, 0);\n” +
“}\n” +
“public " + name + “(Context context, AttributeSet attrs, int defStyleAttr) {\n” +
" super(context, attrs, defStyleAttr);\n”);
return sb;
}
/**
- 读取文件+解析
- @param preFix 前缀
- @param file 文件路径
*/
public static HashMap<String, String> format(String preFix, File file) {
HashMap<String, String> container = new HashMap<>();
if (!file.exists() && file.isDirectory()) {
return null;
}
FileReader fr = null;
try {
fr = new FileReader(file);
//字符数组循环读取
char[] buf = new char[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = fr.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
String className = sb.toString().split("<declare-styleable name="")[1];
className = className.substring(0, className.indexOf("">"));
container.put(“className”, className);
String[] split = sb.toString().split("<");
String part1 = “private”;
String type = “”;//类型
String name = “”;
String result = “”;
String def = “”;//默认值
StringBuilder sb2 = new StringBuilder();
for (String s : split) {
if (s.contains(preFix)) {
result = s.split(preFix)[1];
name = result.substring(0, result.indexOf("""));
type = result.split(“format=”")[1];
type = type.substring(0, type.indexOf("""));
container.put(name, type);
if (type.contains(“color”) || type.contains(“dimension”) || type.contains(“integer”)) {
type = “int”;
def = “0”;
}
if (result.contains(“fraction”)) {
type = “float”;
def = “0.f”;
}
if (result.contains(“string”)) {
type = “String”;
def = ““toly””;
}
if (result.contains(“boolean”)) {
type = “boolean”;
def = “false”;
}
if (name.contains("")) {
String[] partStrArray = name.split("");
name = “”;
for (String part : partStrArray) {
String partStr = upAChar(part);
name += partStr;
}
sb2.append(part1 + " " + type + " m" + name + "= " + def + “;\r\n”);
}
container.put(“result”, sb2.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return container;
}
/**
- 将字符串仅首字母大写
- @param str 待处理字符串
- @return 将字符串仅首字母大写
*/
public static String upAChar(String str) {
String a = str.substring(0, 1);
String tail = str.substring(1);
return a.toUpperCase() + tail;
}
}
3.Svg2Xml.java
/**
- 作者:张风捷特烈
- 时间:2018/10/31 0031:8:47
- 邮箱:1981462002@qq.com
- 说明:svg图标转换为Android可用xml生成器
*/
public class Svg2Xml {
@Test
public void svgDir() {
String dirPath = “E:\Material\MyUI\#svg\factory”;
svg2xmlFromDir(dirPath);
}
@Test
public void singleSvg() {
File file = new File(“C:\Users\Adm
inistrator\Desktop\dao.svg”);
svg2xml(file);
}
/**
- 将一个文件夹里的所有svg转换为xml
- @param filePath
*/
public static void svg2xmlFromDir(String filePath) {
File file = new File(filePath);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
if (f.getName().endsWith(".svg")) {
System.out.println(f);
svg2xml(f);
}
}
} else {
svg2xml(file);
}
}
/**
- 将.svg文件转换为安卓可用的.xml
- @param file 文件路径
*/
public static void svg2xml(File file) {
if (!file.exists() && file.isDirectory()) {
return;
}
FileWriter fw = null;
FileReader fr = null;
ArrayList paths = new ArrayList<>();
try {
fr = new FileReader(file);
//字符数组循环读取
char[] buf = new char[1024];
int len = 0;
StringBuilder sb = new StringBuilder();
while ((len = fr.read(buf)) != -1) {
sb.append(new String(buf, 0, len));
}
//收集所有path
collectPaths(sb.toString(), paths);
//拼接字符串
StringBuilder outSb = contactStr(paths);
//写出到磁盘
File outFile = new File(file.getParentFile(), file.getName().substring(0, file.getName().lastIndexOf(".")) + “.xml”);
fw = new FileWriter(outFile);
fw.write(outSb.toString());
System.out.println(“转换成功:” + file.getName());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();
}
if (fr != null) {
fr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
- 拼接字符串
- @param paths
- @return
*/
private static StringBuilder contactStr(ArrayList paths) {
StringBuilder outSb = new StringBuilder();
outSb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
“<vector xmlns:android=“http://schemas.android.com/apk/res/android”\n” +
" android:width=“48dp”\n" +
" android:height=“48dp”\n" +
" android:viewportWidth=“1024”\n" +
" android:viewportHeight=“1024”>\n");
for (String path : paths) {
aths - @return
*/
private static StringBuilder contactStr(ArrayList paths) {
StringBuilder outSb = new StringBuilder();
outSb.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
“<vector xmlns:android=“http://schemas.android.com/apk/res/android”\n” +
" android:width=“48dp”\n" +
" android:height=“48dp”\n" +
" android:viewportWidth=“1024”\n" +
" android:viewportHeight=“1024”>\n");
for (String path : paths) {