Article Link: HTTPS://MP.WEIXIN.QQ.COM/S/FQMYFT-KYIDBP-0HZK_HPW
Projects often use the function of sharing, there are sharing links and share pictures, which share the image of some need to move the screen to capture the content to share, it is clear that the view to bitmap to share the picture, there is a situation is not visible view into bitmap, This view is not displayed directly on the interface and requires the view that we created using inflate.
First Kind
First look at the Drawingcache method to intercept the normal view, get its view (BITMAP).
private Bitmap createBitmap(View view) { view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); return bitmap;}
This method applies to the view already displayed on the interface, can get the view of the width of the actual size, and then saved by Drawingcache to bitmap.
The second Kind
however , if the view to be intercepted is not fully displayed on the screen, for example, to intercept more than one screen of ScrollView, through the above method is not get bitmap, you need to use the following method, the view is ScrollView Sub View (LinearLayout), and of course the fully displayed view (the first case view) can also be intercepted using 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;}
Third Kind
There is a view is completely not displayed in the interface, through the inflate conversion view, this time through the Drawingcache is not get bitmap, also can not get the width of the view, the above two methods are not feasible. The third method uses measure, layout to obtain the actual size of the view.
public Bitmap createBitmap3(View v, int width, int height) { //测量使得view指定大小 int measuredWidth = View.MeasureSpec.makeMeasureSpec(width, View.MeasureSpec.EXACTLY); int measuredHeight = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY); v.measure(measuredWidth, measuredHeight); //调用layout方法布局后,可以得到view的尺寸大小 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);//这里传值屏幕宽高,得到的视图即全屏大小createBitmap3(view, getScreenWidth(), getScreenHeight());
Also wrote a simple way to save the picture, easy to see 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
Welcome to follow my blog: https://www.manjiexiang.cn/
More welcome attention number: Spring Breeze ten miles Better know you
Learn to make progress together
Android View turn bitmap generation