截取指定区域的并保存图片

  1. view.setDrawingCacheEnabled(true);   
  2. view.buildDrawingCache();   
  3. 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.     }   

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

  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);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值