我正在使用以下方法截取特定 View (SurfaceView)的屏幕截图。
public void takeScreenShot(View surface_view){
// create bitmap screen capture
Bitmap bitmap;
View v1 = surface_view;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0, bos);
byte[] imageData = bos.toByteArray();
}
问题是它给了我整个 Activity 屏幕图像。但我需要拍摄特定 View 的屏幕截图。我尝试了其他方法,但这些方法给我一个黑屏作为屏幕截图,一些帖子说它需要根设备。 谁能帮帮我。我需要这个解决方案。帮帮我....
最佳答案
表面 View 是一个 View ,但表面 View 上的项目(如位图或其他对象)不是任何 View 。因此,当您捕获表面 View 时,它会捕获表面上的所有事物。您必须使用其他 View ,例如 ImageView 或表面 View 之上的其他 View ,然后捕获这些 View 。
首先获取要拍照的 View 然后执行此操作
Bitmap bitmap;
View rv = **your view**
rv.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(rv.getDrawingCache());
rv.setDrawingCacheEnabled(false);
// Write File to internal Storage
String FILENAME = "captured.png";
FileOutputStream fos = null;
try {
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
Log.v("","FileNotFoundException: "+e1.getMessage());
}
try {
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
关于android - 在 android 中拍摄 SurfaceView 的屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12724237/