Android实现指定区域截屏的实现
16lz
2021-01-23
本文转自:
http://blog.csdn.net/hopezhangbo/article/details/7356571
- Viewview=activity.getWindow().getDecorView();
- view.setDrawingCacheEnabled(true);
- view.buildDrawingCache();
- bitmap=view.getDrawingCache();
然后我们需呀计算出我们选定区域的坐标点,注意正选和反选的计算方式不同,
- publicbooleanonTouch(Viewv,MotionEventevent){
- if(event.getAction()==MotionEvent.ACTION_DOWN){
- x=0;
- y=0;
- width=0;
- height=0;
- x=(int)event.getX();
- y=(int)event.getY();
- }
- if(event.getAction()==MotionEvent.ACTION_MOVE){
- m=(int)event.getX();
- n=(int)event.getY();
- myView.setSeat(x,y,m,n);
- myView.postInvalidate();
- }
- if(event.getAction()==MotionEvent.ACTION_UP){
- if(event.getX()>x){
- width=(int)event.getX()-x;
- }else{
- width=(int)(x-event.getX());
- x=(int)event.getX();
- }
- if(event.getY()>y){
- height=(int)event.getY()-y;
- }else{
- height=(int)(y-event.getY());
- y=(int)event.getY();
- }
- image2.setImageBitmap(getBitmap(this));
- }
- if(myView.isSign()){
- returnfalse;
- }else{
- returntrue;
- }
- }
然后为我们计算出来的坐标区域添加选中效果
- protectedvoidonDraw(Canvascanvas){
- if(sign){
- paint.setColor(Color.TRANSPARENT);
- }else{
- paint.setColor(Color.RED);
- paint.setAlpha(80);
- canvas.drawRect(newRect(x,y,m,n),paint);
- }
- super.onDraw(canvas);
- }
最后生成我们需要的图片展示出来,顺便保存到SD卡下一张。
- Rectframe=newRect();
- activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
- inttoHeight=frame.top;
- bitmap=Bitmap.createBitmap(bitmap,x,y+2*toHeight,width,height);
- try{
- FileOutputStreamfout=newFileOutputStream("mnt/sdcard/test.png");
- bitmap.compress(Bitmap.CompressFormat.PNG,100,fout);
- }catch(FileNotFoundExceptione){
- //TODOAuto-generatedcatchblock
- e.printStackTrace();
- }
- view.setDrawingCacheEnabled(false);
更多相关文章
- Android GPS获得经纬度并得到该坐标精确地址
- Android获取点击屏幕的位置坐标
- Android MotionEvent 坐标获取
- Android GPS 获得 经纬度 并得到该 坐标 精确地址
- android Draw Rect 坐标图示
- android view相对于根布局的坐标获取
- Android调用系统Activity选取图像部分区域
- Android开发:布局分区域设置不同背景色
- Android前置摄像头预览并检测人脸,获取人脸区域亮度