前段时间在网上看到这么个例子是将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. javascript实现数据结构与算法系列:循环链表与双向链表

    循环链表(circular linked list) 是另一种形式的链式存储结构.它的特点是表中最后一个结点的指针域指向头结点,整个表形成一个环. 循环链表的操作和线性链表基本一致,仅有细微差别. w ...

  2. mysql触发器使用实例

    DELIMITER $$ USE `db`$$ DROP TRIGGER `member_walletinit_trigger`$$ CREATE TRIGGER `member_walletinit ...

  3. sshd_config配置 详解

    原文:http://blog.licess.org/sshd_config/ # 1. 关于 SSH Server 的整体设定,包含使用的 port 啦,以及使用的密码演算方式 Port 22 # S ...

  4. POJ 2182

    #include <iostream> #define MAXN 8005 using namespace std; int _m[MAXN]; int main() { //freope ...

  5. XCODE快捷键个人总结

    1.在方法名上用CMD+左键 可以查看完整的方法名

  6. C# DES 加密 解密

    //注意:密钥必须为8位 private const string m_strEncryptKey = "abcd1234"; /// <summary> /// 加密 ...

  7. 微软VSS的超级BUG

    发现问题:今天一个新同事,无意中发现他直接就登录进VSS了,并且还是“admin”用户: 解决问题:于是开始在网上找度娘和谷歌帮忙,真是不查不要紧,一查吓一跳,VSS本身就存在这个bug,并且是一个超 ...

  8. 546B. Soldier and Badges

    题目链接 题意: n个数,要保证这n个数完全不相同,求需要把原来的数增加多少,求这个值得最小值   Java 程序 import java.io.PrintStream; import java.ut ...

  9. Eclipse中WEB项目自动部署到Tomcat

    原因 很长时间没用Eclipse了,近期由于又要用它做个简单的JSP项目,又要重新学习了,虽然熟悉的很快,但记忆总是很模糊,偶尔犯错,以前很少写博客,现在感觉还是很有必要的,编程中每个人对于犯过的错误 ...

  10. ios开发与安卓开源项目及库

    自己总结的iOS.mac开源项目及库 https://github.com/Tim9Liu9/TimLiu-iOS 自己总结的Android开源项目及库 https://github.com/Tim9 ...