二、具体实现方式
实用截图方法截取整个activity
public static Bitmap shotActivity(Activity ctx) {View view = ctx.getWindow().getDecorView();view.setDrawingCacheEnabled(true);view.buildDrawingCache();Bitmap bp = Bitmap.createBitmap(view.getDrawingCache(), 0, 0, view.getMeasuredWidth(),view.getMeasuredHeight());view.setDrawingCacheEnabled(false);view.destroyDrawingCache();return bp;
}
实用截图方法截取当前view
public static Bitmap getViewBp(View v) {if (null == v) {return null;}v.setDrawingCacheEnabled(true);v.buildDrawingCache();if (Build.VERSION.SDK_INT >= 11) {v.measure(View.MeasureSpec.makeMeasureSpec(v.getWidth(),View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(v.getHeight(), View.MeasureSpec.EXACTLY));v.layout((int) v.getX(), (int) v.getY(),(int) v.getX() + v.getMeasuredWidth(),(int) v.getY() + v.getMeasuredHeight());} else {v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());}Bitmap b = Bitmap.createBitmap(v.getDrawingCache(), 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());v.setDrawingCacheEnabled(false);v.destroyDrawingCache();return b;
}