Android:将View的内容映射成Bitmap转图片导出 推荐
2011-08-03 09:04
471 查看
前段时间在网上看到这么个例子是将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:
在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap,但是刚开始使用的时候,得到的结果都是null,所以在一个论坛里查到了正确的使用方法.代码如下:
在使用的时候调用
Bitmap bitmap = view.getDrawingCache();
就可以得到图片的bitmap了。
为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。
setview的代码:
第二种方法代码:
文章参考自:http://www.iteye.com/topic/1097916
附件:http://down.51cto.com/data/2358658
在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)); }
文章参考自:http://www.iteye.com/topic/1097916
附件:http://down.51cto.com/data/2358658
相关文章推荐
- Android:将View的内容映射成Bitmap转图片导出
- Android:将View的内容映射成Bitmap转图片导出
- Android:将View的内容映射成Bitmap转图片导出
- 将View的内容映射成Bitmap转图片导出
- 同步手绘板——将View的内容映射成Bitmap转图片导出
- android webview将网页内容导出为图片和PDF方法
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- Android:将View的内容映射成Bitmap
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byt
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android 将图片内容解析成字节数组,将字节数组转换为ImageView可调用的Bitmap对象,图片缩放,把字节数组保存为一个文件,把Bitmap转Byte
- android TextView加载html内容并加载图片
- android 将View界面保存成图片bitmap
- android TextView、EditText对部分内容设置颜色、字体、超链接、图片
- Android中从ImageView中取出图片bitmap注意事项
- Android Glide Google 推荐加载图片框架(仅获取BitMap)
- 图片缩放插件GestureImageView——Android 常用插件推荐(一)
- Android Glide Google 推荐加载图片框架(仅获取BitMap)