我有一个自定义平铺视图,直到最近才基于 SurfaceView。我现在已将其更改为扩展 TextureView。
基本上我得到了一个瓷砖位的大图像。
我需要捕获屏幕的所有内容并将其保存为位图,它适用于包括 SurfaceView 在内的所有视图。但是,当我现在使用相同的方法时,TextureView 的区域是黑色的。我读过这与硬件加速有关。
有什么方法可以捕获纹理视图的图像吗?下面是截屏方法。
public File takeScreenShot(View contentView)
{
File result = null;
String mPath = this.cacheDir + "/screenshot.png";
File imageFile = new File(mPath);
if (imageFile.exists()) {
imageFile.delete();
Log.i("ImageManager","Old screenshot image was deleted");
}
// create bitmap screen capture
Bitmap bitmap;
View v1 = contentView;
v1.setDrawingCacheEnabled(true);
v1.setDrawingCacheBackgroundColor(Color.WHITE);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
outputStream.flush();
outputStream.close();
result = imageFile;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
编辑:新尝试获取所有 TextureViews 并调用 getBitmap。这可行,但是将它们放置在较大位图的顶部与来自 TilingView 它自身或 getLocation OnScreen() 或 getLocationInWindow() 的坐标看起来不正确。
public List<TilingTextureView> getAllTextureViews(View view)
{
List<TilingTextureView> tilingViews = new ArrayList<TilingTextureView>();
if (view instanceof TilingTextureView) {
tilingViews.add((TilingTextureView)view);
}
else if(view instanceof ViewGroup)
{
ViewGroup viewGroup = (ViewGroup)view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
tilingViews.addAll(getAllTextureViews(viewGroup.getChildAt(i)));
}
}
return tilingViews;
}
在获取整个视图的屏幕截图后,在 takeScreenShot 方法中添加该位。
List<TilingTextureView> tilingViews = getAllTextureViews(contentView);
if (tilingViews.size() > 0) {
Canvas canvas = new Canvas(bitmap);
for (TilingTextureView tilingTextureView : tilingViews) {
Bitmap b = tilingTextureView.getBitmap(tilingTextureView.getWidth(), tilingTextureView.getHeight());
int[] location = new int[2];
tilingTextureView.getLocationInWindow(location);
int[] location2 = new int[2];
tilingTextureView.getLocationOnScreen(location2);
canvas.drawBitmap(b, location[0], location[1], null);
}
}