- 浏览: 518253 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
tangyunliang:
大哥你太历害了谢谢
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
u013015029:
LZ,请问下,在// 添加消息到聊天窗口 , 这里获取Ed ...
Android基于XMPP Smack Openfire开发IM【四】初步实现两个客户端通信 -
endual:
怎么保持会话,我搞不懂啊
Android基于XMPP Smack Openfire开发IM【一】登录openfire服务器 -
donala_zq:
显示:[2013-11-30 11:50:36 - Andro ...
android-----------新浪微博 -
donala_zq:
哥,运行不了啊
android-----------新浪微博
将view映射到一个bitmap中,稍加改进可以用于一些截图工具或者截图软件(QQ截图之类),例子写的不够完善,不过很有些学习的意义内容大致如下:
在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap。
在使用的时候调用
Bitmap bitmap = view.getDrawingCache();
就可以得到图片的bitmap了。
为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。
工程结构图:
[img]
[/img]
布局文件:
main.xml
add_view.xml
set_view.xml
AddViewActivity
MainActivity
SetViewActivity
转自:http://hddev.blog.51cto.com/3365350/629808
只为学习。
在Android中自有获取view中的cache内容,然后将内容转换成bitmap,方法名是:getDrawingCache(),返回结果为Bitmap。
在使用的时候调用
Bitmap bitmap = view.getDrawingCache();
就可以得到图片的bitmap了。
为了测试这个功能,作者使用了两种方式来创建LinerLayout中的内容,一种是在xml文件中就将view的内容添加了,只需在代码中添加对应ImageView中的图片就行了;另一种是动态添加LinerLayout中的View。
工程结构图:
[img]
[/img]
布局文件:
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <Button android:id="@+id/setview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="SET_VIEW" /> <Button android:id="@+id/addview" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="ADD_VIEW" /> </LinearLayout>
add_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="add_view" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/addViewContent" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/addViewCache" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgAddViewCache" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
set_view.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:text="set_view" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <LinearLayout android:id="@+id/content" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgSource1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <ImageView android:id="@+id/imgSource2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:id="@+id/cache" android:layout_width="wrap_content" android:layout_height="wrap_content"> <ImageView android:id="@+id/imgCache" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
AddViewActivity
package com.zart; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import android.view.View.MeasureSpec; import android.view.ViewGroup.LayoutParams; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; public class AddViewActivity extends Activity { private LinearLayout addViewContent; private ImageView imgAddViewCache; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.add_view); addViewContent = (LinearLayout) findViewById(R.id.addViewContent); imgAddViewCache = (ImageView) findViewById(R.id.imgAddViewCache); // addImgSource(); addRelativeLayout(); addViewContent.setDrawingCacheEnabled(true); addViewContent.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)); addViewContent.layout(0, 0, addViewContent.getMeasuredWidth(), addViewContent.getMeasuredHeight()); addViewContent.buildDrawingCache(); int color = addViewContent.getDrawingCacheBackgroundColor(); Bitmap cacheBitmap = addViewContent.getDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);// 注意:这地方必须特别注意 if (bitmap != null) { imgAddViewCache.setImageBitmap(bitmap); imgAddViewCache.setDrawingCacheBackgroundColor(color); } 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)); } }
MainActivity
package com.zart; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button btn_setView; private Button btn_addView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main); btn_setView = (Button) findViewById(R.id.setview); btn_addView = (Button) findViewById(R.id.addview); btn_setView.setOnClickListener(this); btn_addView.setOnClickListener(this); } @Override public void onClick(View view) { // TODO Auto-generated method stub switch (view.getId()) { case R.id.setview: Intent intent1 = new Intent(); intent1.setClass(this, SetViewActivity.class); startActivity(intent1); break; case R.id.addview: Intent intent2 = new Intent(); intent2.setClass(this, AddViewActivity.class); startActivity(intent2); break; default: break; } } }
SetViewActivity
package com.zart; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.util.Log; import android.view.View.MeasureSpec; import android.widget.ImageView; import android.widget.LinearLayout; public class SetViewActivity extends Activity { /** Called when the activity is first created. */ private LinearLayout contentLayout; private ImageView imgSource1; private ImageView imgSource2; private ImageView imgCache; @Override 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"); } } }
转自:http://hddev.blog.51cto.com/3365350/629808
只为学习。
发表评论
-
Android中如何模拟一次点击(touch)事件
2014-05-06 10:41 0在Android中有时需要模拟某一个View的touch事件, ... -
Android程序Crash时的异常上报
2014-04-28 18:15 0http://blog.csdn.net/singwhatiw ... -
android程序中证书签名校验的方法
2014-04-28 17:58 1907android程序中证书签名校验的方法一 2013-02 ... -
MD5理解错了,哎
2014-03-17 14:14 0MD5只对数据加密是无法解密的,也就是说,你把100加密后,就 ... -
Android 获取网络时间
2014-03-12 11:42 2013Android 获取网络时间 在网上看到的最常见的方式有: ... -
SQLite清空表并将自增列归零
2014-03-05 18:02 1472SQLite清空表并将自增列归零 作者:Zhu Yanfeng ... -
Handler小看一下
2013-11-11 16:42 0android handler调用post方法还是阻塞 su ... -
Frame Animation小看一下
2013-10-12 16:30 724Demo运行效果图: 源码: -
动画小学一下
2013-10-12 16:14 695转自: http://www.eoeandroid.com/f ... -
Android 动画之ScaleAnimation应用详解
2013-10-12 15:49 965===============eoeAndroid社区推荐:= ... -
android开发中的一个工具类
2013-06-19 16:04 0package com.wanpu.login.dialog; ... -
android TextView怎么设置个别字体颜色并换行?
2013-06-20 09:25 1638(1)、TextView 设置个别字体颜色 TextView ... -
Android开发之文件下载,状态时显示下载进度,点击自动安装
2013-05-07 15:38 1371在进行软件升级时,需要进行文件下载,在这里实现自定义的文件下载 ... -
android中的状态保存
2013-04-07 14:21 935package com.zzl.call; import ... -
android动画基础:tween动画
2013-04-06 11:21 1181工程结构图: [img] [/img] 四个动画的xml ... -
面试中遇到的几个问题
2013-06-09 11:56 945SAX与DOM之间的区别 SAX ( ... -
Android获取其他包的Context实例,然后调用它的方法,反射!!!
2013-03-25 10:32 1163Android中有Context的概念,想必大家都知道。Con ... -
Android的内存机制和常见泄漏情形
2013-03-06 16:55 756一、 Android的内存机制 Android的程序由Ja ... -
JUnit测试小小demo
2013-03-06 16:37 1089运行效果图: [img] [/img] 项目结构图 ... -
android开发中的异常小工具
2013-03-04 15:53 864package com.zzl.tools; impor ...
相关推荐
android 把一个view视图转换成bitmap 保存到本地 可以用于分享的局部截屏
Android View转成Bitmap
Android不支持将Bitmap转换成单色的Bmp图片,所以参考Bmp格式说明,自己写了一个转换类。亲测有效!!!
实现了获取界面部分view,生成bitmap图片,也就是view截图,获取指定view的截图,适用于分享截图等
一个在VC中将Bitmap转换为Byte[]的小例子。
将Bitmap转成byte[],进行保存,用到时再取出byte[],转化成Bitmap,进行显示
机器视觉技术必备,从位图到halcon图像,bitmap图像转为halcon图像,bitmap to Hobject
Halcon_Bitmap转换方法 写的文档 开发笔记
Android图片Bitmap和字符串String之间相互转换的工具类,外加项目代码,封装好的,直接可以用,
android图片处理(压缩,保存,截屏,view转化为bitmap)相关函数 ImageUtils
BMP2H264 Bitmap图像转换为H264
软件开发网在此之前给大家介绍过图片加载框架Glide的基本用法介绍,大家可以先参考一下,本篇内容更加深入的分析了Glide获取图片Path、Bitmap用法,以及实现的代码分析。 1. 获取Bitmap: 1)在图片下载缓存好之后...
Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式 博文介绍:http://blog.csdn.net/qq_21376985/article/details/52083611
Bitmap 转 txt 源码
改用原始图像数据流回调函数RegisterImageStreamCallback进行抓图 但回调函数帧为YUV420格式 需要转成Bitmap才用用于显示及本地查看 在网上找了好多都是基于R Y+1 4075 V 128 G Y 0 3455 U 128 – 0 7169 V 128 B Y+...
android里面 canvas 转换成bitmap
如:图片内容为“1”命名为“1.png”,图片内容为“万”命名为“万.png” 2、将该软件放在图片所在文件夹下。 3、运行该软件。①提示“请输入字体大小(如24)”,输入字体的大小,然后回车;②提示“请输入...
注意: DEMO里图片的URL需要更换成你需要下载的URL, 否则我使用...此DEMO实现了将网络服务器上的TIFF或TIF格式的图片下载到本地, 然后转换成BITMAP, 再使用相关VIEW显示出来, 可以是imageview, photoview等.看自己需求.