Android screenshot from code. Got it but not perfect

前端 未结 6 550
攒了一身酷
攒了一身酷 2021-02-04 21:47

I\'m trying to take a screenshot in code in Android. Actually the screenshot is the bitmap of the main RelativeLayout. The screenshot is taken but the content appears wrong, the

6条回答
  •  别那么骄傲
    2021-02-04 22:26

    I think you need to replace the

    RelativeLayout v = (RelativeLayout)findViewById(R.id.main_layout);
    
    v.setDrawingCacheEnabled(true);
    
    v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    
    v.layout(0, 0, v.getWidth(), v.getHeight());
    
    v.buildDrawingCache(true);
    
    Bitmap bm = Bitmap.createBitmap(v.getDrawingCache());
    
    v.setDrawingCacheEnabled(false);
    

    with

    Bitmap bitmap;
    
    View root = findViewById(android.R.id.content);
    
    View v1 = root.getRootView();
    
    v1.setDrawingCacheEnabled(true);
    
    root.setDrawingCacheEnabled(true);
    
    Bitmap bm = Bitmap.createBitmap(root.getDrawingCache());
    
    root.setDrawingCacheEnabled(false);
    

提交回复
热议问题