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

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

已测量过的View生成Bitmap

         即经过测量、布局、绘制并显示在界面上的View,此类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

        直接Inflate后并未显示在界面的View,此类View必须手动进行测量和布局后,方可进行绘制,否则获取不到对应的宽高和内容。

/** * 先测量和布局,再生成Bitmap */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;}

更新、填充布局中view的内容显示

public void layoutView(final View viewBitmap,  String url, Activity activity) {    final ImageView imageView = viewBitmap.findViewById(R.id.iv_show);    //注意加载网络图片时一定要用SimpleTarget回调    Glide.with(activity).asBitmap().load(url).into(new SimpleTarget
() { @Override public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition
transition) { //图片加载完毕回调,可以在这里完成整个页面bitmap的转换 imageView.setImageBitmap(resource); Bitmap bitmap = getBitmap(viewBitmap); //保存到本地 savePhotoToSDCard(bitmap ); } });}

保存到本地(切记在清单文件里面增加sd卡的相关权限、动态申请等操作)

public static void savePhotoToSDCard(Bitmap photoBitmap) {        FileOutputStream fos;        String imagePath = "";        try {            // 判断手机设备是否有SD卡            boolean isHasSDCard = Environment.getExternalStorageState().equals(                    android.os.Environment.MEDIA_MOUNTED);            if (isHasSDCard) {                // SD卡根目录                File sdRoot = Environment.getExternalStorageDirectory();                File file = new File(sdRoot, Calendar.getInstance().getTimeInMillis()+".png");                fos = new FileOutputStream(file);                imagePath = file.getAbsolutePath();            } else{                throw new Exception("创建文件失败!");            }            cachebmp.compress(Bitmap.CompressFormat.PNG, 90, fos);            fos.flush();            fos.close();        } catch (Exception e) {            e.printStackTrace();        }    }

其实里面的坑有好多,也是参考了好几篇文章,才解决了这个需求,谢谢以下的文章作者,同时也给大家一个参考。

(动态改变布局中的imageview的图片)
 

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

你可能感兴趣的文章
mySQL和Hive的区别
查看>>
MySQL和Java数据类型对应
查看>>
mysql和oorcale日期区间查询【含左右区间问题】
查看>>
MySQL和SQL入门
查看>>
mysql在centos下用命令批量导入报错_Variable ‘character_set_client‘ can‘t be set to the value of ‘---linux工作笔记042
查看>>
Mysql在Linux运行时新增配置文件提示:World-wrirable config file ‘/etc/mysql/conf.d/my.cnf‘ is ignored 权限过高导致
查看>>
Mysql在Windows上离线安装与配置
查看>>
MySQL在渗透测试中的应用
查看>>
Mysql在离线安装时启动失败:mysql服务无法启动,服务没有报告任何错误
查看>>
Mysql在离线安装时提示:error: Found option without preceding group in config file
查看>>
MySQL基于SSL的主从复制
查看>>
Mysql基本操作
查看>>
mysql基本操作
查看>>
mysql基础
查看>>
mysql基础---mysql查询机制
查看>>
MySQL基础5
查看>>
MySQL基础day07_mysql集群实例-MySQL 5.6
查看>>
Mysql基础命令 —— 数据库、数据表操作
查看>>
Mysql基础命令 —— 系统操作命令
查看>>
MySQL基础学习总结
查看>>