前段时间在网上看到这么个例子是将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转图片导出的更多相关文章

  1. 同步手绘板——将View的内容映射成Bitmap转图片导出

    在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null, ...

  2. Android之View的内容

    View的事件体系 本章介绍View的事件分发和滑动冲突问题的解决方案. 3.1 view的基础知识 View的位置参数.MotionEvent和TouchSlop对象.VelocityTracker ...

  3. Android中将布局文件转成bitmap

    在实践中发现,有些需要打印的小票高度小于屏幕的高度,而有些小票内容过多高度高于屏幕高度. 小于屏幕高度的布局文件转成bitmap较为容易,高于屏幕高度的布局文件转成长图bitmap较为复杂. 一.小于 ...

  4. android截屏:保存一个view的内容为图片并存放到SD卡

    项目中偶尔会用到截屏分享,于是就有了下面这个截屏的方法~ 下面得saveImage()方法就是保存当前Activity对应的屏幕所有内容的截屏保存. private void saveImage() ...

  5. Android获取View对应的Bitmap

    我的应用里面有一个需求,将一个画面分享出去,这个画面底层是一个View,所以首先要把这个View转换成Bitmap,然后在分享这个bitmap即可.话不多说,直接上代码. 有个地方需要注意一下:就是/ ...

  6. Android中View转换为Bitmap及getDrawingCache=null的解决方法

    1.前言 Android中经常会遇到把View转换为Bitmap的情形,比如,对整个屏幕视图进行截屏并生成图片:Coverflow中需要把一页一 页的view转换为Bitmap.以便实现复杂的图形效果 ...

  7. Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据

    Android中如何将Bitmap byte裸数据转换成Bitmap图片int数据 2014-06-11 10:45:14   阅读375次 我们在JNI中处理得到的BMP图片Raw数据,我们应该如何 ...

  8. bitMap算法实现以及ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串

    ckHash函数类,将字符串映射成数字,同时可以将数字映射成字符串 说明 1.所谓的BitMap就是用一个bit位来标记某个元素所对应的value,而key即是该元素,由于BitMap使用了bit位来 ...

  9. 该View转换成Bitmap方法

    方法一: /** * 该View绘制到Bitmap上 * @param view 须要绘制的View * @param width 该View的宽度 * @param height 该View的高度 ...

随机推荐

  1. Notepad++ 几款实用插件简介,让你的 Notepad++ 如虎添翼

    Notepad++ 是一款非常优秀的文本编辑器,非常适合编辑源代码.Notepad++ 安装时已经附带有 Compare 等优秀插件,通过其 Plugin Manager 可以下载更多实用插件. 一. ...

  2. request 获取请求参数

    /** * 根据request获取请求的用户参数 * @return * @return */ protected <T> T getParamConvertEntity(Class cl ...

  3. Css 单图片按钮实例(css 图片变换)

    1.场景描述,根据鼠标的移动,动态的切换按钮图片. 2.方法1,准备两张120*41的图片,一张正常状态图片,一张按下效果图片.在鼠标放在的按钮上设置按下图片,移开又恢复到正常状态图片.缺点:在网页上 ...

  4. 函数式 CSS (FCSS)

    在Wealthfront我们是一个函数式编程的超级粉丝.强调不变性和函数式风格意味着更少的“意外”(surprises),因为副作用是有限的或不存在的.我们能将独立的组件迅速构建出大型系统,通过组合的 ...

  5. sematext

    https://sematext.atlassian.net/wiki/display/PUBLOGSENE/Syslog

  6. poj 1568 Find the Winning Move 极大极小搜索

    思路:用极大极小搜索解决这样的问题很方便!! 代码如下: #include <cstdio> #include <algorithm> #define inf 10000000 ...

  7. ring0 与 ring3 层之间的交互

    在进行Windows的ring0层开发时,必不可免的要与 ring3 层进行交互.进行数据间的相互传输.可用的方法有DeviceIoCntrol,ReadFile.我平常都是用的DeviceIoCon ...

  8. postgresql数据库的yum安装方法

    实验环境>>>>>>>>>>>>>>>>>>操作系统:CentOS release 6.3 ...

  9. yarn介绍

    hadoop 1.0 mapreduce过程 主要问题: JobTracker 是 Map-reduce 的集中处理点,存在单点故障. JobTracker 完成了太多的任务,造成了过多的资源消耗,当 ...

  10. 应用内存优化之OnLowMemory&OnTrimMemory

    1.应用内存onLowMemory& onTrimMemory优化 onLowMemory& onTrimMemory简介:OnLowMemory是Android提供的API,在系统内 ...