Method of generating screenshots from android view to Bitmap

  • 2021-10-15 11:24:14
  • OfStack

Sharing is often used in projects, There are sharing links and sharing pictures, among which some sharing pictures require the mobile terminal to intercept and share the screen content. To put it bluntly, view is converted into bitmap and then to share pictures. In another case, invisible view is converted into bitmap. This view is not directly displayed on the interface, and we need to use inflate to create view.

Type 1

First look at the DrawingCache method to intercept the ordinary view and get its view (Bitmap).


private Bitmap createBitmap(View view) {
  view.buildDrawingCache();
  Bitmap bitmap = view.getDrawingCache();
  return bitmap;
}

This method is suitable for view, which is already displayed on the interface. The actual width and height of view can be obtained, and then saved as bitmap through DrawingCache.

Type 2

However, if the view to be intercepted is not completely displayed on the screen, for example, scrollview with more than one screen is to be intercepted, and bitmap cannot be obtained by the above method, the following method needs to be used, and the transmitted view is the child view (LinearLayout) of scrollview, etc. Of course, the completely displayed view (view in the first case) can also be intercepted by this method.


public Bitmap createBitmap2(View v) {
  Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas c = new Canvas(bmp);
  c.drawColor(Color.WHITE);
  v.draw(c);
  return bmp;
}

Type 3

There is also an view that is not displayed on the interface at all, and view transformed by inflate. At this time, bitmap and the width and height of view cannot be obtained by DrawingCache. Neither of the above two methods is feasible. In the third method, the actual size of view is obtained by measure and layout.


public Bitmap createBitmap3(View v, int width, int height) {
  // Measure so that view Specify size 
  int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY);
  int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);
  v.measure(measuredWidth, measuredHeight);
  // Call layout Method layout, you can get the view The size of 
  v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
  Bitmap bmp = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas c = new Canvas(bmp);
  c.drawColor(Color.WHITE);
  v.draw(c);
  return bmp;
}


View view = LayoutInflater.from(this).inflate(R.layout.view_inflate, null, false);
// Here, the width and height of the screen are passed, and the obtained view is the full screen size 
createBitmap3(view, getScreenWidth(), getScreenHeight());

In addition, I wrote a simple way to save pictures, which is convenient to view the effect.


private void saveBitmap(Bitmap bitmap) {
  FileOutputStream fos;
  try {
    File root = Environment.getExternalStorageDirectory();
    File file = new File(root, "test.png");
    fos = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);
    fos.flush();
    fos.close();
  } catch (Exception e) {
    e.printStackTrace();
  }
}

github Address: https://github.com/taixiang/view2bitmap


Related articles: