zoukankan      html  css  js  c++  java
  • android 关于截屏

    在android4.0以下用这个方法:

    private void cutScreen()
    	{
    		View v = getLayoutInflater().inflate(R.layout.activity_main, null);
    		//打开图像缓存
    		v.setDrawingCacheEnabled(true);
    		//v.buildDrawingCache();
    		//测量view的大小
    		v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, 
    				MeasureSpec.UNSPECIFIED));
    				
    		//发送位置和尺寸到View及其所有的子View
    		/*View v = this.getWindow().getDecorView();
    		v.setDrawingCacheEnabled(true);
    		v.buildDrawingCache();*/
    	
    		v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    		LogHelper.LogV("" + v.getMeasuredWidth() + " " + v.getMeasuredHeight());
    		
    		try {
    			Bitmap bitmap = v.getDrawingCache();
    			FileOutputStream fos = new FileOutputStream("/sdcard/test.png");
    			bitmap.compress(CompressFormat.PNG, 100, fos);
    			fos.close();
    			LogHelper.LogE("截屏成功");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			LogHelper.LogE("截屏失败 + " + e.getMessage());
    			e.printStackTrace();
    		}
    	}

    如果是android 4.0以上可以这样用:

    private void cutScreen()
    	{
    	/*	View v = getLayoutInflater().inflate(R.layout.activity_main, null);
    		//打开图像缓存
    		v.setDrawingCacheEnabled(true);
    		//v.buildDrawingCache();
    		//测量view的大小
    		v.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, 
    				MeasureSpec.UNSPECIFIED));*/
    				
    		//发送位置和尺寸到View及其所有的子View
    		View v = this.getWindow().getDecorView();
    		v.setDrawingCacheEnabled(true);
    		v.buildDrawingCache();
    	
    		v.layout(0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
    		LogHelper.LogV("" + v.getMeasuredWidth() + " " + v.getMeasuredHeight());
    		
    		try {
    			Bitmap bitmap = v.getDrawingCache();
    			FileOutputStream fos = new FileOutputStream("/sdcard/test.png");
    			bitmap.compress(CompressFormat.PNG, 100, fos);
    			fos.close();
    			LogHelper.LogE("截屏成功");
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			LogHelper.LogE("截屏失败 + " + e.getMessage());
    			e.printStackTrace();
    		}
    	}
  • 相关阅读:
    数据结构 课程安排 (拓扑排序)
    数据结构 通畅工程 (最小生成树)
    01 C#基础
    计算机组成原理——第一章 系统概述
    数据结构——第八章 排序 第九章 文件
    数据结构——第七章 查找
    字符编码(转)
    数据结构——第六章 图
    NodeJS加密算法(转)
    入职总结
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3074157.html
Copyright © 2011-2022 走看看