Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用户触摸时将TextView边界限制为ImageView_Java_Android_Android Layout_Textview_Ontouchlistener - Fatal编程技术网

Java 用户触摸时将TextView边界限制为ImageView

Java 用户触摸时将TextView边界限制为ImageView,java,android,android-layout,textview,ontouchlistener,Java,Android,Android Layout,Textview,Ontouchlistener,我有一个imageview,它只占屏幕的一部分。我在imageview上面有一个文本视图。当用户触摸textview时,他们可以将其拖动到屏幕上任何他们想要的位置。但是,我想限制用户只能在imageview上移动textview 当textview移动到屏幕的顶部或右侧时,它会自动阻止它进一步移动(类似于将计算机鼠标移动到屏幕的顶部或左侧)。我想尝试在imageview的范围内模拟这种效果 我尝试过很多技术,比如获取imageview的大小,并将其与y和x值进行比较,如果textview超过这一

我有一个imageview,它只占屏幕的一部分。我在imageview上面有一个文本视图。当用户触摸textview时,他们可以将其拖动到屏幕上任何他们想要的位置。但是,我想限制用户只能在imageview上移动textview

当textview移动到屏幕的顶部或右侧时,它会自动阻止它进一步移动(类似于将计算机鼠标移动到屏幕的顶部或左侧)。我想尝试在imageview的范围内模拟这种效果

我尝试过很多技术,比如获取imageview的大小,并将其与y和x值进行比较,如果textview超过这一点,它将等于这一点。然而,到目前为止还没有成功

这是我当前的代码(在OnTouchListener()中):

其中一些变量为:

                touchX = (int) eventTwo.getRawX();
                touchY = (int) eventTwo.getRawY();
                //helps make the edit text start where the user touches the screen rather than that just being the top corner
                int editTextWidth = tvOne.getWidth() / 2;
                int editTextHeight = tvOne.getHeight() / 2;

                topText = touchY - editTextHeight;
                leftSideText = touchX - editTextWidth;
此代码只会使textview在经过此点后消失

那么,为了澄清我的问题,如何将textview边界限制在imageview的范围内?如果您能提供任何帮助或建议,我们将不胜感激,谢谢

编辑:我找到了问题的原因,但还没有找到解决方案。首先,文本视图似乎没有消失,只是在图像下方的黑色背景上不可见。其次,textview超过imageview的原因是因为没有显示完整的图像,或者看起来是这样。所以,当我得到高度时,它指的是整个图像的高度,但是,整个图像并没有被显示

看看我的问题

                touchX = (int) eventTwo.getRawX();
                touchY = (int) eventTwo.getRawY();
                //helps make the edit text start where the user touches the screen rather than that just being the top corner
                int editTextWidth = tvOne.getWidth() / 2;
                int editTextHeight = tvOne.getHeight() / 2;

                topText = touchY - editTextHeight;
                leftSideText = touchX - editTextWidth;