大佬教程收集整理的这篇文章主要介绍了android – 如何在点之间画一条线,拉这些点?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
PointF topLeft = new PointF(10,10); PointF topRight = new PointF(90,10); PointF bottomLeft = new PointF(10,90); PointF bottomRight = new PointF(90,90);
你需要做的是在每个点周围做一个RectF. RectF越大,触摸面积就越大.
float sizeOfRect = 5f; RectF topLeftTouchArea = new RectF(topLeft.x - sizeOfRect,topLeft.y - sizeOfRect,topLeft.x + sizeOfRect,topLeft.y + sizeOfRect); //Do this for the other points too
定义一些全局变量来跟踪用户在onTouch中的操作.一个int是被触摸的角落,另外四个是角落的标识符.
private final int nONE = -1,TOUCH_TOP_LEFT = 0,TOUCH_TOP_RIGHT = 1,TOUCH_BOT_LEFT = 2,TOUCH_BOT_RIGHT = 3; int currentTouch = NONE;
现在,在您的onTouch事件中,您可以检查用户感兴趣的点,如下所示:
@Override public Boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { //The user just put @R_944_8917@ finger down. //We check to see which corner the user is touching //And set our global,currentTouch,to the appropriate constant. case MotionEvent.ACTION_DOWN: if (topLeftTouchArea.contains(event.getX(),event.getY()) { currentTouch = TOUCH_TOP_LEFT; } else if (topRightTouchArea.contains(event.getX(),event.getY()) { currentTouch = TOUCH_TOP_RIGHT; } else if (botLeftTouchArea.contains(event.getX(),event.getY()) { currentTouch = TOUCH_BOT_LEFT; } else if (botrightTouchArea.contains(event.getX(),event.getY()) { currentTouch = TOUCH_BOT_RIGHT; } else { return false; //Return false if user touches none of the corners } return true; //Return true if the user touches one of the corners //Now we kNow which corner the user is touching. //When the user moves @R_944_8917@ finger,we update the point to the user position and invalidate. case MotionEvent.ACTION_MOVE: switch (currentTouch) { case TOUCH_TOP_LEFT: topLeft.x = event.getX(); topLeft.y = event.getY(); //The bottom left x position has to move with the top left corner bottomLeft.x = topLeft.x; //The top right y position has to move with the top left corner topRight.y = topLeft.y; invalidate(); return true; case TOUCH_TOP_RIGHT: topRight.x = event.getX(); topRight.y = event.getY(); //The top left y position has to move with the top right corner topLeft.y = topRight.y; //The bottom right x position has to move with the top right corner bottomRight.x = topRight.x; invalidate(); return true; case TOUCH_BOT_LEFT: bottomLeft.x = event.getX(); bottomLeft.y = event.getY(); bottomRight.y = bottomLeft.y; topLeft.x = bottomLeft.x; invalidate(); return true; case TOUCH_BOT_RIGHT: bottomRight.x = event.getX(); bottomRight.y = event.getY(); topRight.x = bottomRight.x; bottomLeft.y = bottomRight.y; invalidate(); return true; } //We returned true for all of the above cases,because we used the event return false; //If currentTouch is none of the above cases,return false //Here the user lifts up @R_944_8917@ finger. //We update the points one last time,and set currentTouch to NONE. case MotionEvent.ACTION_UP: switch (currentTouch) { case TOUCH_TOP_LEFT: topLeft.x = event.getX(); topLeft.y = event.getY(); //The bottom left x position has to move with the top left corner bottomLeft.x = topLeft.x; //The top right y position has to move with the top left corner topRight.y = topLeft.y; invalidate(); currentTouch = NONE; return true; case TOUCH_TOP_RIGHT: topRight.x = event.getX(); topRight.y = event.getY(); //The top left y position has to move with the top right corner topLeft.y = topRight.y; //The bottom right x position has to move with the top right corner bottomRight.x = topRight.x; invalidate(); currentTouch = NONE; return true; case TOUCH_BOT_LEFT: bottomLeft.x = event.getX(); bottomLeft.y = event.getY(); bottomRight.y = bottomLeft.y; topLeft.x = bottomLeft.x; invalidate(); currentTouch = NONE; return true; case TOUCH_BOT_RIGHT: bottomRight.x = event.getX(); bottomRight.y = event.getY(); topRight.x = bottomRight.x; bottomLeft.y = bottomRight.y; invalidate(); currentTouch = NONE; return true; } return false; } }
这是做一个围绕你的点的矩形.想像一下在图片周围的绘图框.这些是由Rect对象创建的“触摸板”.矩形的大小由sizeOfRect设置.在onTouchEvent中,它检查每个矩形对象以查看用户的触摸是否在矩形内,从而指示用户尝试触摸该点.
以上是大佬教程为你收集整理的android – 如何在点之间画一条线,拉这些点?全部内容,希望文章能够帮你解决android – 如何在点之间画一条线,拉这些点?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。