1

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

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

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

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

    if (touchY >= imageHeight){
                    params.leftMargin = leftSideText;//sets the margin so the textview is displayed on the screen according to them because its aligned to top and left
                    params.topMargin = (tvOne.getHeight()) - imageHeight;
                    params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
                    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
                    tvOne.setLayoutParams(params);
                }

其中一些变量是:

                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 边界限制在 imageview 的范围内?如果您能提供任何帮助或建议,将不胜感激,谢谢!

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

看看我的问题here

4

0 回答 0