本人在安卓程序开发时用到的各种小功能(一) 屏幕截图
2016-01-26 09:43
429 查看
新建一个方法
其中的webview是一个网页控件,想切图像的哪一个部分,直接定义即可 比如 一个页面是一个线性布局,那就先初始化xml中的线性布局 LinearLayout linearLayout; 把
webview 替换成linearLayout即可
APKUtil为用户自定义的一个工具类
APKUtil.getDiskCacheDir(); /**
* 获得磁盘缓存目录 [PS:应用卸载后会被自动删除]
*
* @param context
* @param uniqueName
* @return
*/
public static File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getApplicationContext().getExternalCacheDir().getPath();
} else {
cachePath = context.getApplicationContext().getFilesDir().getPath();
}
File file = new File(cachePath + File.separator + uniqueName);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
Constants用户自定义的网络连接地址
Constants.TEMP_DIR
public static final String TEMP_DIR = "temp"; // 临时文件夹
之后选择进行截图的方式 以下是一个按钮点击时截图
btnright.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = captureScreen();
if (file != null || file.getAbsolutePath() != null) {
path = file.getAbsolutePath();
}
}
});
private File captureScreen() { webview.setDrawingCacheEnabled(true); webview.buildDrawingCache(); Bitmap bmp = webview.getDrawingCache(); File file = new File(APKUtil.getDiskCacheDir(this, Constants.TEMP_DIR), messageTitle + ".jpg"); if (file.exists()) { file.delete(); } file.getParentFile().mkdirs(); FileOutputStream out; try { out = new FileOutputStream(file); if (bmp.compress(Bitmap.CompressFormat.JPEG, 70, out)) { out.flush(); out.close(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return file; }
其中的webview是一个网页控件,想切图像的哪一个部分,直接定义即可 比如 一个页面是一个线性布局,那就先初始化xml中的线性布局 LinearLayout linearLayout; 把
webview 替换成linearLayout即可
APKUtil为用户自定义的一个工具类
APKUtil.getDiskCacheDir(); /**
* 获得磁盘缓存目录 [PS:应用卸载后会被自动删除]
*
* @param context
* @param uniqueName
* @return
*/
public static File getDiskCacheDir(Context context, String uniqueName) {
String cachePath;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cachePath = context.getApplicationContext().getExternalCacheDir().getPath();
} else {
cachePath = context.getApplicationContext().getFilesDir().getPath();
}
File file = new File(cachePath + File.separator + uniqueName);
if (!file.exists()) {
file.mkdirs();
}
return file;
}
Constants用户自定义的网络连接地址
Constants.TEMP_DIR
public static final String TEMP_DIR = "temp"; // 临时文件夹
之后选择进行截图的方式 以下是一个按钮点击时截图
btnright.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File file = captureScreen();
if (file != null || file.getAbsolutePath() != null) {
path = file.getAbsolutePath();
}
}
});
相关文章推荐