gpt4 book ai didi

android - 使用 DrawingCache 的屏幕截图 TextureView 是黑色的

转载 作者:行者123 更新时间:2023-11-29 00:25:41 32 4
gpt4 key购买 nike

我有一个自定义平铺 View ,它直到最近才基于 SurfaceView。我现在将其更改为扩展 TextureView。

基本上我得到的是一张大图 block 。

我需要捕获屏幕的所有内容并将其保存为位图,该位图适用于包括 SurfaceView 在内的所有 View 。但是,当我现在使用相同的方法时,TextureView 的区域是黑色的。我读到这与硬件加速有关。

有没有办法捕捉纹理 View 的图像?下面是截屏方法。

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;
}

编辑:新尝试获取所有 TextureView 并调用 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;
}

获取整个 View 的屏幕截图后,在 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);
}
}

最佳答案

注意事项:

Bitmap textureViewBitmap = textureView.getBitmap();

View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
v1.setDrawingCacheBackgroundColor(Color.TRANSPARENT);
Bitmap viewBitmap = Bitmap.createBitmap(v1.getDrawingCache());
viewBitmap.setHasAlpha(true);
v1.setDrawingCacheEnabled(false);

Canvas canvas = new Canvas(textureViewBitmap);
canvas.drawBitmap(viewBitmap, 0, 0, paint);

FileOutputStream outputStream = new FileOutputStream(imageFileDest);
int quality = 100;
cameraBitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();

假设 textureView 是全屏的并且只有一个。如果您希望叠加 View 覆盖 TextureView,viewBitmap.setHasAlpha(true); 很重要,否则组合位图在“textureView 部分”上仍然是黑色。

关于android - 使用 DrawingCache 的屏幕截图 TextureView 是黑色的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19704060/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com