android-------截屏的两种方法
来源:互联网 发布:不用网络的手机收音机 编辑:程序博客网 时间:2024/06/06 18:49
在android中截屏是一种很常用的功能,现在来实现一下android的截屏。
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:id="@+id/rootLayout" tools:context=".MainActivity" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/my_button" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:text="截屏方式二" > </Button> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/my_button" android:layout_centerHorizontal="true" android:text="截屏方式一" /></RelativeLayout>
因为截屏得到的图片保存在sd卡上,因此要加上以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
第一种方法截屏:
bt1=(Button) findViewById(R.id.button1); bt1.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub //Context context = getApplicationContext(); View rootView = findViewById(R.id.rootLayout); Bitmap newb = Bitmap.createBitmap( 320, 480, Config.ARGB_8888 ); Canvas canvas = new Canvas(newb); rootView.draw(canvas); File file = new File(Environment.getExternalStorageDirectory().getPath()+ "/" + "1.png"); FileOutputStream f = null; try { f = new FileOutputStream(file); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } boolean b = newb.compress(Bitmap.CompressFormat.PNG, 100, f); if(b){ //截图成功 Toast.makeText(MainActivity.this, "截图成功", 1).show(); } } });
截图效果:
这种方法截屏是没有截取标题栏的,并且中间的白色部分根本就没显示出来,都是透明色的。
第二种方法截屏:
this.button = (Button) this.findViewById(R.id.my_button); this.button.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss", Locale.US); //String fname ="/sdcard/"+sdf.format(new Date()) + ".png"; String fname =Environment.getExternalStorageDirectory().getPath()+"/"+sdf.format(new Date()) + ".png"; View view = v.getRootView(); view.setDrawingCacheEnabled(true); view.buildDrawingCache(); Bitmap bitmap = view.getDrawingCache(); if(bitmap!= null){ try{ FileOutputStream out = new FileOutputStream(fname); bitmap.compress(Bitmap.CompressFormat.PNG,100, out); Toast.makeText(MainActivity.this, "截图成功", 1).show(); }catch(Exception e) { e.printStackTrace(); } }else{ Toast.makeText(MainActivity.this, "截图失败", 1).show(); } } });
截图效果:
这种方法截屏把标题栏也截取进去了
运行代码后再sd卡上生成以下文件:
观察上面的生成的文件的大小,发现第一种方法生成的文件比较小(没有截取标题栏),若对内存要求比较高的可以选择第一种方法截图。
源代码下载地址:http://download.csdn.net/detail/dangnianmingyue_gg/9092983
0 0
- android-------截屏的两种方法
- Android使用Handler的两种方法
- android 获取Bitmap 的两种方法
- Android全屏的两种方法
- android 启动 service 的两种方法
- android menu的两种实现方法
- Android两种设置全屏的方法
- Android全屏的两种方法
- Android 注册BroadcastReceiver的两种方法
- Android添加optionsmenu的两种方法
- Android的语言设置两种方法
- Android 全屏显示的两种方法
- Android 实现全屏的两种方法
- Android获得屏幕分辨率的两种方法
- Android访问Wcf的两种方法
- android 控件监听的两种方法
- Android的两种拍照方法
- Android--TabHost的两种实现方法
- Java获取某个月的天数
- 在window 下安装Memcache详解
- java IO流分析,java文件读写
- 转载 [置顶] 计算机视觉领域的一些牛人博客,超有实力的研究机构等的网站链接
- 个人经验不能代替需求
- android-------截屏的两种方法
- 利用脚本修复WINDOWS引导文件丢失
- OpenGL-----Removing Artifacts In Warps
- 文本查询程序
- 61单片机项目 - 定时器Timer A/B
- 移动端如何调用服务端
- 冬天来了,要加强锻炼,克服身体酸麻,懒散,多学习专业知识,不为生活,为了活着。
- 排序
- 关于hashCode