首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用DrawingCache时,屏幕截图TextureView为黑色

使用DrawingCache时,屏幕截图TextureView为黑色
EN

Stack Overflow用户
提问于 2013-10-31 18:31:42
回答 1查看 3.7K关注 0票数 2

我有一个自定义的平铺视图,直到最近都是基于SurfaceView的。我现在已经将其更改为扩展TextureView。

基本上,我得到了一个大的图像在瓷砖比特。

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

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

代码语言:javascript
运行
复制
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()的坐标看起来并不正确。

代码语言:javascript
运行
复制
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方法中的。

代码语言:javascript
运行
复制
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);
    }
}
EN

回答 1

Stack Overflow用户

发布于 2018-03-06 17:51:37

需要注意的一些事情:

代码语言:javascript
运行
复制
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是全屏的,并且只有一个。如果您希望覆盖在TextureView上的覆盖视图,viewBitmap.setHasAlpha(true);是重要的,否则组合的位图在"textureView部件“上仍然是黑色的。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19704060

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档