android脚步---将layout和其输出一起保存成图片
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脚步---将layout和其输出一起保存成图片的更多相关文章
- android脚步---设置layout隐藏属性
设置layout的属性,应用到android view的setVisibility 有三个值 visibility VISIBLE, INVISIBLE, GONE. 可见的 不可见的 ...
- 【Android Studio安装部署系列】十六、Android studio在layout目录下新建子目录
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 概述 一般用于分类显示不同模块的layout布局文件. 在res/layout文件夹下创建子目录 res/layout鼠标右键——New— ...
- Android - HelloWorld的Layout内容
Android - HelloWorld的Layout内容 本文地址: http://blog.csdn.net/caroline_wendy 作为最基础的Android程序, HelloWorld的 ...
- Android教程-02 在程序中输出Log
视频教程,建议采用超清模式观看 在Android中一般都用Log输出日志,常见的有5个不同的级别 Log.v() Log.d() Log.i() Log.w() Log.e() 当然很多程序员还比较习 ...
- Android 高清加载巨图方案 拒绝压缩图片
Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...
- Android(java)学习笔记182:保存数据到SD卡 (附加:保存数据到内存)
1. 如果我们要想读写数据到SD卡中,首先必须知道SD的路径: File file = new File(Environment.getExternalStorageDirectory()," ...
- Android(java)学习笔记238:多媒体之图片画画板案例
1.首先我们编写布局文件activity_main.xml如下: <RelativeLayout xmlns:android="http://schemas.android.com/a ...
- Android——用户登陆及用户名和密码的保存
Android——用户登陆及用户名和密码的保存 在之前的学习过程中已经将Android学习完了,但是在后面将近一年的时间里都没有进行过Android开发,所以对Android的所有的知识点又有点忘 ...
- Java乔晓松-android中调用系统拍照功能并显示拍照的图片
android中调用系统拍照功能并显示拍照的图片 如果你是拍照完,利用onActivityResult获取data数据,把data数据转换成Bitmap数据,这样获取到的图片,是拍照的照片的缩略图 代 ...
随机推荐
- 学习笔记——迭代器模式Iterator
迭代器模式,使用很多,但是很少实现.常用的集合都支持迭代器. 集合中的CreateIterator()可用于创建自己的迭代器,在里面通过调用迭代器的构造函数Iterator(Aggregate)来绑定 ...
- 文本注释系统 + MarkDown
标记系统: 笔记的要点 题材 缘起 目标 等级: 细节性 事实性 规律 法则 适用范围: 时间.地点.人物.起因.经过.结果,who what where when why how whom 6W1H ...
- 转:java.io.IOException: Exceeeded maximum number of redirects: 5 解决版本
Jmeter运行的时候出现的重定向超过n次的问题: When trying to test a Silverlight application, I get the below error. Has ...
- php给数组key:value中的key加引号
public function Ext_json_decode($str, $mode=false){ if(preg_match('/\w:/', $str)){ $str = preg_repla ...
- Cross compile openwrt
在Centos7上交叉编译生成OpenWrt固件 安装ss-* 获取最新的ss, 当前是 wget https://github.com/shadowsocks/shadowsocks-libev/a ...
- 《Windows驱动开发技术详解》之驱动程序的同步处理
中断请求级 中断请求被分为软件中断和硬件中断两种,这些中断都映射成不同级别的中断请求级.每个中断请求都有各自的优先级别,正在运行的线程随时都可以被中断打断,进入到中断处理程序.优先级高的中断来临时,处 ...
- 《Windows驱动开发技术详解》之读写操作
缓冲区方式读写操作 设置缓冲区读写方式:
- 第九节,基本条件语句if
条件语句 如果我们希望有效的响应用户的输入,代码就需要具有判断能力.能够让程序进行判断的结构成为条件,条件判断语句返回的是布尔值真或假,真就执行一条线路,假就执行另外一条线路 注意if判断如果怎样,否 ...
- IoC容器Autofac正篇之简单实例(四)
先上一段代码. namespace ConsoleApplication3 { class Program { static void Main(string[] args) { ContainerB ...
- 自定义 IP 地址
可以在安装的时候,点击网络配置 1.修改网卡配置 编辑:vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0 #描述网卡对应的设备别名,例如 ...