Android 截图

mac2025-05-19  44

直接上代码

截取整个activity

/** * The activity passed in is the activity to be screenshot */ public static Bitmap getViewBitmap(Activity activity) { View view = activity.getWindow().getDecorView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap b1 = view.getDrawingCache(); Rect frame = new Rect(); activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int width = activity.getResources().getDisplayMetrics().widthPixels; int height = activity.getResources().getDisplayMetrics().heightPixels; Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height - statusBarHeight); view.destroyDrawingCache(); return b; }

截取ViewGroup

public static Bitmap getViewBitmap(ViewGroup viewGroup) { if(viewGroup.getBackground()==null){ viewGroup.setBackgroundColor(Color.WHITE); } //Total height of ViewGroup int h = 0; Bitmap bitmap; // Apply to ListView or RecyclerView for (int i = 0; i < viewGroup.getChildCount(); i++) { h += viewGroup.getChildAt(i).getHeight(); } bitmap = Bitmap.createBitmap(viewGroup.getWidth(), h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); viewGroup.draw(canvas); return bitmap; }

注意:

截取的viewgroup 要设置背景色,截取ScrollView时子Linearlayout也要设置背景色

 

 

 

 

最新回复(0)