android脚步---将layout和其输出一起保存成图片
2023-09-14 08:59:07 时间
public void convertViewToBitmap(View view) { //View view = getLayoutInflater().inflate(R.layout.test_layout, null); view.setDrawingCacheEnabled(true); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); bitmap = view.getDrawingCache(); Date date = new Date(System.currentTimeMillis()); Storage.writeBitmapFile("/sdcard/DCIM/UUCamera/test"+date.toLocaleString()+".jpeg", bitmap); // Testbt.setVisibility(View.VISIBLE); view.destroyDrawingCache(); }
输入layout对应的vIEW然后进行操作,保存时应用了date,这样每张图片的保存名不同。
应该要注意的是view.measure和 view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());对layout重新进行计算高度和宽度,所以layout务必要封装好,不然会出现移位
以下面layout为例
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/screen" android:background="@drawable/test_bg" android:gravity="center_horizontal" android:orientation="vertical"> <RelativeLayout android:id="@+id/uupic" android:layout_width="304dp" android:layout_height="380dp" android:layout_marginTop="90dp" android:background="@drawable/test_photo_bg" android:gravity="center" > <ImageView android:id="@+id/myuupic" android:layout_width="264dp" android:layout_height="352dp"/> </RelativeLayout> <RelativeLayout android:id="@+id/lovepic" android:layout_height="116dp" android:layout_width="match_parent" > <TextView android:id="@+id/agetext" android:layout_width="120dp" android:layout_height="116dp" android:gravity="center" android:textColor="#ffffff" android:textSize="@dimen/age_text_size" android:background="@drawable/test_age_bg"/> <ImageView android:id="@+id/genderpic" android:layout_width="44dp" android:layout_height="44dp" android:layout_toRightOf="@+id/agetext" android:layout_alignParentBottom="true" android:layout_marginLeft="-30dp" android:scaleType="fitEnd"/> <ImageView android:id="@+id/testimg" android:layout_width="222dp" android:layout_height="86dp" android:layout_toRightOf="@+id/agetext" android:layout_alignParentBottom="true" /> </RelativeLayout> </LinearLayout> <!-- share and save layout --> <RelativeLayout android:id="@+id/testbt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentBottom="true" android:layout_marginLeft="24dp" android:layout_marginRight="24dp" android:layout_marginBottom="20dp" > <Button android:id="@+id/save" android:layout_width="148dp" android:layout_height="40dp" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:background="@drawable/testbtstyle" android:text="保存" android:textColor="#ff4c6c" android:textSize="20sp" /> <Button android:id="@+id/share" android:layout_width="148dp" android:layout_height="40dp" android:layout_alignParentRight="true" android:layout_alignParentTop="true" android:background="@drawable/testbtstyle" android:text="分享" android:textColor="#ff4c6c" android:textSize="20sp" /> </RelativeLayout> </RelativeLayout>
封装成两部分上边显示,下边隐藏
引用时
mScreenShot = mTestView.findViewById(R.id.screen); public void convertViewToBitmap(View view) { //View view = getLayoutInflater().inflate(R.layout.test_layout, null); view.setDrawingCacheEnabled(true); view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); bitmap = view.getDrawingCache(); Date date = new Date(System.currentTimeMillis()); Storage.writeBitmapFile("/sdcard/DCIM/UUCamera/test"+date.toLocaleString()+".jpeg", bitmap); // FileOutputStream fos = new FileOutputStream("/sdcard/DCIM/UUCamera/test.png"); //将Bitmap对象中的图像数据压缩成png格式的图像数据,并将这些数据保存在test.png文件中 // bitmap.compress(CompressFormat.PNG, 100, fos); //关闭文件输出流 // Testbt.setVisibility(View.VISIBLE); view.destroyDrawingCache(); if(!ispicsave()) { return; } } case R.id.save: LogMan.e(TAG,"save..."); if(mScreenShot!=null){ // Testbt.setVisibility(View.INVISIBLE); convertViewToBitmap(mScreenShot); } // Testbt.setVisibility(View.VISIBLE); break;
相关文章
- android studio不能输入中文_Android模拟器
- android 置灰不可点击,Android Studio 运行按钮灰色的完美解决方法
- android开机动画多长时间_Android开机动画原理分析
- android签名命令行,Android系统签名位置及命令
- strictmode android,Android 应用性能优化-StrictMode(严格模式)
- android toast 自定义时间,Android 自定义 Toast 显示时间「建议收藏」
- Android preference_android studio preview
- Android n_android 反编译
- android调用相册并显示图片_Android获取相册列表
- android 横向滑动分类效果实现
- 【错误记录】Flutter / Android 报错 ( AAPT: error: attribute android:requestLegacyExternalStorage not found )
- 【Android 安装包优化】开启资源压缩 ( 资源压缩配置 | 启用严格模式的资源引用检查 | 自定义保留/移除资源配置 | 资源压缩效果 )
- 【Android 逆向】类加载器 ClassLoader ( 类加载时机 | 隐式加载 | 显示加载 | 类加载步骤 | 装载 | 链接 | 初始化 )
- 【Android Gradle 插件】Module 目录下 build.gradle 配置文件 ( android 闭包块配置 | AppExtension 扩展类型参考文档 )
- 【错误记录】编译 Android 版本的 ijkplayer 报错 ( You must define ANDROID_NDK before starting. | 下载指定版本 NDK )
- 【错误记录】Android Studio 打包 apk 文件报错 ( The destination folder does not exist or is not writeable )
- 【Android Gradle 插件】自定义 Gradle 任务 ⑬ ( DefaultTask 中的任务输入和输出属性 | TaskInputs 任务输入接口 | FileCollection )
- 【错误记录】Android Studio 集成 ARoute 编译报错 ( 兼容 support 库和 androidx 库 | add ‘tools:replace=“android:appCo )
- 华为手机Android Studio的Logcat无任何内容输出的解决方案详解手机开发
- Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局详解手机开发
- android Universal Image Loader for Android 说明文档 (1)详解手机开发
- android 异步加载图片详解编程语言
- Android程序应用的生命周期
- Android开发笔记之:如何安全中止一个自定义线程Thread的方法
- android中Intent传值与Bundle传值的区别详解
- android如何获取MCC/MNC控制小区广播的开启
- Android实现左右滑动效果的方法详解