前段时间在网上看到这么个例子是将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:

在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null,所以在一个论坛里查到了正确的使用方法.代码如下:

        
  1. contentLayout.setDrawingCacheEnabled(true);
  2. contentLayout.measure(
  3. MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),
  4. MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
  5. contentLayout.layout(0,0,contentLayout.getMeasuredWidth(),
  6. contentLayout.getMeasuredHeight());
  7. contentLayout.buildDrawingCache();
  8. Bitmapbitmap=contentLayout.getDrawingCache();

在使用的时候调用

Bitmap bitmap = view.getDrawingCache();

就可以得到图片的bitmap了。

为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。

setview的代码:

        
  1. publicvoidonCreate(BundlesavedInstanceState){
  2. super.onCreate(savedInstanceState);
  3. setContentView(R.layout.set_view);
  4. contentLayout=(LinearLayout)findViewById(R.id.content);
  5. imgSource1=(ImageView)findViewById(R.id.imgSource1);
  6. imgSource2=(ImageView)findViewById(R.id.imgSource2);
  7. imgCache=(ImageView)findViewById(R.id.imgCache);
  8. imgSource1.setImageResource(R.drawable.source1);
  9. imgSource2.setImageResource(R.drawable.source2);
  10. contentLayout.setDrawingCacheEnabled(true);
  11. contentLayout.measure(
  12. MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED),
  13. MeasureSpec.makeMeasureSpec(0,MeasureSpec.UNSPECIFIED));
  14. contentLayout.layout(0,0,contentLayout.getMeasuredWidth(),
  15. contentLayout.getMeasuredHeight());
  16. contentLayout.buildDrawingCache();
  17. Bitmapbitmap=contentLayout.getDrawingCache();
  18. if(bitmap!=null){
  19. imgCache.setImageBitmap(bitmap);
  20. }else{
  21. Log.i("CACHE_BITMAP","DrawingCache=null");
  22. }
  23. }

第二种方法代码:

        
  1. privatevoidaddRelativeLayout(){
  2. //TODOAuto-generatedmethodstub
  3. RelativeLayout.LayoutParamslayoutpare=newRelativeLayout.LayoutParams(
  4. LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
  5. RelativeLayoutrelativeLayout=newRelativeLayout(this);
  6. relativeLayout.setLayoutParams(layoutpare);
  7. ImageViewimgView1=newImageView(this);
  8. ImageViewimgView2=newImageView(this);
  9. imgView1.setImageResource(R.drawable.source1);
  10. imgView2.setImageResource(R.drawable.source2);
  11. RelativeLayout.LayoutParamsimg1=newRelativeLayout.LayoutParams(38,
  12. 38);
  13. img1.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
  14. RelativeLayout.LayoutParamsimg2=newRelativeLayout.LayoutParams(38,
  15. 38);
  16. img2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
  17. relativeLayout.addView(imgView1,img1);
  18. relativeLayout.addView(imgView2,img2);
  19. addViewContent.addView(relativeLayout);
  20. }
  21. /**
  22. *添加图片源
  23. */
  24. privatevoidaddImgSource(){
  25. ImageViewimgView1=newImageView(this);
  26. ImageViewimgView2=newImageView(this);
  27. imgView1.setImageResource(R.drawable.source1);
  28. imgView2.setImageResource(R.drawable.source2);
  29. addViewContent.addView(imgView1,newLayoutParams(
  30. LinearLayout.LayoutParams.WRAP_CONTENT,
  31. LinearLayout.LayoutParams.WRAP_CONTENT));
  32. addViewContent.addView(imgView2,newLayoutParams(
  33. LinearLayout.LayoutParams.WRAP_CONTENT,
  34. LinearLayout.LayoutParams.WRAP_CONTENT));
  35. }

文章参考自:http://www.iteye.com/topic/1097916

更多相关文章

  1. android中关于使用scrollview嵌套LinearLayout,页面滚动条不到底
  2. Android中程序向桌面和Launcher添加快捷方式【安卓进化三十二】
  3. js如何判断用户是在PC端和还是移动端访问
  4. Android实现与SQL连接
  5. 谷歌J2ObjC:打破Android应用与iOS应用之间的语言障碍
  6. android碎片Fragment简介详解
  7. 《深入探索Android热修复原理》代码热修复总结
  8. Android适配器进阶之一(单类型抽象适配器)
  9. android通讯录开发

随机推荐

  1. golang 有gc吗
  2. golang 中const是什么
  3. golang 协程占多大内存
  4. golang 指针怎么用
  5. golang 为什么没有枚举
  6. golang 怎么做热更新
  7. GOLANG 为什么还有指针
  8. golang 怎么调用c代码
  9. golang 数组存的是什么
  10. golang 怎么设计一个栈