Android : taking Screenshot of the selected area programmatically

后端 未结 2 818
礼貌的吻别
礼貌的吻别 2021-01-15 04:08

My code is as follow :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main         


        
相关标签:
2条回答
  • 2021-01-15 04:26

    You can wrap the contents in a layout for example LinearLayout and follow the above code for taking screenshot by using the methods on the wrapped layout.

     Bitmap bitmap;
     ViewGroup v1 = findViewById(R.id.layout_id);
     v1.setDrawingCacheEnabled(true);
     bitmap = Bitmap.createBitmap(v1.getDrawingCache());
     v1.setDrawingCacheEnabled(false);
    
    0 讨论(0)
  • 2021-01-15 04:27

    Below method takes snapshot of given view which is adjustable by means of height and width then returns bitmap of it

    public static Bitmap takeSnapshot(View givenView, int width, int height) {
    Bitmap bm = Bitmap.createBitmap(width , height, Bitmap.Config.ARGB_8888);                
    Canvas snap = new Canvas(bm);
    givenView.layout(0, 0, givenView.getLayoutParams().width, givenView.getLayoutParams().height);
    givenView.draw(snap);
    return bm;  }
    
    0 讨论(0)
提交回复
热议问题