CapturePictureUtils 截图工具类
支持 View、Activity、FrameLayout、RelativeLayout、LinearLayout、ListView、GridView、ScrollView、HorizontalScrollView、NestedScrollView、WebView、RecyclerView(GridLayoutManager、LinearLayoutManager、StaggeredGridLayoutManager)
方法 |
注释 |
setBitmapConfig |
设置 Bitmap Config |
setBackgroundColor |
设置 Canvas 背景色 |
setPaint |
设置画笔 |
snapshotWithStatusBar |
获取当前屏幕截图, 包含状态栏 ( 顶部灰色 TitleBar 高度, 没有设置 android:theme 的 NoTitleBar 时会显示 ) |
snapshotWithoutStatusBar |
获取当前屏幕截图, 不包含状态栏 ( 如果 android:theme 全屏, 则截图无状态栏 ) |
enableSlowWholeDocumentDraw |
关闭 WebView 优化 |
snapshotByWebView |
截图 WebView |
snapshotByView |
通过 View 绘制为 Bitmap |
snapshotByViewCache |
通过 View Cache 绘制为 Bitmap |
snapshotByLinearLayout |
通过 LinearLayout 绘制为 Bitmap |
snapshotByFrameLayout |
通过 FrameLayout 绘制为 Bitmap |
snapshotByRelativeLayout |
通过 RelativeLayout 绘制为 Bitmap |
snapshotByScrollView |
通过 ScrollView 绘制为 Bitmap |
snapshotByHorizontalScrollView |
通过 HorizontalScrollView 绘制为 Bitmap |
snapshotByNestedScrollView |
通过 NestedScrollView 绘制为 Bitmap |
snapshotByListView |
通过 ListView 绘制为 Bitmap |
snapshotByGridView |
通过 GridView 绘制为 Bitmap |
snapshotByRecyclerView |
通过 RecyclerView 绘制为 Bitmap |
package dev.utils.app;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Picture;
import android.graphics.Point;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.ColorInt;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import dev.DevUtils;
import dev.utils.LogPrintUtils;
public final class CapturePictureUtils {
private CapturePictureUtils() {
}
private static final String TAG = CapturePictureUtils.class.getSimpleName();
private static Bitmap.Config BITMAP_CONFIG = Bitmap.Config.RGB_565;
private static int BACKGROUND_COLOR = Color.TRANSPARENT;
private static Paint PAINT = new Paint();
public static void setBitmapConfig(final Bitmap.Config config) {
if (config == null) return;
BITMAP_CONFIG = config;
}
public static void setBackgroundColor(@ColorInt final int backgroundColor) {
BACKGROUND_COLOR = backgroundColor;
}
public static void setPaint(final Paint paint) {
if (paint == null) return;
PAINT = paint;
}
public static Bitmap snapshotWithStatusBar(final Activity activity) {
try {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) return null;
int[] widthHeight = getScreenWidthHeight();
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap, 0, 0, widthHeight[0], widthHeight[1]);
view.destroyDrawingCache();
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotWithStatusBar");
}
return null;
}
public static Bitmap snapshotWithoutStatusBar(final Activity activity) {
try {
View view = activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap cacheBitmap = view.getDrawingCache();
if (cacheBitmap == null) return null;
int[] widthHeight = getScreenWidthHeight();
int statusBarHeight = getStatusBarHeight(activity);
Rect frame = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap, 0, statusBarHeight, widthHeight[0], widthHeight[1] - statusBarHeight);
view.destroyDrawingCache();
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotWithoutStatusBar");
}
return null;
}
public static void enableSlowWholeDocumentDraw() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
android.webkit.WebView.enableSlowWholeDocumentDraw();
}
}
public static Bitmap snapshotByWebView(final WebView webView) {
return snapshotByWebView(webView, Integer.MAX_VALUE, BITMAP_CONFIG, 0f);
}
public static Bitmap snapshotByWebView(final WebView webView, final int maxHeight) {
return snapshotByWebView(webView, maxHeight, BITMAP_CONFIG, 0f);
}
public static Bitmap snapshotByWebView(final WebView webView, final float scale) {
return snapshotByWebView(webView, Integer.MAX_VALUE, BITMAP_CONFIG, scale);
}
public static Bitmap snapshotByWebView(final WebView webView, final int maxHeight, final Bitmap.Config config) {
return snapshotByWebView(webView, maxHeight, config, 0f);
}
public static Bitmap snapshotByWebView(final WebView webView, final int maxHeight,
final Bitmap.Config config, final float scale) {
if (webView != null && config != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
try {
float newScale = scale;
if (newScale <= 0) {
newScale = webView.getScale();
}
int width = webView.getWidth();
int height = (int) (webView.getContentHeight() * newScale + 0.5);
height = (height > maxHeight) ? maxHeight : height;
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
webView.draw(canvas);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByWebView - SDK_INT >= 21(5.0)");
}
} else {
try {
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
height = (height > maxHeight) ? maxHeight : height;
Bitmap bitmap = Bitmap.createBitmap(width, height, config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
picture.draw(canvas);
return bitmap;
}
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByWebView - SDK_INT < 21(5.0)");
}
}
}
return null;
}
public static Bitmap snapshotByView(final View view) {
return snapshotByView(view, BITMAP_CONFIG);
}
public static Bitmap snapshotByView(final View view, final Bitmap.Config config) {
if (view == null || config == null) return null;
try {
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), config);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(BACKGROUND_COLOR);
view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
view.draw(canvas);
return bitmap;
} catch (Exception e) {
LogPrintUtils.eTag(TAG, e, "snapshotByView");
}
return null;
}
public static Bitmap snapshotByViewCache(final View view) {
if (view == null) return null;
try {
view.clearFocus();
view.setPressed(false);
boolean willNotCache = view.willNotCacheDrawing();
view.setWillNotCacheDrawing(false);
int color = view.getDrawingCacheBackgroundColor();
view.setDrawingCacheBackgroundColor(0);
if (color !=