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();
    		}
    	}
  • 相关阅读:
    Simpo
    Numpy技巧
    流程图
    注册免费试用12个月的亚马逊AWS云计算服务
    java-Collection
    CoreGraphic
    吴裕雄--天生自然python学习笔记:python 用pygame模块开发俄罗斯方块游戏
    吴裕雄--天生自然python学习笔记:python 用pygame模块检测键盘事件和鼠标事件
    吴裕雄--天生自然python学习笔记:python 用pygame模块角色类(Sprite)移动与碰撞
    吴裕雄--天生自然python学习笔记:python 用pygame模块动画一让图片动起来
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3074157.html
Copyright © 2011-2022 走看看