android怎么实现简单的矩形裁剪框
android怎么实现简单的矩形裁剪框
这篇文章主要介绍“android怎么实现简单的矩形裁剪框”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“android怎么实现简单的矩形裁剪框”文章能帮助大家解决问题。
正常模式是这样的
简单的添加了等比例裁剪
贴代码
publicclassCutViewextendsView{floatdownX;floatdownY;booleanisLeft;booleanisRight;booleanisTop;booleanisBottom;booleanisMove;booleanisSlideLeft;booleanisSlideRight;booleanisSlideTop;booleanisSlideBottom;floatrectLeft;floatrectRight;floatrectTop;floatrectBottom;privateintmeasuredWidth;privateintmeasuredHeight;privatePaintpaint;privateintdp3;privateintcornerLength;privateintdp1;privatefloataspect=-1;publicCutView(Contextcontext,AttributeSetattrs,intdefStyleAttr){super(context,attrs,defStyleAttr);init();}publicCutView(Contextcontext,AttributeSetattrs){super(context,attrs);init();}publicCutView(Contextcontext){super(context);init();}privatevoidinit(){dp3=(int)getResources().getDimension(R.dimen.dp3);dp1=(int)getResources().getDimension(R.dimen.dp1);paint=newPaint();paint.setAntiAlias(true);paint.setColor(Color.WHITE);paint.setStyle(Paint.Style.STROKE);}@OverridepublicbooleanonTouchEvent(MotionEventevent){switch(event.getAction()){caseMotionEvent.ACTION_DOWN:downX=event.getX();downY=event.getY();if(downX>=rectLeft&&downX<=rectRight&&downY>=rectTop&&downY<=rectBottom){//判断手指的范围在左面还是右面intw=(int)((rectRight-rectLeft)/3);if(downX>=rectLeft&&downX<=rectLeft+w){isLeft=true;}elseif(downX<=rectRight&&downX>=rectRight-w){isRight=true;}//判断手指的范围在上面还是下面inth=(int)((rectBottom-rectTop)/3);if(downY>=rectTop&&downY<=rectTop+h){isTop=true;}elseif(downY<=rectBottom&&downY>=rectBottom-h){isBottom=true;}//如果手指范围没有在任何边界位置,那么我们就认为用户是想拖拽框体if(!isLeft&&!isTop&&!isRight&&!isBottom){isMove=true;}}break;caseMotionEvent.ACTION_MOVE:floatmoveX=event.getX();floatmoveY=event.getY();//得到手指移动距离floatslideX=moveX-downX;floatslideY=moveY-downY;if(isMove){//判断是否是拖拽模式rectLeft+=slideX;rectRight+=slideX;rectTop+=slideY;rectBottom+=slideY;//同时改变left和right值,达到左右移动的效果if(rectLeft<0||rectRight>measuredWidth){//判断x轴的移动边界rectLeft-=slideX;rectRight-=slideX;}//同时改变top和bottom值,达到上下移动的效果if(rectTop<0||rectBottom>measuredHeight){//判断y轴的移动边界rectTop-=slideY;rectBottom-=slideY;}//实时触发onDraw()方法invalidate();downX=moveX;downY=moveY;}else{if(aspect!=-1){if(isLeft&&(isTop||isBottom)){if(!isSlideLeft&&!isSlideTop&&!isSlideBottom){floatx=Math.abs(slideX);floaty=Math.abs(slideY);if(x>y&&x>10){isSlideLeft=true;}elseif(x<y&&y>10){if(isTop){isSlideTop=true;}else{isSlideBottom=true;}}}}elseif(isRight&&(isTop||isBottom)){if(!isSlideRight&&!isSlideTop&&!isSlideBottom){floatx=Math.abs(slideX);floaty=Math.abs(slideY);if(x>y&&x>10){isSlideRight=true;}elseif(x<y&&y>10){if(isTop){isSlideTop=true;}else{isSlideBottom=true;}}}}elseif(isLeft&&!isSlideLeft){isSlideLeft=true;}elseif(isRight&&!isSlideLeft){isSlideRight=true;}elseif(isTop&&!isSlideTop){isSlideTop=true;}elseif(isBottom&&!isSlideBottom){isSlideBottom=true;}if(isSlideLeft){rectLeft+=slideX;if(rectLeft<0)rectLeft=0;floatw=rectRight-rectLeft;if(w<cornerLength*2){w=cornerLength*2;rectLeft=rectRight-w;}floath=w/aspect;if(h<cornerLength*2){h=cornerLength*2;w=h*aspect;rectLeft=rectRight-w;}if(isTop){rectBottom=rectTop+h;}elseif(isBottom){rectTop=rectBottom-h;}else{floatrh=rectBottom-rectTop;floatt=(rh-h)/2;rectTop+=t;rectBottom-=t;}if(rectTop<0){rectTop=0;rectBottom=h;if(rectBottom>measuredHeight){rectBottom=measuredHeight;}w=rectBottom*aspect;rectLeft=rectRight-w;}elseif(rectBottom>measuredHeight){rectBottom=measuredHeight;rectTop=measuredHeight-h;if(rectTop<0){rectTop=0;}w=(rectBottom-rectTop)*aspect;rectLeft=rectRight-w;}invalidate();downX=moveX;downY=moveY;}elseif(isSlideRight){rectRight+=slideX;if(rectRight>measuredWidth)rectRight=measuredWidth;floatw=rectRight-rectLeft;if(w<cornerLength*2){w=cornerLength*2;rectRight=rectLeft+w;}floath=w/aspect;if(h<cornerLength*2){h=cornerLength*2;w=h*aspect;rectRight=rectLeft+w;}if(isTop){rectBottom=rectTop+h;}elseif(isBottom){rectTop=rectBottom-h;}else{floatrh=rectBottom-rectTop;floatt=(rh-h)/2;rectTop+=t;rectBottom-=t;}if(rectTop<0){rectTop=0;rectBottom=h;if(rectBottom>measuredHeight){rectBottom=measuredHeight;}w=rectBottom*aspect;rectRight=rectLeft+w;}elseif(rectBottom>measuredHeight){rectBottom=measuredHeight;rectTop=measuredHeight-h;if(rectTop<0){rectTop=0;}w=(rectBottom-rectTop)*aspect;rectRight=rectLeft+w;}invalidate();downX=moveX;downY=moveY;}elseif(isSlideTop){rectTop+=slideY;if(rectTop<0)rectTop=0;floath=rectBottom-rectTop;if(h<cornerLength*2){h=cornerLength*2;rectTop=rectBottom-h;}floatw=h*aspect;if(w<cornerLength*2){w=cornerLength*2;h=w/aspect;rectTop=rectBottom-h;}if(isLeft){rectRight=rectLeft+w;}elseif(isRight){rectLeft=rectRight-w;}else{floatrw=rectRight-rectLeft;floatt=(rw-w)/2;rectLeft+=t;rectRight-=t;}if(rectLeft<0){rectLeft=0;rectRight=w;if(rectRight>measuredWidth){rectRight=measuredWidth;}h=rectRight/aspect;rectTop=rectBottom-h;}elseif(rectRight>measuredWidth){rectRight=measuredWidth;rectLeft=measuredWidth-w;if(rectLeft<0){rectLeft=0;w=measuredWidth;}h=w/aspect;rectTop=rectBottom-h;}invalidate();downX=moveX;downY=moveY;}elseif(isSlideBottom){rectBottom+=slideY;if(rectBottom>measuredHeight)rectBottom=measuredHeight;floath=rectBottom-rectTop;if(h<cornerLength*2){h=cornerLength*2;rectBottom=rectTop+h;}floatw=h*aspect;if(w<cornerLength*2){w=cornerLength*2;h=w/aspect;rectBottom=rectTop+h;}if(isLeft){rectRight=rectLeft+w;}elseif(isRight){rectLeft=rectRight-w;}else{floatrw=rectRight-rectLeft;floatt=(rw-w)/2;rectLeft+=t;rectRight-=t;}if(rectLeft<0){rectLeft=0;rectRight=w;if(rectRight>measuredWidth){rectRight=measuredWidth;}h=rectRight/aspect;rectBottom=rectTop+h;}elseif(rectRight>measuredWidth){rectRight=measuredWidth;rectLeft=measuredWidth-w;if(rectLeft<0){rectLeft=0;w=measuredWidth;}h=w/aspect;rectBottom=rectTop+h;}invalidate();downX=moveX;downY=moveY;}}else{if(isLeft){rectLeft+=slideX;if(rectLeft<0)rectLeft=0;if(rectLeft>rectRight-cornerLength*2)rectLeft=rectRight-cornerLength*2;}elseif(isRight){rectRight+=slideX;if(rectRight>measuredWidth)rectRight=measuredWidth;if(rectRight<rectLeft+cornerLength*2)rectRight=rectLeft+cornerLength*2;}//改变边框的高度,如果两个都满足(比如手指在边角位置),那么就呈现一种缩放状态if(isTop){rectTop+=slideY;if(rectTop<0)rectTop=0;if(rectTop>rectBottom-cornerLength*2)rectTop=rectBottom-cornerLength*2;}elseif(isBottom){rectBottom+=slideY;if(rectBottom>measuredHeight)rectBottom=measuredHeight;if(rectBottom<rectTop+cornerLength*2)rectBottom=rectTop+cornerLength*2;}invalidate();downX=moveX;downY=moveY;}}break;caseMotionEvent.ACTION_CANCEL:caseMotionEvent.ACTION_UP:isLeft=false;isRight=false;isTop=false;isBottom=false;isMove=false;isSlideLeft=false;isSlideRight=false;isSlideTop=false;isSlideBottom=false;break;}returntrue;}/***得到裁剪区域的margin值*/publicfloat[]getCutArr(){float[]arr=newfloat[4];arr[0]=rectLeft;arr[1]=rectTop;arr[2]=rectRight;arr[3]=rectBottom;returnarr;}publicintgetRectWidth(){return(int)(measuredWidth);}publicintgetRectHeight(){return(int)(measuredHeight);}publicvoidsetAspect(floataspect){this.aspect=aspect;}@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){super.onMeasure(widthMeasureSpec,heightMeasureSpec);if(measuredWidth==0){initParams();}}privatevoidinitParams(){measuredWidth=getMeasuredWidth();measuredHeight=getMeasuredHeight();if(aspect==-1){cornerLength=measuredWidth/6;rectRight=measuredWidth;rectLeft=0;rectTop=0;rectBottom=measuredHeight;}else{floatvh=measuredWidth*1.0f/measuredHeight;if(aspect>1){cornerLength=measuredWidth/6;}else{cornerLength=measuredHeight/6;}if(aspect>vh){rectLeft=0;rectRight=measuredWidth;floath=measuredWidth/aspect;rectTop=(measuredHeight-h)/2;rectBottom=rectTop+h;}else{rectTop=0;rectBottom=measuredHeight;floatw=measuredHeight*aspect;rectLeft=(measuredWidth-w)/2;rectRight=rectLeft+w;}}}@OverrideprotectedvoidonDraw(Canvascanvas){paint.setStrokeWidth(dp1);//绘制裁剪区域的矩形,传入margin值来确定大小canvas.drawRect(rectLeft,rectTop,rectRight,rectBottom,paint);//绘制四条分割线和四个角drawLine(canvas,rectLeft,rectTop,rectRight,rectBottom);}/***绘制四条分割线和四个角*/privatevoiddrawLine(Canvascanvas,floatleft,floattop,floatright,floatbottom){paint.setStrokeWidth(1);//绘制四条分割线floatstartX=(right-left)/3+left;floatstartY=top;floatstopX=(right-left)/3+left;floatstopY=bottom;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=(right-left)/3*2+left;startY=top;stopX=(right-left)/3*2+left;stopY=bottom;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=left;startY=(bottom-top)/3+top;stopX=right;stopY=(bottom-top)/3+top;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=left;startY=(bottom-top)/3*2+top;stopX=right;stopY=(bottom-top)/3*2+top;canvas.drawLine(startX,startY,stopX,stopY,paint);paint.setStrokeWidth(dp3);//绘制四个角startX=left-dp3/2;startY=top;stopX=left+cornerLength;stopY=top;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=left;startY=top;stopX=left;stopY=top+cornerLength;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=right+dp3/2;startY=top;stopX=right-cornerLength;stopY=top;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=right;startY=top;stopX=right;stopY=top+cornerLength;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=left;startY=bottom;stopX=left;stopY=bottom-cornerLength;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=left-dp3/2;startY=bottom;stopX=left+cornerLength;stopY=bottom;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=right+dp3/2;startY=bottom;stopX=right-cornerLength;stopY=bottom;canvas.drawLine(startX,startY,stopX,stopY,paint);startX=right;startY=bottom;stopX=right;stopY=bottom-cornerLength;canvas.drawLine(startX,startY,stopX,stopY,paint);}}
使用的时候,只要把这个CutView盖在图片的View上,CutView的宽高必须和图片View的显示宽高一样
我是这样计算的
intscreenWidth=mWidthPixels;intscreenHeight=mHeightPixels;intleft,top,viewWidth,viewHeight;floatsh=screenWidth*1.0f/screenHeight;floatvh=videoWidth*1.0f/videoHeight;if(sh<vh){left=0;viewWidth=screenWidth;viewHeight=(int)(videoHeight*1.0f/videoWidth*viewWidth);top=(screenHeight-viewHeight)/2;}else{top=0;viewHeight=screenHeight;viewWidth=(int)(videoWidth*1.0f/videoHeight*viewHeight);left=(screenWidth-viewWidth)/2;}LinearLayout.LayoutParamsparams=newLinearLayout.LayoutParams(viewWidth,viewHeight);params.leftMargin=left;params.topMargin=top;params.bottomMargin=mHeightPixels-top-viewHeight;videoView.setLayoutParams(params);
设置是否比例画框
cutView.setAspect(-1);
-1表示不用,需要比例显示的话就传入width*1.0f/heigh
关于“android怎么实现简单的矩形裁剪框”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注恰卡编程网行业资讯频道,小编每天都会为大家更新不同的知识点。
推荐阅读
-
android(如何快速开发框架 小米note开发版MIUI,安卓6.0,怎么安装Xposed框架)
稳定版,你必须先根除。你上网搜索安卓可以叫别人s框架,对方可以把框架做成jar包,把这个jar包加载到项目目录的libs文件中使...
-
android(studio 虚拟机启动不了 android studio可以当模拟器用吗)
androidstudio可以当模拟器用吗?AmdCUP引导模拟器有点复杂。雷电模拟器上的抖音怎么登录不上?不是,闪电模拟调用...
-
从实践中学习手机抓包与数据分析(android 手机抓包app)
android手机抓包app?netcapture抓包精灵app(手机抓包工具)又名sslcapture,是什么专业的安卓手机抓...
-
android(studio全局搜索 android studio怎么看app界面)
androidstudio怎么看app界面?在设备桌面点击运用直接进入到App界面,就也可以参与其他你的操作了。android-...
-
怎么把android框架源代码拉到本地(android studio如何运行别人的源代码)
androidstudio如何运行别人的源代码?androidstudio点击刚建在列表中你选择导入module,导入即可在用...
-
android(studio2022年使用教程 怎么安装Android studio详细教程)
怎么安装Androidstudio详细教程?androidstudio中haxm直接安装的方法追加:1、简单的方法打开Andr...
-
怎么使用Android基准配置文件Baseline Profile方案提升启动速度
-
HTML5如何实现禁止android视频另存为
-
学java好还是学php好?
-
Android如何实现多点触控功能