android怎么实现简单的矩形裁剪框-亚博电竞手机版
开发技术
2022年05月19日 10:36
2
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(x10){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 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 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 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 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 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 rectbottom-cornerlength*2)recttop=rectbottom-cornerlength*2;}elseif(isbottom){rectbottom =slidey;if(rectbottom>measuredheight)rectbottom=measuredheight;if(rectbottom 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设置是否比例画框
cutview.setaspect(-1);-1表示不用,需要比例显示的话就传入width*1.0f/heigh
关于“android怎么实现简单的矩形裁剪框”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注恰卡编程网行业资讯频道,小编每天都会为大家更新不同的知识点。
展开全文