【Android】多种方式实现截图(屏幕截图、控件截图、长图)

发布于:2024-06-21 ⋅ 阅读:(97) ⋅ 点赞:(0)

Android 截图主要为四种:View 截图、WebView 截图、屏幕截图、系统截图和 adb 截图。后两种截图不常用,不详细展开。

1. View截图

可以截取到View不可见的部分,生成长图,状态栏和导航栏无法截到

fun screenshotView(view: ViewGroup):Bitmap?{
    var h = 0
    var bitmap:Bitmap?=null
    for(i in 0 until view.childCount){
        h += view.getChildAt(i).height
        view.getChildAt(i).setBackgroundColor(Color.parseColor("#6CC287"))
    }
    bitmap = Bitmap.createBitmap(view.width, h, Bitmap.Config.RGB_565)
    val canvas = Canvas(bitmap)
    view.draw(canvas)
    //重新赋色
    for(i in 0 until view.childCount){
        view.getChildAt(i).setBackgroundDrawable(null)
    }
    return bitmap
}

2. WebView截图

// 1.capturePicture方法废弃
// 2.getScale方法废弃

// 3.getDrawingCache方法
private static byte[] screenshotWebView() {
  Bitmap bitmap = webView.getDrawingCache();
  byte[] drawByte = getBitmapByte(bmp);
  return drawByte;
}

// 4.draw方法
private static byte[] screenshotWebView() {
  // webView.setDrawingCacheEnabled(true); 设置缓存
  Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webView.getHeight(), Bitmap.Config.ARGB_8888);
  Canvas canvas = new Canvas(bitmap);
  webView.draw(canvas);
  webView.destroyDrawingCache();
  byte[] drawByte = getBitmapByte(bitmap);
  return drawByte;
}

可能会截取不到 cavans 元素,原因是开启了硬件加速(关闭硬件加速可能导致页面异常),可在 AndroidManifest.xml 中设置:

android:hardwareAccelerated="false"

截长图的话需要配置:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  WebView.enableSlowWholeDocumentDraw();
}
setContentView(R.layout.webview);

3. 屏幕截图

截取应用当前屏幕的图片:

	/**
     * 获取当前屏幕截图,包含状态栏
     *
     * @param activity activity
     * @return Bitmap
     */
    public static Bitmap captureWithStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap ret = Bitmap.createBitmap(bmp, 0, 0, width, height);
        view.destroyDrawingCache();
        return ret;
    }

    /**
     * 获取当前屏幕截图,不包含状态栏
     *
     * @param activity activity
     * @return Bitmap
     */
    public static Bitmap captureWithoutStatusBar(Activity activity) {
        View view = activity.getWindow().getDecorView();
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        Bitmap bmp = view.getDrawingCache();
        int statusBarHeight = getStatusBarHeight(activity);
        int width = getScreenWidth(activity);
        int height = getScreenHeight(activity);
        Bitmap ret = Bitmap.createBitmap(bmp, 0, statusBarHeight, width, height - statusBarHeight);
        view.destroyDrawingCache();
        return ret;
    }

	/**
     * 得到屏幕的高
     *
     * @param context
     * @return
     */
    public static int getScreenHeight(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int height = wm.getDefaultDisplay().getHeight();
        return height;
    }

    /**
     * 得到屏幕的宽
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        int width = wm.getDefaultDisplay().getWidth();
        return width;
    }

	/**
     * 获取状态栏高度
     *
     * @param context 上下文
     * @return 状态栏高度
     */
    public static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources()
                .getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

格式转换方法

// Bitmap 转 Base64
private static String getBitmapString(Bitmap bitmap) {
  String result = null;
  ByteArrayOutputStream out = null;
  try {
    if (bitmap != null) {
      out = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
​
      out.flush();
      out.close();byte[] bitmapBytes = out.toByteArray();
      result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
    }
  } catch (IOException e) {
      e.printStackTrace();
  } finally {
    try {
      if (out != null) {
          out.flush();
          out.close();
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  return result;
}// Bitmap 转 Byte
private static byte[] getBitmapByte(Bitmap bitmap){
  ByteArrayOutputStream out = new ByteArrayOutputStream();
  // 转换类型,压缩质量,字节流资源
  bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
  try {
      out.flush();
      out.close();
  } catch (IOException e) {
      e.printStackTrace();
  }
  return out.toByteArray();
}

// Drawable 转 Bitmap
public static Bitmap toBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof ColorDrawable) {
        //color
        Bitmap bitmap = Bitmap.createBitmap(32, 32, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(((ColorDrawable) drawable).getColor());
        return bitmap;
    } else if (drawable instanceof NinePatchDrawable) {
        //.9.png
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
                drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);
        return bitmap;
    }
    return null;
}

网站公告

今日签到

点亮在社区的每一天
去签到