博客
关于我
Android 布局文件(view)生成Bitmap
阅读量:132 次
发布时间:2019-02-27

本文共 2914 字,大约阅读时间需要 9 分钟。

已测量过的View生成Bitmap

在布局中绘制已测量的View至Bitmap,通常不需要额外测量和布局操作。以下代码示例展示了如何直接绘制已测量的View内容。

private static Bitmap drawMeasureView(View view) {    int width = view.getWidth();    int height = view.getHeight();    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);    Canvas canvas = new Canvas(bitmap);    view.draw(canvas);    return bitmap;}

未测量过的View生成Bitmap

对于未在布局中显示的View,需要手动进行测量和布局操作后再进行绘制。以下代码示例展示了完整的测量、布局和绘制流程。

public static Bitmap getBitmap(View view) {    DisplayMetrics metric = new DisplayMetrics();    getWindowManager().getDefaultDisplay().getMetrics(metric);    int screenWidth = metric.widthPixels; // 屏幕宽度(像素)    int screenHeight = metric.heightPixels; // 屏幕高度(像素)    int widthSpec = View.MeasureSpec.makeMeasureSpec(        screenWidth,         View.MeasureSpec.AT_MOST    );    int heightSpec = View.MeasureSpec.makeMeasureSpec(        screenHeight,         View.MeasureSpec.AT_MOST    );    view.measure(widthSpec, heightSpec);    int measuredWidth = view.getMeasuredWidth();    int measuredHeight = view.getMeasuredHeight();    view.layout(0, 0, measuredWidth, measuredHeight);    int width = view.getWidth();    int height = view.getHeight();    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);    Canvas canvas = new Canvas(bitmap);    view.draw(canvas);    return bitmap;}

布局更新与内容填充

在动态加载图片并填充布局中的ImageView时,需注意以下操作。以下代码示例展示了如何在图片加载完成后更新View的布局。

public void layoutView(final View viewBitmap, String url, Activity activity) {    final ImageView imageView = viewBitmap.findViewById(R.id.iv_show);    Glide.with(activity)        .asBitmap()        .load(url)        .into(new SimpleTarget
() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition
transition) { imageView.setImageBitmap(resource); // 更新并保存布局中的View内容 Bitmap bitmap = getBitmap(viewBitmap); savePhotoToSDCard(bitmap); } });}

图片保存到本地

确保在清单文件中添加必要的权限,并进行动态申请操作。以下代码示例展示了如何将图片保存到SD卡中。

public static void savePhotoToSDCard(Bitmap photoBitmap) {    FileOutputStream fos;    String imagePath = "";    try {        boolean isHasSDCard = Environment.getExternalStorageState().equals(            android.os.Environment.MEDIA_MOUNTED        );        if (isHasSDCard) {            File sdRoot = Environment.getExternalStorageDirectory();            File file = new File(sdRoot, Calendar.getInstance().getTimeInMillis() + ".png");            fos = new FileOutputStream(file);            imagePath = file.getAbsolutePath();        } else {            throw new Exception("创建文件失败!");        }        photoBitmap.compress(Bitmap.CompressFormat.PNG, 90, fos);        fos.flush();        fos.close();    } catch (Exception e) {        e.printStackTrace();    }}

权限设置

确保在Android清单文件中添加以下权限,允许应用程序进行必要的操作。

本文内容参考了多篇技术文章,结合实际开发经验编写,旨在提供清晰的技术解答和实用代码示例。

转载地址:http://wxub.baihongyu.com/

你可能感兴趣的文章
Postman接口自动化测试之——批量执行(集合操作)
查看>>
Postman接口自动化测试实战整理
查看>>
Postman接口自动化测试:从入门到实践!
查看>>
Postman断言与依赖接口测试详解!
查看>>
Postman核心功能解析 —— 参数化和测试报告
查看>>
Postman环境变量以及设置token全局变量!
查看>>
postman的使用
查看>>
Postman的使用说明
查看>>
Postman被低估的功能 — 自动化接口测试
查看>>
Postman被低估的功能 — 自动化接口测试
查看>>
Postman轻松签名,让SHA256withRSA保驾护航
查看>>
Postman还能做Mock?又学了一招!
查看>>
Postman还能做Mock?又学了一招!
查看>>
postman进行http接口测试
查看>>
Postman高阶技能:Collection集合批量运行!
查看>>
postMessage跨标签页共享数据
查看>>
QImage对一般图像的处理
查看>>
post为什么会发送两次请求?
查看>>
Post表单提交TextArea的值出现转译乱码问题 - Spring MVC处理表单提交
查看>>
Power BI 中的 Python 可视化需要什么设置?任何特定的 matplotlib 包版本或系统设置?
查看>>