android怎么实现简单的矩形裁剪框(android,开发技术)

时间:2024-05-03 11:55:19 作者 : 石家庄SEO 分类 : 开发技术
  • TAG :

正常模式是这样的

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

 </div> <div class="zixun-tj-product adv-bottom"></div> </div> </div> <div class="prve-next-news">
本文:android怎么实现简单的矩形裁剪框的详细内容,希望对您有所帮助,信息来源于网络。
上一篇:python如何修改包导入时搜索路径下一篇:

6 人围观 / 0 条评论 ↓快速评论↓

(必须)

(必须,保密)

阿狸1 阿狸2 阿狸3 阿狸4 阿狸5 阿狸6 阿狸7 阿狸8 阿狸9 阿狸10 阿狸11 阿狸12 阿狸13 阿狸14 阿狸15 阿狸16 阿狸17 阿狸18