对于不同测试机型截图的快捷键有很大差别,由于本人比较懒,不想记各种机型的截屏快捷键,对此我直接写了android手机截图,实现方式用的是java swing界面,界面如下
image.png
整体思路如下,点击截图路径按钮弹出路径选择框,点击确定后,JTextfiled显示路径,点击截图按钮,执行截图命令,截图的路径为JTextfiled显示的路径,截图命令成功后弹出截图成功的Toast
下面附上所有代码:
1.获取当前时间工具类
import java.text.SimpleDateFormat;
import java.util.Date;
public class GetStringTimeUtils {
//获取String类型的时间格式
public static String getTime(){
SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String date = df.format(new Date());
return date;
}
}
2.cmd命令执行工具类
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class CmdUtils {
List<String>resultList=new ArrayList<String>();
public List<String> excuteCmd(String command){
//String command="adb devices";
String line = null;
StringBuilder sb = new StringBuilder();
Runtime runtime = Runtime.getRuntime();
try {
Process process = runtime.exec(command);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(process.getInputStream()));
while ((line = bufferedReader.readLine()) != null) {
resultList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
return resultList;
}
}
3.Toast类
import java.awt.Color;
import java.awt.Composite;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.RenderingHints;
import java.awt.Window;
import java.awt.font.FontRenderContext;
import java.awt.geom.Rectangle2D;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JWindow;
//自定义toast
public class Toast extends JWindow{
private static final long serialVersionUID = 1L;
private String message = "";
private final Insets insets = new Insets(12, 24, 12, 24);
private int period = 1500;
private Font font;
public static final int msg = 0;// 提示 黑色背景色
public static final int success = 1;// 成功提示 浅蓝色背景色
public static final int error = 2;// 错误提示 粉红色背景色
private Color background;
private Color foreground;
public Toast(Window parent, String message, int period) {
this(parent, message, period, 0);
}
public Toast(Window parent, String message, int period, int type) {
super(parent);
this.message = message;
this.period = period;
font = new Font("宋体", Font.PLAIN, 14);
setSize(getStringSize(font, true, message));
// 相对JFrame的位置
setLocationRelativeTo(parent);
installTheme(type);
}
@Override
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// old
Composite oldComposite = g2.getComposite();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setFont(font);
FontMetrics fm = g2.getFontMetrics();
g2.setColor(background);
g2.fillRoundRect(0, 0, getWidth(), getHeight(), 12, 12);
g2.setColor(foreground);
g2.drawString(message, insets.left, fm.getAscent() + insets.top);
// restore
g2.setComposite(oldComposite);
}
/**
* 启动提示
*/
public void start() {
this.setVisible(true);
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
setVisible(false);
}
}, period);
}
public void setMessage(String message) {
this.message = message;
Dimension size = getStringSize(font, true, message);
setSize(size);
revalidate();
repaint(0, 0, size.width, size.height);
if (!isVisible()) {
start();
}
}
private void installTheme(int type) {
switch (type) {
case msg:
background = new Color(0x515151);
foreground = Color.WHITE;
break;
case success:
background = new Color(223, 240, 216);
foreground = new Color(49, 112, 143);
break;
case error:
background = new Color(242, 222, 222);
foreground = new Color(221, 17, 68);
break;
default:
background = new Color(0x515151);
foreground = Color.WHITE;
break;
}
}
private Dimension getStringSize(Font font, boolean isAntiAliased,
String text) {
FontRenderContext renderContext = new FontRenderContext(null,
isAntiAliased, false);
Rectangle2D bounds = font.getStringBounds(text, renderContext);
int width = (int) bounds.getWidth() + 2 * insets.left;
int height = (int) bounds.getHeight() + insets.top * 2;
return new Dimension(width, height);
}
}
4.截图功能类
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
//截图线程
public class ScreenCapTask extends Thread {
CmdUtils cmdUtils=new CmdUtils();
JTextField textField;
String device;
public ScreenCapTask(JTextField textField){
this.textField=textField;
}
@Override
public void run() {
// TODO Auto-generated method stub
super.run();
//截图命令
String command="adb shell screencap -p /sdcard/screen.png";
cmdUtils.excuteCmd(command);
if(textField.getText().toString().equals("")){
//未选择路径弹出未选择路径的提示框
JOptionPane.showConfirmDialog(null, "未选择截图路径,日志文件保存在手机/sdcard/screen.png中","输入提示", JOptionPane.DEFAULT_OPTION);
}
else{
//截图后从手机将截图推到选择的路径下
String command1="adb pull /sdcard/screen.png "+textField.getText();
System.out.println(command);
cmdUtils.excuteCmd(command1);
}
}
}
5.主类代码
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.naming.Context;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.WindowConstants;
public class Screenshot extends JFrame {
JLabel label;
JTextField jTextField;
JButton jButton,jButton2;
//由于Toast需要传本类的实例,此处使用单例模式
private static Screenshot instance = new Screenshot();
public static Screenshot getInstance(){
return instance;
}
private Screenshot(){
setTitle("Yalla测试平台");
setLayout(null);
setBounds(0,0, 800,300);
initView();
addView();
setLinstner();
}
private void setLinstner() {
// TODO Auto-generated method stub
//选择截图路径
jButton2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
JFileChooser jf = new JFileChooser();
jf.setFileSelectionMode(JFileChooser.SAVE_DIALOG | JFileChooser.DIRECTORIES_ONLY);
jf.showDialog(null,null);
File fi = jf.getSelectedFile();
//选择路径后,路径展示在jTextField上
String f = fi.getAbsolutePath()+"\\"+GetStringTimeUtils.getTime()+".png";
System.out.println("save: "+f);
jTextField.setText(f);
}
});
//截图
jButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
new ScreenCapTask(jTextField).start();
//截图命令后弹出toast,提示截图成功
Toast toast=new Toast(getInstance(), "截图成功", 5000, Toast.success);
toast.start();
}
});
}
private void addView() {
// TODO Auto-generated method stub
add(jButton);
add(jButton2);
add(jTextField);
add(label);
setVisible(true);
//窗口关闭后程序关闭
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
}
private void initView() {
// TODO Auto-generated method stub
jButton=new JButton("截图");
jButton.setBounds(477,10,100, 25);
jButton2=new JButton("截图路径选择");
jButton2.setBounds(450,10,25, 25);
jTextField=new JTextField();
jTextField.setBounds(73,10,375,25);
label=new JLabel("截图路径");
label.setBounds(10,10,60, 25);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//getInstance();
getInstance();
}
}
网友评论