Android實現指定地區截屏的實現

來源:互聯網
上載者:User
  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.     }   

然後為我們計算出來的座標地區添加選中效果

  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);

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.