Android:在翻译中缩放 EditText Android 问题并在放置在 parentView 外部时获取 childView 的 Touch 事件

标签 android android-layout view android-edittext scale

我的问题与 android 功能有关,用于缩放由多个 subview 组成的父 View 。

ZoomView->ParentView->[多个 subview ]

我正在研究 Demo project缩放 subview 并无限平移。缩放可根据需要完美运行。

问题 1: 但是,如果 View 中有一个 EditText,并且我尝试放大它,那么我将面临以下问题。

  1. 放大后文字模糊

  2. 指向文本工作正常,但翻译文本是 翻译速度非常快,因为它的翻译乘以 比例因子

  3. 选择文本也有上述问题。

    尝试运行 Demo如果从上面不清楚,可以理解问题

我尝试了两种方法来缩放 View 的内容,但这两种方法都出现了同样的问题。

  • 缩放 Canvas 并通过父类的 Matrix.class 缩放 MotionEvent 来转换 MotionEvent。
  • 设置包含 subview 的父级的 ScaleX 和 ScaleY

我的演示项目的层次结构缩放 View (UML 图)

enter image description here

基本问题是当 View 缩放到某个值时将光标放在正确的位置。

我已经提到了这个thread

问题 2: 如果我有一个可以在父 View 中移动的 subview ,那么在缩小之后如果 subview 被翻译到父 View 的范围之外,事件将停止捕获并且 subview 变得不可触摸 我试过使用 TouchDelegate 但我不知道如何扩展 parentView 触摸区域。 Code reference Thread

scale is 1 the area touch area is equal to screen for parentView and ZoomView

但是当比例因子不等于 1 时,父级的触摸区域小于(放大)显示的 ZoomView

黄色-parentView触摸区域

青色-缩放 View 触摸区域

绿色-ChildView 触摸区域

截图在这里

enter image description here

注意: 这是我在 StackOverflow 上的第一个问题,所以如果需要进行一些编辑,请提出建议。

最佳答案

这是我最接近解决上述问题的。

问题 1:

  1. 放大后文字模糊

setLayerType(LAYER_TYPE_HARDWARE,null);

set layer type Hardware in the view which has text View. for me i set it in green square view.

  1. 指向文本工作正常,但翻译文本时翻译速度非常快,因为它的翻译乘以缩放因子
  2. 选择文本也有上述问题。

For above two: This is a bug in android when you scale a view using the scaleX,scaleY api's the EditText get's event based on the current scale value of parent's.But issue is with the Cursor of editText.

The best solution we got is we wrote our own EditTextView (which extend's TextView).As we checked android framework source code we came to know that android use's PopUpWindow to display cursor on screen.

The source of issue is when we use scaleX and scaleY editText get's scaled even't as it's parent is scaled but as cursor is a PopUpWindow and not a child of EditText so event's are not scaled and above issue occur's

So wrote our own EditText if you see the source code EditText code is not very complex and can be written on our own.And to solve the scaling issue of Cursor, we added our own Cursor PopUpWindow and made it Scale aware.

NOTE: (UPDATE) Check at demo code here customEditText and CustomCursor. Run the project set focus pink focus(BY Android) will come ,wait for few seconds then the black cursor will appear that is the custom cursor. It is a very basic example and we need to add all other edit text Feature's on our own like multi-Select, popUpMenu etc.

问题 2: 如果我有一个可以在父 View 中移动的 subview ,那么在缩小之后如果 subview 被翻译到父 View 的范围之外,事件将停止捕获并且 subview 变得不可触摸 我试过使用 TouchDelegate 但我不知道如何扩展 parentView 触摸区域

This solution is specific to the given problem not a generic one as there can be multiple solution's for this problem. In this case 3 view's are there:

  1. Yellow-parentView of green square View
  2. Cyan-parent of yellow View
  3. Green-ChildView non touchable outside parent

    My Solution is that i used created a callback from green square view to Yellow parent view. Whenever the translation of green square view ended the callback is triggered and in yellow parent i used below code.

override fun eventEnded() {
      setDelegate()
    }


fun setDelegate() {
            val rect = Rect()
            val parent = (this.parent as View) 
            parent.getHitRect(rect)
            val touchDelegate = TouchDelegate(rect, this)
            if (View::class.java.isInstance(editorContainer.parent)) {
                (editorContainer.parent as View).touchDelegate = touchDelegate
            }
    }

The above code translate to : when child view translation is ended check the bound's of parent and update the TouchDelegate accordingly.

As the problem i faced was when i scaled(Zoomed) the view then the View's delegate was not updated so for that also solution is same make a callback from zoomView (parent) to yellowView (childView) onScaleEnded call setDelegate().

When scale changes the hitArea also changes but as per delegate nothing is changed so we need to update the rect of TouchDelegate.

欢迎在评论区讨论。如果有人有更好的解决方案真的很想知道。因为我们尝试了许多不同的解决方案,但没有其他解决方案。这是我找到的最好的解决方案,我在生产中使用上面的代码,直到现在还没有遇到任何问题。

关于Android:在翻译中缩放 EditText Android 问题并在放置在 parentView 外部时获取 childView 的 Touch 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49150313/

相关文章:

android - 在查询中将长转换为日期

android - 页面上的自定义水平选择器

android - 如何将 OnClickListener 设置为 RecyclerView 中的两个按钮?

mysql - 用 View 翻译表格是否可行?

view - 当我保存文件时,为什么Visual Studio Code会格式化我的文件?

android - Altbeacon 库测距只有 30% 的时间检测到信标

安卓文件提供者 : Failed to find configured root that contains

java - Activity 启动时引发空指针异常

android - Android 中的圆环按钮

model-view-controller - MVC - 通过ViewBag将List传递给View