简体   繁体   English

试图拍摄视图及其子级的屏幕快照

[英]trying to take a screen shot of a view and its children

I have relative layout named rel that dynamically adds an imageview to it like so: 我有一个名为rel相对布局,可以动态向其中添加一个imageview,如下所示:

     rel.setBackgroundResource(R.drawable.bg_share_one);

                RelativeLayout.LayoutParams p1 = new RelativeLayout.LayoutParams(921, 691);
                p1.leftMargin = 30;
                p1.topMargin = 30;

                ImageView img = new ImageView(this);
                img.setLayoutParams(p1);
                myBitmap = BitmapFactory.decodeFile(files[0].getAbsolutePath(), option1);
                img.setImageBitmap(myBitmap);

                rel.addView(img);
                saveCompareImage();

i then call this method to take a screen shot that relative layout but the imageview it added is not showing up in the bitmap: 然后,我调用此方法拍摄相对布局的屏幕截图,但其添加的imageview未显示在位图中:

 protected void saveCompareImage() {
    // TODO Auto-generated method stub

    rel.setDrawingCacheEnabled(true);
    try {

        File file = new File("/sdcard/LC/compare.jpg");
        file.createNewFile();
        Bitmap bm = rel.getDrawingCache();
        FileOutputStream ostream = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.JPEG, 80, ostream);
        ostream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Perhaps you have hardware acceleration enabled? 也许您启用了硬件加速? The documentation of setDrawingCacheEnabled() states that in that case rendering is done differently: setDrawingCacheEnabled()的文档指出,在这种情况下,呈现方式有所不同:

When hardware acceleration is turned on, enabling the drawing cache has no effect on rendering because the system uses a different mechanism for acceleration which ignores the flag. 启用硬件加速后,启用图形缓存对渲染没有影响,因为系统使用不同的加速机制来忽略该标志。 If you want to use a Bitmap for the view, even when hardware acceleration is enabled, see setLayerType(int, android.graphics.Paint) for information on how to enable software and hardware layers. 如果要在视图中使用位图,即使启用了硬件加速,也请参阅setLayerType(int,android.graphics.Paint)以获取有关如何启用软件和硬件层的信息。

Try setLayerType(LAYER_TYPE_SOFTWARE, null) or something. 尝试使用setLayerType(LAYER_TYPE_SOFTWARE, null)或其他方法。

problem was not calling measure: this works: 问题不在于采取措施:这可行:

protected void saveCompareImage() {
    // TODO Auto-generated method stub

    rel.setDrawingCacheEnabled(true);

    try {
        File file = new File("/sdcard/LC/compare.jpg");
        file.createNewFile();   
        Bitmap bitmap;
        rel.setDrawingCacheEnabled(true);
        rel.measure(MeasureSpec.makeMeasureSpec(rel.getLayoutParams().width, MeasureSpec.EXACTLY),
                MeasureSpec.makeMeasureSpec(rel.getLayoutParams().height, MeasureSpec.EXACTLY));
        rel.layout(0, 0, rel.getMeasuredWidth(), rel.getMeasuredHeight());

        bitmap = Bitmap.createBitmap(rel.getDrawingCache());


        FileOutputStream ostream = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
        ostream.close();
    } catch (Exception e) {
        Log.v("ERRRO","e:"+e);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM