前段时间在网上看到这么个例子是将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. JAVA算法系列 冒泡排序

    java算法系列之排序 手写冒泡 冒泡算是最基础的一个排序算法,简单的可以理解为,每一趟都拿i与i+1进行比较,两个for循环,时间复杂度为 O(n^2),同时本例与选择排序进行了比较,选择排序又叫直 ...

  2. Python中的正则表达式regular expression

    1 match = re.search(pat,str)  If the search is successful, search() returns a match object or None o ...

  3. Ubuntu下配置Docbook环境

    1.准备环境 $sudo apt-get install xsltproc $sudo apt-get install docbook-xsl $sudo apt-get install docboo ...

  4. INSERT IGNORE 与INSERT INTO的区别

      INSERT IGNORE 与INSERT INTO的区别就是INSERT IGNORE会忽略数据库中已经存在 的数据,如果数据库没有数据,就插入新的数据,如果有数据的话就跳过这条数据.这样就可以 ...

  5. 腾讯开源的轻量级CSS3动画库:JX.Animate

          JX.Animate 是由腾讯前端团队 AlloyTeam 推出的一个 CSS3 动画库,通过 JX(腾讯的前端框架)插件的形式提供. Why CSS3 众所周知在支持HTML5的浏览器中 ...

  6. css内边距与外边距的区别

    你真的了解margin吗?你知道margin有什么特性吗?你知道什么是垂直外边距合并?margin在块元素.内联元素中的区别?什么时候该用 padding而不是margin?你知道负margin吗?你 ...

  7. uva 11374

    Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...

  8. POJ 1151 Atlantis(经典的线段树扫描线,求矩阵面积并)

    求矩阵的面积并 采用的是区间更新 #include <iostream> #include <stdio.h> #include <string.h> #inclu ...

  9. isMobile 一个简单的JS库,用来检测移动设备

    点这里 github地址:https://github.com/kaimallea/isMobile Example Usage I include the minified version of t ...

  10. [C++]类的继承与派生

    继承性是面向对象程序设计的第二大特性,它允许在既有类的基础上创建新类,新类可以继承既有类的数据成员和成员函数,可以添加自己特有的数据成员和成员函数,还可以对既有类中的成员函数重新定义.利用类的继承和派 ...