Take screenshot with dialog

前端 未结 5 1511
既然无缘
既然无缘 2021-01-02 03:59

I need to take screenshot programmatically of activity view. I have found a lot of answers how to do it, but there is not answer how to do it with opened dialog

5条回答
  •  攒了一身酷
    2021-01-02 04:10

    This is work for me (but without dialog):

    public class MainActivity extends AppCompatActivity {
    
    private View main;
    private ImageView imageView;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    
        //Here we Checking CALL_PHONE permission
    
        setContentView(R.layout.activity_main);
        main = findViewById(R.id.main);
        imageView = (ImageView) findViewById(R.id.imageView);
        Button btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
    
                Bitmap b = Screenshot.takescreenshotOfRoot(imageView);
                imageView.setImageBitmap(b);
                main.setBackgroundColor(Color.parseColor("#999999"));
            }
        });
    
    }
    

    }

    Screenshot class:

    public class Screenshot {
    
    public static Bitmap takescreenshot (View v) {
        v.setDrawingCacheEnabled(true);
        v.buildDrawingCache(true);
        Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
        v.setDrawingCacheEnabled(false);
        return b;
    }
    
    public static Bitmap takescreenshotOfRoot(View v) {
        return takescreenshot(v.getRootView());
    
    }
    

    }

    XML:

    
    
    
    

提交回复
热议问题