1.RecyclerView的Adapter自动生成器(含ViewHolder)
2.自定义属性的自定义View代码生成器(含自定义属性的初始化)
3.svg图标转换为Android可用xml生成器
最近喜欢切割字符串,这三个类是近期的作品,感觉挺好用的,在此分享一下
三个工具都会贴在本文末尾,本文末尾,本文末尾
最近写了几个RecyclerView的Adapter,控件一多ViewHolder写起来感觉挺不爽的
也感觉其中也只是布局文件里的几个id和View的类型有区别,写个工具读一下xml自动生成一下呗
既然ViewHolder自动生成了,顺便吧Adapter也一起生成算了,反正初始也就那一大段
1.把工具类拷贝到test包里
2.写上你xml的路径和生成的.java所在的包
3.点击运行,就可以生成了。如果有问题,欢迎指出
点一下,就生成这么多,一个一个敲怎么也要五分钟吧,这种枯燥的工作,还是留给计算机吧。
之后根据自己的业务需求,小修补一下就行了。
虽然喜欢用butterknife,但感觉每次加依赖好麻烦,也就是想找个id,也没必要引个库,毕竟都占空间的。
这可谓我的得意之作,本人比较喜欢自定义控件,但自定义属相写起来费心费力,也没什么含量
基本上也就那么几个属性在变,一咬牙,写个工具类吧,然后就有了下文:
1.把工具类拷贝到test包里
2.写上你xml的路径和生成的.java所在的包,写上你的专属前缀
3.点击运行,就可以生成了。如果有问题,欢迎指出注意点
:自定义属性必须有自己的专属前缀(任意字符都可以,统一即可)
和上面一样,将所有svg放在一个文件夹里,即可批处理
/**
* 作者:张风捷特烈
* 时间:2018/10/31 0031:8:47
* 邮箱:[email protected]
* 说明:初始化RecyclerView的Adapter
*/
public class Xml2Adapter {
@Test
public void main() {
//你的布局xml所在路径
File file = new File("I:\\Java\\Android\\Unit\\B\\asyn\\src\\main\\res\\layout\\item_list_pic.xml");
//你的Adapter的java类放在哪个包里
File out = new File("I:\\Java\\Android\\Unit\\B\\asyn\\src\\main\\java\\com\\toly1994\\app");
//你的Adapter的名字--不要加.java
String name = "MyRVAdapter";
initView(file, out, name);
}
@Test
public void findView() {
//你的布局xml所在路径
File file = new File("I:\\Java\\Android\\Unit\\B\\asyn\\src\\main\\res\\layout\\item_list_pic.xml");
findViewById(file);
}
private void findViewById(File in) {
String res = readFile(in);
HashMap map = split(res);
StringBuilder sb = new StringBuilder();
map.forEach((id, view) -> {
sb.append("public ").append(view).append(" ").append(formatId2Field(id)).append(";").append("\r\n");
});
sb.append("\n\n");
map.forEach((id, view) -> {
sb.append(formatId2Field(id))
.append("= itemView.findViewById(R.id.")
.append(id).append(");").append("\r\n");
if ("Button".equals(view)) {
sb.append(formatId2Field(id) + ".setOnClickListener(v -> {\n" +
" });\n");
}
});
System.out.println(sb.toString());
}
/**
* 读取文件
*
* @param in xml文件路径
* @param out 输出的java路径
* @param name
*/
private static void initView(File in, File out, String name) {
FileWriter fw = null;
try {
HashMap map = split(readFile(in));
String result = contactAdapter(in, out, name, map);
//写出到磁盘
File outFile = new File(out, name + ".java");
fw = new FileWriter(outFile);
fw.write(result);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (fw != null) {
fw.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 读取文件
*
* @param in
* @return
*/
private static String readFile(File in) {
if (!in.exists() && in.isDirectory()) {
return "";
}
FileReader fr = null;
try {
fr = new FileReader(in);
//字符数组循环读取
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));
}
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return "";
} finally {
try {
if (fr != null) {
fr.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 直接拼接出Adapter
*/
private static String contactAdapter(File file, File out, String name, HashMap map) {
StringBuilder sb = new StringBuilder();
String path = out.getAbsolutePath();
path.split("java");
sb.append("package " + path.split("java\\\\")[1].replaceAll("\\\\", ".") + ";");
sb.append("\npublic class " + name + " extends RecyclerView.Adapter {\n");
sb.append("private Context mContext;\n");
sb.append("private List mData;\n");
sb.append("public MyRVAdapter(Context context, List data) {\n" +
" mContext = context;\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" +
" 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");
map.forEach((id, view) -> {
if ("Button".equals(view)) {
sb.append("holder." + formatId2Field(id) + ".setOnClickListener(v -> {\n" +
" });\n");
}
if ("TextView".equals(view)) {
sb.append("holder." + formatId2Field(id) + ".setText(\"\");\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 map) {
StringBuilder sb = new StringBuilder();
sb.append("class MyViewHolder extends RecyclerView.ViewHolder {\r\n");
map.forEach((id, view) -> {
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 split(String res) {
String[] split = res.split("<");
HashMap 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;
}
}
/**
* 作者:张风捷特烈
* 时间:2018/10/31 0031:8:47
* 邮箱:[email protected]
* 说明:安卓自定义属性,代码生成器
*/
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 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 format(String preFix, File file) {
HashMap 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(""));
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;
}
}
/**
* 作者:张风捷特烈
* 时间:2018/10/31 0031:8:47
* 邮箱:[email protected]
* 说明:svg图标转换为Android可用xml生成器
*/
public class Svg2Xml {
@Test
public void svgDir() {
String dirPath = "E:\\Material\\MyUI\\#svg\\基础";
svg2xmlFromDir(dirPath);
}
@Test
public void singleSvg() {
File file = new File("E:\\Material\\MyUI\\#svg\\字母\\icon_a.svg");
svg2xml(file);
svg2xmlFromDir("E:\\Material\\MyUI\\#svg\\字母");
}
/**
* 将一个文件夹里的所有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());
} 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("\n" +
"\n");
for (String path : paths) {
outSb.append(" ");
}
outSb.append(" ");
return outSb;
}
/**
* 收集所有path
*
* @param result
* @param paths
*/
private static void collectPaths(String result, ArrayList paths) {
String[] split = result.split("