android截图 从View截图Bitmap

步骤:

I.获取图片

II.保存到图库 


I.获取图片

旧的API是对View 的 DrawingCache 进行操作的,为了避免不必要的内存开销,已经放弃了这一方案采用PixelCopy来获取图像,使用老API出现了尺寸异常,而新API又有版本限制!!!

所以不能同统一使用新的API,则代码如下:

新API采用 PixelCopy#request()request(@NonNull Window source, @Nullable Rect srcRect,
        @NonNull Bitmap dest, @NonNull OnPixelCopyFinishedListener listener,
        @NonNull Handler listenerThread)

其中的OnPixelCopyFinishedListener其实可以不去实现,代码挺同步的>.<,为了严谨还是好好写代码吧  >_<

/**
 * 从 View 中获取 Bitmap
 *
 * @param activity Build.VERSION.SDK_INT >= Build.VERSION_CODES.O 会使用 activity获取 window
 * @param view     将要被截图的View
 * @return view的截图
 */
private Bitmap getBitmapFromView(Activity activity, View view) {
    if (view == null) return null;
    Bitmap b;
    //请求转换
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        //获取layout的位置
        final int[] location = new int[2];
        view.getLocationInWindow(location);
        //准备一个bitmap对象,用来将copy出来的区域绘制到此对象中
        b = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888, true);
        PixelCopy.request(activity.getWindow(),
                new Rect(location[0], location[1], location[0] + view.getWidth(), location[1] + view.getHeight()),
                b, copyResult -> {
                    //如果成功
                    if (copyResult == PixelCopy.SUCCESS) {
                        //这里做的回调,其实这个过程可以作为同步代码处理
                        //image.setImageBitmap(bitmap);
                    }
                }, new Handler(Looper.getMainLooper()));
    } else {
        view.setDrawingCacheEnabled(true);
        view.buildDrawingCache();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            view.measure(View.MeasureSpec.makeMeasureSpec(view.getWidth(), View.MeasureSpec.EXACTLY),
                    View.MeasureSpec.makeMeasureSpec(view.getHeight(), View.MeasureSpec.EXACTLY));
            view.layout((int) view.getX(), (int) view.getY(),
                    (int) view.getX() + view.getMeasuredWidth(), (int) view.getY() + view.getMeasuredWidth());
        } else {
            view.measure(
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                    View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
            );
            view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        }
        b = Bitmap.createBitmap(view.getDrawingCache(),
                0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
        view.setDrawingCacheEnabled(false);
        view.destroyDrawingCache();
    }
    return b;
}

II.保存到图库

  /**
     * 保存到相册
     *
     * @param bitmap
     */
    private void save2Media(Bitmap bitmap) {
        String image = MediaStore.Images.Media.insertImage(
                getContentResolver(),
                bitmap, "预填单信息", "");

        //保存图片后发送广播通知更新数据库
        sendBroadcast(new Intent(
                Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
                Uri.parse(image)));
        showToast("已保存至相册");
    }

 

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值