博客
关于我
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 如果有很多或以收缩形式
查看>>
Python 子进程 Popen 与 Pyinstaller
查看>>
Python 子进程 Popen.communicate() 等价于 Popen.stdout.read()?
查看>>
Python 子进程 Popen:为什么会出现“ls *.txt“?不行?
查看>>
Python 子进程参数
查看>>
python 字典 key 和value 互换
查看>>
python 字典sorted自定义排序,按照key or value排序
查看>>
python 字典和列表区别_元组列表和字典的主要区别是什么?
查看>>
python 字符串中特定字符替换,截取
查看>>
Python 字符串总结
查看>>
python 字符串显示中文_Python字符串开头的b"、u"、r"与中文乱码
查看>>
Python 字符串的几种拼装方式
查看>>
python 存入数据库bigint_python基础_MySQL的bigint类型
查看>>
python 学习 面向对象编程
查看>>
Python 学习小结
查看>>
Python 学习日记第三篇 -- 字典
查看>>
Python 学习笔记(一)Data type
查看>>
python 安装 easy_install 和 pip 流程
查看>>
python 安装echarts
查看>>
python 安装opencv及问题解决
查看>>