关键代码:
点击按钮后开始截取屏幕
this.button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyy-MM-dd_HH-mm-ss", Locale.US);
String fname = "/sdcard/" + sdf.format(new Date()) + ".png";
filename=fname;
File f = new File(fname);
// if(!f.exists()){
// try {
// f.createNewFile();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
View view = v.getRootView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap != null) {
System.out.println("bitmap got!");
try {
FileOutputStream out = new FileOutputStream(fname);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
System.out.println("file " + fname + "output done.");
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("bitmap is NULL!");
}
}
});