Android:将View的内容映射成Bitmap转图片导出
前段时间在网上看到这么个例子是将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:
在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null,所以在一个论坛里查到了正确的使用方法.代码如下:
contentLayout.setDrawingCacheEnabled(true);
contentLayout.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),
contentLayout.getMeasuredHeight());
contentLayout.buildDrawingCache();
Bitmap bitmap= contentLayout.getDrawingCache();
在使用的时候调用
Bitmap bitmap = view.getDrawingCache();
就可以得到图片的bitmap了。
为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。

setview的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.set_view);
contentLayout = (LinearLayout) findViewById(R.id.content);
imgSource1 = (ImageView) findViewById(R.id.imgSource1);
imgSource2 = (ImageView) findViewById(R.id.imgSource2);
imgCache = (ImageView) findViewById(R.id.imgCache);
imgSource1.setImageResource(R.drawable.source1);
imgSource2.setImageResource(R.drawable.source2);
contentLayout.setDrawingCacheEnabled(true);
contentLayout.measure(
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
contentLayout.layout(0, 0, contentLayout.getMeasuredWidth(),
contentLayout.getMeasuredHeight());
contentLayout.buildDrawingCache();
Bitmap bitmap= contentLayout.getDrawingCache();
if(bitmap!=null){
imgCache.setImageBitmap(bitmap);
}else{
Log.i("CACHE_BITMAP", "DrawingCache=null");
}
}
第二种方法代码:
private void addRelativeLayout() {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams layoutpare = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(layoutpare);
ImageView imgView1 = new ImageView(this);
ImageView imgView2 = new ImageView(this);
imgView1.setImageResource(R.drawable.source1);
imgView2.setImageResource(R.drawable.source2);
RelativeLayout.LayoutParams img1 = new RelativeLayout.LayoutParams(38,
38);
img1.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams img2 = new RelativeLayout.LayoutParams(38,
38);
img2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
relativeLayout.addView(imgView1, img1);
relativeLayout.addView(imgView2, img2);
addViewContent.addView(relativeLayout);
}
/**
* 添加图片源
*/
private void addImgSource() {
ImageView imgView1 = new ImageView(this);
ImageView imgView2 = new ImageView(this);
imgView1.setImageResource(R.drawable.source1);
imgView2.setImageResource(R.drawable.source2);
addViewContent.addView(imgView1, new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
addViewContent.addView(imgView2, new LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
Android:将View的内容映射成Bitmap转图片导出的更多相关文章
- 同步手绘板——将View的内容映射成Bitmap转图片导出
在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null, ...
- Android之View的内容
View的事件体系 本章介绍View的事件分发和滑动冲突问题的解决方案. 3.1 view的基础知识 View的位置参数.MotionEvent和TouchSlop对象.VelocityTracker ...
- Android中将布局文件转成bitmap
在实践中发现,有些需要打印的小票高度小于屏幕的高度,而有些小票内容过多高度高于屏幕高度. 小于屏幕高度的布局文件转成bitmap较为容易,高于屏幕高度的布局文件转成长图bitmap较为复杂. 一.小于 ...
- android截屏:保存一个view的内容为图片并存放到SD卡
项目中偶尔会用到截屏分享,于是就有了下面这个截屏的方法~ 下面得saveImage()方法就是保存当前Activity对应的屏幕所有内容的截屏保存. private void saveImage() ...
- Android获取View对应的Bitmap
我的应用里面有一个需求,将一个画面分享出去,这个画面底层是一个View,所以首先要把这个View转换成Bitmap,然后在分享这个bitmap即可.话不多说,直接上代码. 有个地方需要注意一下:就是/ ...
- Android中View转换为Bitmap及getDrawingCache=null的解决方法
1.前言 Android中经常会遇到把View转换为Bitmap的情形,比如,对整个屏幕视图进行截屏并生成图片:Coverflow中需要把一页一 页的view转换为Bitmap.以便实现复杂的图形效果 ...
- Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据
Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14 阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...
- bitMap算法实现以及ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串
ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串 说明 1.所谓的BitMap就是用一个bit位来标记某个元素所对应的value,而key即是该元素,由于BitMap使用了bit位来 ...
- 该View转换成Bitmap方法
方法一: /** * 该View绘制到Bitmap上 * @param view 须要绘制的View * @param width 该View的宽度 * @param height 该View的高度 ...
随机推荐
- The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near
The Brain vs Deep Learning Part I: Computational Complexity — Or Why the Singularity Is Nowhere Near ...
- MyEclipse提示键配置、提示快捷键、提示背景色、关键字颜色、代码显示、编...
1.提示键配置 一般默认情况下,Eclipse ,MyEclipse 的代码提示功能是比Microsoft Visual Studio的差很多的,主要是Eclipse ,MyEclipse本身有很多选 ...
- C# 中的 == 和 equals()有什么区别?
如以下代码: 1 2 3 4 5 6 7 8 9 int age = 25; short newAge = 25; Console.WriteLine(age == newAge); //t ...
- javascript实现数据结构:线性表--简单示例及线性表的顺序表示和实现
线性表(linear list)是最常用且最简单的一种数据结构.一个线性表是n个数据元素的有限序列.在稍复杂的线性表中,一个数据元素可以由若干个数据项(item)组成. 其中: 数据元素的个数n定义为 ...
- javascript中onclick事件能调用多个方法吗
Q: javascript中onclick事件能调用多个方法吗? A: 可以的,方法如下onclick="aa();bb();cc();"每个方法用“;”分号隔开就行了
- 字符模型和Windows等价程序
二者很明显的区别,dos和gui 字符模式模型 #include "stdafx.h" int _tmain(int argc, _TCHAR* argv[]){ print ...
- 安卓app缓存设置
无论大型或小型应用,灵活的缓存可以说不仅大大减轻了服务器的压力,而且因为更快速的用户体验而方便了用户. Android的apk可以说是作为小型应用,其中99%的应用并不是需要实时更新的,而且诟病于蜗牛 ...
- 如何在solution中添加一个test case
在solution Explorer中右键点击需要添加的folder,选择Add-New Item.也可以选择使用相应Unit Test之类的.Generic Test一般用于创建manual cas ...
- Hibernate笔记——C3P0配置
Hibernate作为持久层(ORM)框架,操作数据库,自然也就离不开数据库连接池了.其支持多种连接池,这里就用最熟悉的C3P0连接池. C3P0连接池前面已经介绍了并使用很多次了就不再详细说明了. ...
- 【ArcEngine入门与提高】Element(元素)、Annotation(注记)旋转
因项目需要,需要做一个旋转注记的工具.因为注记这玩意用的比较少,网上资源也很少,所以做起来相当头疼.在经过一番研究之后,终于搞清楚注记的存储原理了,原来是和Element的类似,只不过注记是要把Ele ...