Created
April 10, 2016 16:20
-
-
Save zty5678/4eacd5be43e7fa12fd2b2d9193e9f2f8 to your computer and use it in GitHub Desktop.
将@2x.png这样的文件重命名并复制到drawable-xxhdpi里
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.Dimension; | |
import java.awt.GridBagConstraints; | |
import java.awt.GridBagLayout; | |
import java.awt.GridLayout; | |
import java.awt.Toolkit; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.event.WindowAdapter; | |
import java.awt.event.WindowEvent; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Files; | |
import javax.swing.BorderFactory; | |
import javax.swing.JButton; | |
import javax.swing.JFrame; | |
import javax.swing.JLabel; | |
import javax.swing.JOptionPane; | |
import javax.swing.JPanel; | |
import javax.swing.JTextField; | |
public class RenameAndCopyDemo { | |
private JFrame mainFrame; | |
JTextField inputEdit; | |
private JPanel northPanel; | |
private JPanel centerPanel; | |
public RenameAndCopyDemo() { | |
prepareGUI(); | |
} | |
public static void main(String[] args) { | |
new RenameAndCopyDemo(); | |
} | |
private void prepareGUI() { | |
mainFrame = new JFrame("重命名小工具"); | |
mainFrame.setSize(600,300); | |
mainFrame.setLayout(new GridLayout(3, 2)); | |
mainFrame.addWindowListener(new WindowAdapter() { | |
public void windowClosing(WindowEvent windowEvent) { | |
System.exit(0); | |
} | |
}); | |
northPanel = initNorthPanel(); | |
centerPanel = initCenterPanel(); | |
mainFrame.add(northPanel); | |
mainFrame.add(centerPanel); | |
mainFrame.setVisible(true); | |
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); | |
mainFrame.setLocation(dim.width / 2 - mainFrame.getSize().width / 2, | |
dim.height / 2 - mainFrame.getSize().height / 2); | |
} | |
private JPanel initNorthPanel() { | |
JPanel panel = new JPanel(); | |
panel.setBorder(BorderFactory.createEmptyBorder(1, 10, 10, 10)); | |
JLabel headerLabel = new JLabel("输入@2x.png所在的文件夹的全路径:"); | |
headerLabel.setBorder(BorderFactory.createEmptyBorder(10, 0, 10, 0)); | |
inputEdit = new JTextField(""); | |
GridBagLayout layout = new GridBagLayout(); | |
panel.setLayout(layout); | |
panel.add(headerLabel); | |
panel.add(inputEdit); | |
GridBagConstraints s = new GridBagConstraints(); | |
s.fill = GridBagConstraints.BOTH; | |
s.gridwidth=0; | |
s.weightx = 0; | |
s.weighty=0; | |
layout.setConstraints(headerLabel, s); | |
s.gridwidth=1; | |
s.gridheight=0; | |
s.weightx = 0; | |
s.weighty=0; | |
layout.setConstraints(inputEdit, s); | |
return panel; | |
} | |
private JPanel initCenterPanel() { | |
JPanel panel = new JPanel(); | |
JButton okButton = new JButton("开始复制"); | |
okButton.addActionListener(new ActionListener() { | |
public void actionPerformed(ActionEvent e) { | |
tryBeginFindAndCopy(inputEdit.getText()); | |
} | |
}); | |
panel.add(okButton); | |
return panel; | |
} | |
private void tryBeginFindAndCopy(String text){ | |
File file=new File(text); | |
if (file.exists()&& file.isDirectory()) { | |
int count=beginFindAndCopy(text); | |
JOptionPane.showMessageDialog(mainFrame, | |
"执行成功。复制了共"+count+"个文件。"); | |
}else{ | |
JOptionPane.showMessageDialog(mainFrame, | |
"错误,文件夹不存在", | |
"Error", | |
JOptionPane.ERROR_MESSAGE); | |
} | |
} | |
/** | |
* | |
* @param srcPath | |
* 输入的文件夹 | |
* @return 复制的文件数量 | |
*/ | |
protected int beginFindAndCopy(String srcPath) { | |
File rootFolder = new File(srcPath); | |
int i = 0; | |
for (File file : rootFolder.listFiles()) { | |
if (file.isFile()) { | |
String name = file.getName(); | |
System.out.println(name); | |
String fileName = null; | |
String multiplay = ""; | |
if (name.contains("@")) { | |
multiplay = name.substring(name.lastIndexOf("@") + 1, | |
name.lastIndexOf(".")); | |
String extention = name.substring(name.lastIndexOf(".")); | |
fileName = name.substring(0, name.lastIndexOf("@")) | |
+ extention; | |
} else { | |
multiplay = "1x"; | |
fileName = name; | |
} | |
multiplay = multiplay.toLowerCase(); | |
String folderName = getXXhdpiFolderNameByNX(multiplay); | |
if (folderName != null) { | |
File folder = new File(file.getParentFile(), folderName); | |
if (!(folder.isDirectory() && folder.exists())) { | |
folder.mkdirs(); | |
} | |
File targetFile = new File(folder, fileName); | |
copyFileFromTo(file, targetFile); | |
i++; | |
} | |
} | |
} | |
return i; | |
} | |
private static void copyFileFromTo(File srcFile, File targetFile) { | |
System.out.println(targetFile.getAbsolutePath()); | |
try { | |
Files.copy(srcFile.toPath(), targetFile.toPath()); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static String getXXhdpiFolderNameByNX(String multiplay) { | |
switch (multiplay) { | |
case "1x": | |
return "drawable-mdpi"; | |
case "1.5x": | |
return "drawable-hdpi"; | |
case "2x": | |
return "drawable-xhdpi"; | |
case "3x": | |
return "drawable-xxhdpi"; | |
case "4x": | |
return "drawable-xxxhdpi"; | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment