Android 自定义View实现可拖动边框缩放的矩形
发布日期:2021-06-29 11:46:17
浏览次数:3
分类:技术文章
本文共 3427 字,大约阅读时间需要 11 分钟。
先看效果图
我的思路是,首先我给确定下来我点击的是哪一条边,这个可以用onTouchEvent来监听实现。然后,由于用户很难精确的点击到边上,所有点击时需要存在一定的误差范围。最后,我需要判断每条边的边界情况,例如右边框不能移动到左边框的左边,上边框不能移动到下边框的下面等。
下面是自定义View的源码:
public class MyRectView extends View { private Paint paint; private Rect rect; private int left=200,right=700,top=200,bottom=500; private int strokeWidth=10; private boolean onVerticalLeftStroke=false,onVerticalRightStroke=false; //是否在左边框上;右边框上 private boolean onHorizontalUpStroke=false,onHorizontalDownStroke=false;//是否在上边框上;下边框上 private float currentX=0,currentY=0; public MyRectView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); init(); } private void init(){ paint=new Paint(); paint.setStrokeWidth(strokeWidth); paint.setColor(Color.RED); paint.setStyle(Paint.Style.STROKE); rect=new Rect(left,top,right,bottom); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawRect(rect,paint); } @Override public boolean onTouchEvent(MotionEvent event) { float x=event.getX(); float y=event.getY(); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: currentX=x;currentY=y; onHorizontalRectStroke(x,y); onVerticalRectStroke(x,y); break; case MotionEvent.ACTION_MOVE: currentX=x;currentY=y; moveRectStroke(); break; case MotionEvent.ACTION_UP: onHorizontalUpStroke=false; onHorizontalDownStroke=false; onVerticalLeftStroke=false; onVerticalRightStroke=false; break; } return true; } //通过边框移动矩形(重画) private void moveRectStroke(){ if(onVerticalLeftStroke){ left=(int)currentX; //检查左边框的边界 if(left<0)left=0; if(left>right-25)left=right-25; redrawRect(); }else if(onVerticalRightStroke){ right=(int)currentX; //检查右边框的边界 if(right>Constants.SCREEN_WIDTH)right=900; if(rightbottom-25)top=bottom-25; redrawRect(); }else if(onHorizontalDownStroke){ bottom=(int)currentY; //检查下边框的边界 if(bottom>Constants.SCREEN_HEIGHT)bottom=1500; if(bottom =top && y<=bottom){ //在左边框上(在一定的误差范围内) if(x<=left+strokeWidth*5 && x>=left-strokeWidth*5){ onVerticalLeftStroke=true; } //在右边框上(在一定的误差范围内) else if(x<=right+strokeWidth*5 && x>=right-strokeWidth*5){ onVerticalRightStroke=true; } } } //是否在矩形边框上 private void onHorizontalRectStroke(float x,float y){ //在横边框上 if(x>=left && x<=right){ //在上边框上(在一定的误差范围内) if(y<=top+strokeWidth*5 && y>=top-strokeWidth*5){ onHorizontalUpStroke=true; } //在下边框上(在一定的误差范围内) else if(y<=bottom+strokeWidth*5 && y>=bottom-strokeWidth*5) { onHorizontalDownStroke=true; } } }}
Activity类:
public class MyRectAct extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.act_rect); }}
xml布局文件如下:
转载地址:https://blog.csdn.net/zz51233273/article/details/107629079 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月28日 17时34分33秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
电赛 | 19年全国一等奖,北航学子回忆录。
2019-04-29
电赛 | 19年全国一等奖,北航学子回忆录(上)
2019-04-29
电赛 | 19年全国一等奖,北航学子回忆录(下)
2019-04-29
突破!台积电1nm芯片,有了新进展。
2019-04-29
一文读懂全系列树莓派!
2019-04-29
自制一个害羞的口罩,见人就闭嘴,戴着可以喝奶茶
2019-04-29
聊聊我是如何编程入门的
2019-04-29
J-Link该如何升级固件?
2019-04-29
从电子垃圾中提炼黄金,可以!!!
2019-04-29
知乎大神深入解析:单片机晶振脚原理是什么?
2019-04-29
电容有17种?看看详细介绍!
2019-04-29
如何准备电赛?19年电赛经验总结!
2019-04-29
蓝牙:为啥叫“蓝”牙,不叫“白”牙?
2019-04-29
干货 | 如何系统学习 C 语言?
2019-04-29
多层PCB内部长啥样? 3D大图解析高端PCB板的设计工艺
2019-04-29
鸿蒙2.0都来了,快搭个环境玩起来吧!
2019-04-29
PCB散热的10种方法!
2019-04-29
值得收藏!268条PCB layout设计规范
2019-04-29
Keil升级了,Keil Studio 来了!
2019-04-29
关于RS-485总线,这篇很详细
2019-04-29