Android:缩放EditText Android放置在parentView之外时,会发生childView的翻译和Get Touch事件中的翻译问题 [英] Android: Zooming EditText Android Issue in translation And Getting Touch event of childView when Placed outside parentView

查看:82
本文介绍了Android:缩放EditText Android放置在parentView之外时,会发生childView的翻译和Get Touch事件中的翻译问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题与android功能有关,以缩放由多个子视图组成的父视图.

ZoomView-> ParentView-> [多个childViews]

我正在研究演示项目,以放大子视图并无限地平移.缩放完美地完成了根据需要.

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

  1. 在放大文字时是模糊的

  2. 指向文本的
  3. 可以正常工作,但通过文本进行翻译可以 翻译非常快,因为其翻译乘以 scaleFactor

  4. 选择文本也存在上述问题.

    尝试运行演示,如果从上面无法清楚了解该问题

我尝试了两种方法来缩放View的内容,但是两种方法都存在相同的问题.

  • 通过父类的Matrix.class缩放MotionEvent,缩放画布并转换MotionEvent.
  • 设置包含childViews的父级的ScaleX和ScaleY

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

基本问题是,将视图缩放到某个值时,将光标放置在正确的位置.

我已经提到了这个线程

问题2: 如果我有一个可在父视图中移动的childView,则在缩小之后,如果childView在parentView的边界之外翻译,则事件将停止捕获,并且子视图变得不可触摸 我已经尝试过使用TouchDelegate,但是我不知道如何扩展parentView触摸区域.参考线程

比例为1,触摸区域的面积等于parentView和ZoomView的屏幕

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

黄父母查看触摸区域

Cyan-ZoomView触摸区域

绿色ChildView触摸区域

在这里截图

注意: 这是我关于StackOverflow的第一个问题,因此请建议是否需要进行一些编辑.

解决方案

这是我解决上述问题的最接近的方法.

问题1:

  1. 在放大文字时,效果会模糊

setLayerType(LAYER_TYPE_HARDWARE,null);

在具有文本视图"的视图中

设置图层类型硬件".对我来说 将其设置为绿色正方形视图.

  1. 指向文本的效果很好,但是通过将文本的翻译乘以scalingFactor
  2. ,可以快速翻译文本
  3. 选择文本也存在上述问题.

对于以上两个: 当您使用scaleX,scaleY api的EditText事件基于父对象的当前缩放值对视图进行缩放时,这是android中的一个错误.但是问题在于editText的游标.

我们得到的最好的解决方案是编写自己的EditTextView(扩展了TextView).当我们检查android框架源代码时,我们知道android使用了源代码 EditText代码不是很复杂,可以自己编写.为了解决Cursor的缩放问题,我们添加了自己的Cursor Demo project to Zoom the child View and to pan infinitely.Zooming works perfectly as needed.

PROBLEM 1: But if there is a EditText in the view and i try to zoom on that then below issues are faced by me.

  1. on zooming in the text is blurred

  2. pointing on a text is working fine but translating through the text is translating very fast as its translation is multiplied by the scalingFactor

  3. Selecting the text is also having above issue.

    Try running the the Demo to understand the issue if not clear from above

I have tried two approaches to Zoom the content of the View but both approaches gave the same issue.

  • Scaling canvas and transforming the MotionEvent by scaling the MotionEvent by Matrix.class of the Parent Class.
  • Setting ScaleX and ScaleY of the Parent containing the childViews

hierarchy of my demo project zooming the view(UML Diagram)

Basic problem is placing the cursor at the right position when the view is scaled to some value.

I have already referred to this thread

PROBLEM 2: if i have a childView which is movable in the parent View then after zooming out if the childView is translated outside the bounds of parentView the events are ceased to capture and the child view becomes untouchable i have tried using TouchDelegate but i don't know how to expand the parentView touch area .Code reference Thread

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

But when scale factor is not equal to one the touch area of parent is lesser(zoomIn) than the ZoomView as displayed

Yellow-parentView Touch Region

Cyan-ZoomView Touch Region

Green-ChildView Touch Region

ScreenShot here

Note: This is my first Question on StackOverflow So please recommend if some edits are needed.

解决方案

This the the closest i got to solve above problem's.

PROBLEM 1:

  1. On zooming in the text is blurred

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. pointing on a text is working fine but translating through the text is translating very fast as its translation is multiplied by the scalingFactor
  2. Selecting the text is also having above issue.

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.

PROBLEM 2: if i have a childView which is movable in the parent View then after zooming out if the childView is translated outside the bounds of parentView the events are ceased to capture and the child view becomes untouchable i have tried using TouchDelegate but i don't know how to expand the parentView touch area

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.

Please feel free to discuss in comment section. If someone has a better solution Really eager to know.As we tried many different solution's but nothing else worked.This is the best i found and i am using above code in production and haven't faced any issue till now.

这篇关于Android:缩放EditText Android放置在parentView之外时,会发生childView的翻译和Get Touch事件中的翻译问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆