获取surfaceView的屏幕截图在Android中 [英] Get screenshot of surfaceView in Android
本文介绍了获取surfaceView的屏幕截图在Android中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的应用我认为有一些按钮和SurfaceView。我一定要把这个观点的截图,但在地方SurfaceView的只有黑场。我尝试,我发现计算器的解决方案,但他们不为我工作。
In my application I have view with some buttons and SurfaceView. I must take screenshot of this view, but in place of SurfaceView is only black field. I try solutions which I found on stackoverflow, but they don't work for me.
这是我的code,我usualy采取截图:
This is my code which I usualy take screenshots:
File folder = new File(Environment.getExternalStorageDirectory().getPath() + "/folder");
if (!folder.exists()) {
folder.mkdir();
}
String path = folder.getAbsolutePath() + "snapshot.png";
File file = new File(path);
file.createNewFile();
View view = this.findViewById(android.R.id.content).getRootView();
view.setDrawingCacheEnabled(true);
Bitmap drawingCache = view.getDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(drawingCache);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
} finally {
bitmap.recycle();
view.setDrawingCacheEnabled(false);
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
也许有人可以提供帮助。
Maybe someone can help.
推荐答案
我找到的解决方案如何使截图(但我不知道这是否是一个正确使用)。在surfaceView我创建方法drawBitmap:
I found solution how to make screenshot (but I don't know if this is a correct use). In surfaceView I created method drawBitmap:
public Bitmap drawBitmap() {
Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
thread.doDraw(canvas);
return bitmap;
}
在下一步骤余COM preSS的这个位图:
In the next step I compress this bitmap:
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, fos);
} finally {
bitmap.recycle();
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
这篇关于获取surfaceView的屏幕截图在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文