你好,游客 登录 注册 搜索
背景:
阅读新闻

Android实现指定区域截屏的实现【附源码】

[日期:2012-03-09] 来源:Linux社区  作者:androidll [字体:大 中 小]

其实实现方式很简单,首先我们需要做的就是截取整个屏幕生成一个bitmap对象

Android实现指定区域截屏的实现源码下载:

免费下载地址在 http://linux.linuxidc.com/

用户名与密码都是www.linuxidc.com

具体下载目录在 /2012年资料/3月/9日/Android实现指定区域截屏的实现【附源码】/

  1. View view = activity.getWindow().getDecorView();   
  2. view.setDrawingCacheEnabled(true);   
  3. view.buildDrawingCache();   
  4. bitmap = view.getDrawingCache();  
然后我们需呀计算出我们选定区域的坐标点,注意正选和反选的计算方式不同 ,
  1. public boolean onTouch(View v, MotionEvent event) {   
  2.      if(event.getAction() == MotionEvent.ACTION_DOWN){   
  3.             x = 0;   
  4.             y = 0;   
  5.             width = 0;   
  6.             height = 0;   
  7.             x = (int) event.getX();   
  8.             y = (int) event.getY();   
  9.         }   
  10.       if(event.getAction() == MotionEvent.ACTION_MOVE){   
  11.             m = (int) event.getX();   
  12.             n = (int) event.getY();   
  13.             myView.setSeat(x, y, m, n);   
  14.             myView.postInvalidate();   
  15.         }   
  16.       if(event.getAction() == MotionEvent.ACTION_UP){   
  17.     if(event.getX()>x){   
  18.          width = (int)event.getX()-x;   
  19.     }else{   
  20.          width = (int)(x-event.getX());   
  21.           x = (int) event.getX();   
  22.     }   
  23.                  if(event.getY()>y){   
  24.          height = (int) event.getY()-y;   
  25.      }else{   
  26.          height = (int)(y-event.getY());   
  27.           y = (int) event.getY();   
  28.     }   
  29.     image2.setImageBitmap(getBitmap(this));   
  30.         }   
  31.     if(myView.isSign()){   
  32.          return false;   
  33.     }else{   
  34.           return true;   
  35.     }   
  36. } 

然后为我们计算出来的坐标区域添加选中效果

  1. protected void onDraw(Canvas canvas) {   
  2.          if(sign){   
  3.     paint.setColor(Color.TRANSPARENT);   
  4.          }else{   
  5.     paint.setColor(Color.RED);   
  6.     paint.setAlpha(80);   
  7.     canvas.drawRect(new Rect(x, y, m, n), paint);   
  8.            }   
  9.     super.onDraw(canvas);   
  10. }  

最后生成我们需要的图片展示出来,顺便保存到SD卡下一张。

  1. Rect frame = new Rect();   
  2.       activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);   
  3.       int toHeight = frame.top;   
  4.       bitmap = Bitmap.createBitmap(bitmap, x, y+2*toHeight, width, height);   
  5.       try {   
  6.     FileOutputStream fout = new FileOutputStream("mnt/sdcard/test.png");   
  7.     bitmap.compress(Bitmap.CompressFormat.PNG, 100, fout);   
  8.         } catch (FileNotFoundException e) {   
  9.             // TODO Auto-generated catch block   
  10.             e.printStackTrace();   
  11.         }   
  12.         view.setDrawingCacheEnabled(false);  
linux
本文评论   查看全部评论 (6)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款
第 6 楼
* 匿名 发表于 2013/11/29 15:26:36
运行 就会报错,bitmap = Bitmap.createBitmap(bitmap, x, y+2*toHeight, width, height);报错代码这一行
第 5 楼
* 匿名 发表于 2013/5/30 21:29:13
求发一份源码可以吗?444578966@qq.com 感谢~
第 4 楼
* 图 发表于 2012/7/11 10:36:43
帅锅 根本下不了啊
第 3 楼
* d 发表于 2012/3/30 16:20:48
不能下
第 2 楼
* 小俞 发表于 2012/3/9 17:28:24
你那个源代码下不下来。能发我邮箱吗?miss__yu@163.com谢谢