博客
关于我
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/

你可能感兴趣的文章
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:Google API - 从消息中获取 mimeTypes
查看>>
Python:subprocess.call,stdout to file,stderr to file,在屏幕上实时显示stderr
查看>>
Python:TypeError:无法连接“str“和“int“对象
查看>>
Python:XPath 在 ElementTree 中不可用
查看>>
Python:从 FTP 服务器下载文件
查看>>
python:从列表中创建元组列表
查看>>
Python:从嵌套列表中删除重复项
查看>>
Python:从间隔映射到值
查看>>
Python:使用 shell=False 的子进程调用不起作用
查看>>
Python:修改 PATH 环境变量的平台无关方式
查看>>
Python:假与 0
查看>>
Python:函数使用
查看>>
Python:函数进阶
查看>>
Python:在整数的二进制表示中查找最长的二进制间隙
查看>>
Python:如何从二进制转换为 base 64 并返回?
查看>>
Python:如何以非阻塞方式读取子进程的标准输出
查看>>
Python:如何使用 plotly 制作阴影区域或交替背景颜色?
查看>>
Python:如何根据日期时间获取值的计数
查看>>
python:如何检测串行 COM 上的设备名称/ID
查看>>