前段时间在网上看到这么个例子是将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));

}

最新文章

  1. 在 iTunes content中创建新的版本时,出现构建版本后面没有加号。
  2. 十大经典排序算法总结——JavaScrip版
  3. 在 SharePoint Server 2013 中配置建议和使用率事件类型
  4. Spark目录
  5. [译]开始学习webpack
  6. Echart 官网给的一个直观的事例
  7. Win8开发疑问与解答
  8. QNX 线程 调度策略 优先级 时钟频率 同步
  9. Android RoboGuice 使用指南
  10. JqueryMobile基础之创建页面
  11. Thanks David's Share
  12. SpringCloud之初识Feign ----- 分布式负载自动拼接请求的URL
  13. MT【257】任意存在并存
  14. How To Install WildFly as a Service on Linux
  15. WPF-利用Blend写的平面控制闸门开关动画
  16. POJ - 2299 Ultra-QuickSort(归并排序)
  17. 查看运行中的Java其配置的堆大小
  18. ubuntu ssh 连接加速
  19. 深入理解Aspnet Core之Identity(4)
  20. apache测试网页执行效率

热门文章

  1. linux下安装vsftp
  2. 如何开始你的CTF比赛之旅-网站安全-
  3. Request/Server的相关topic
  4. 测试c语言函数调用性能因素之测试三
  5. Sqli-labs less 31
  6. 当你碰到一个网络中有多个PXE Server 肿么办?
  7. iOSpush过后返回多级界面
  8. SQL技术内幕-7 varchar类型的数字和 int 类型的数字的比较+cast的适用
  9. (转)Android学习进阶路线导航线路(Android源码分享)
  10. Linux信号处理2