不多说,直接上码!(原理是把视频去帧,保存到画布,然后把当前屏幕截图,然后再画到画布上)
@SuppressLint("NewApi")
private void savaScreenShot() {
// 构建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap(w/2, h/2, Config.ARGB_8888);
View decorview = this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp = decorview.getDrawingCache();
Bitmap bitmap = null;
// 2.3api下可用
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(
MainActivity.this,
Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.sws));
bitmap = retriever.getFrameAtTime(1000);// 按当前播放位置选择帧
Canvas canvas = new Canvas(bitmap);
// 游戏背景
// 假设这里的babelfish为游戏画面,将babelfish先绘制到canvas画布上,
// 实际是将babelfish画在了mGameBitmap 中。
canvas.drawBitmap(Bmp,0, -50, new Paint());
} catch (RuntimeException ex) {
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
}
// 截图保存路径
String mScreenshotPath = Environment.getExternalStorageDirectory()
+ "/droidnova";
String SavePath = getSDCardPath() + "/Demo/ScreenImages";
// 保存Bitmap
try {
File path = new File(SavePath);
// 文件
String filepath = SavePath + "/Screen_1.png";
File file = new File(filepath);
if (!path.exists()) {
path.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = null;
fos = new FileOutputStream(file);
if (null != fos) {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
Toast.makeText(MainActivity.this,
"截屏文件已保存至SDCard/ScreenImages/目录下",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 获取SDCard的目录路径功能
*
* @return
*/
private String getSDCardPath() {
File sdcardDir = null;
// 判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdcardExist) {
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}
@SuppressLint("NewApi")
private void savaScreenShot() {
// 构建Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap(w/2, h/2, Config.ARGB_8888);
View decorview = this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp = decorview.getDrawingCache();
Bitmap bitmap = null;
// 2.3api下可用
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(
MainActivity.this,
Uri.parse("android.resource://" + getPackageName() + "/"
+ R.raw.sws));
bitmap = retriever.getFrameAtTime(1000);// 按当前播放位置选择帧
Canvas canvas = new Canvas(bitmap);
// 游戏背景
// 假设这里的babelfish为游戏画面,将babelfish先绘制到canvas画布上,
// 实际是将babelfish画在了mGameBitmap 中。
canvas.drawBitmap(Bmp,0, -50, new Paint());
} catch (RuntimeException ex) {
} finally {
try {
retriever.release();
} catch (RuntimeException ex) {
}
// 截图保存路径
String mScreenshotPath = Environment.getExternalStorageDirectory()
+ "/droidnova";
String SavePath = getSDCardPath() + "/Demo/ScreenImages";
// 保存Bitmap
try {
File path = new File(SavePath);
// 文件
String filepath = SavePath + "/Screen_1.png";
File file = new File(filepath);
if (!path.exists()) {
path.mkdirs();
}
if (!file.exists()) {
file.createNewFile();
}
FileOutputStream fos = null;
fos = new FileOutputStream(file);
if (null != fos) {
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
fos.flush();
fos.close();
Toast.makeText(MainActivity.this,
"截屏文件已保存至SDCard/ScreenImages/目录下",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 获取SDCard的目录路径功能
*
* @return
*/
private String getSDCardPath() {
File sdcardDir = null;
// 判断SDCard是否存在
boolean sdcardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
if (sdcardExist) {
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}