【问题标题】:onTouch giving weird touch points AndroidonTouch 提供奇怪的接触点 Android
【发布时间】:2012-07-20 07:42:44
【问题描述】:

这是我正在做的非常简单的事情,我以前做过,但现在它没有像我预期的那样运行。无论如何,让我简要解释一下我正在尝试做什么以及我得到了什么::

场景:: 我有一个RelativeLayout,其中放置了一个ImageView,现在我将touchlistener 设置为:imageview.setOnTouchListener(this);

它要求我覆盖我所做的onTouch(View v,MotionEvent event) 函数... 在Action_Move 中,我得到 xy 并将它们添加到左边距和上边距,这样我正在移动我的图像。

奇怪的问题: 我的ImageView 正在移动,但震动非常明显,就像我朝正确的方向移动一样,ImageView 会向右侧移动,但在途中它会回到左侧,因此看起来图像不稳定,它在振动.. 类似的东西.. 我给出了我在log 期间得到的 xy ...希望这能给你这个想法。

ACTION_DOWN[#0(pid 0)=160,233]  Going right..
ACTION_MOVE[#0(pid 0)=160,233]  ////
ACTION_MOVE[#0(pid 0)=160,233]  //
ACTION_MOVE[#0(pid 0)=174,231]  //
ACTION_MOVE[#0(pid 0)=176,233]  //
ACTION_MOVE[#0(pid 0)=196,232]  //
ACTION_MOVE[#0(pid 0)=152,232]  // suddenly i got 152 as location for x... image comes     back
ACTION_MOVE[#0(pid 0)=167,232]  // again started to go right
ACTION_MOVE[#0(pid 0)=180,233]  // going right
ACTION_MOVE[#0(pid 0)=173,233]  // again comes a little back
ACTION_MOVE[#0(pid 0)=187,232]  // same thing goes till end..
ACTION_MOVE[#0(pid 0)=159,232]
ACTION_MOVE[#0(pid 0)=174,231]
ACTION_MOVE[#0(pid 0)=177,233]
ACTION_MOVE[#0(pid 0)=189,231]
ACTION_MOVE[#0(pid 0)=155,232]
ACTION_MOVE[#0(pid 0)=171,231]
ACTION_MOVE[#0(pid 0)=183,230]
ACTION_MOVE[#0(pid 0)=161,234]
ACTION_MOVE[#0(pid 0)=171,233]
ACTION_MOVE[#0(pid 0)=174,230]
ACTION_MOVE[#0(pid 0)=183,230]
ACTION_MOVE[#0(pid 0)=162,234]
ACTION_MOVE[#0(pid 0)=170,233]
ACTION_MOVE[#0(pid 0)=176,233]
ACTION_MOVE[#0(pid 0)=165,233]
ACTION_MOVE[#0(pid 0)=175,232]
ACTION_MOVE[#0(pid 0)=163,233]
ACTION_MOVE[#0(pid 0)=171,233]
ACTION_MOVE[#0(pid 0)=167,233]
ACTION_MOVE[#0(pid 0)=172,232]
ACTION_MOVE[#0(pid 0)=178,232]
ACTION_MOVE[#0(pid 0)=158,234]
ACTION_MOVE[#0(pid 0)=170,234]
ACTION_MOVE[#0(pid 0)=170,232]
ACTION_MOVE[#0(pid 0)=177,231]
ACTION_MOVE[#0(pid 0)=157,234]
ACTION_MOVE[#0(pid 0)=160,234]
ACTION_MOVE[#0(pid 0)=169,232]
ACTION_MOVE[#0(pid 0)=165,233]
ACTION_MOVE[#0(pid 0)=167,233]
ACTION_MOVE[#0(pid 0)=159,233]
ACTION_UP  [#0(pid 0)=161,233]

RelativeLayout 的 XML 代码:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:background="#99000000">

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="400dp"
        android:layout_height="400dp"
        android:layout_marginLeft="100dp"
        android:layout_marginTop="100dp"
        android:scaleType="matrix" />
</RelativeLayout>

所以,这是最大的,我可以解释我的问题,如果你需要什么,请告诉我。

更新:: 在其容器中移动ImageView 的代码,即RelativeLayout

case MotionEvent.ACTION_MOVE:
            LayoutParams params = (LayoutParams) imageview.getLayoutParams();

            params.leftMargin = params.leftMargin + x;
            params.topMargin = params.topMargin + y;

            imageview.setLayoutParams(params);
            break;

【问题讨论】:

    标签: android android-relativelayout android-imageview ontouchlistener


    【解决方案1】:

    onTouch 中的 X 和 Y 变量是相对于附加了onTouchListener 的视图的 X 和 Y。所以 10, 10 将来自您的 ImageView 的左上角,而不是 RelativeLayout 的左上角。

    如果您使用这些值来移动您的ImageView,那么您自然会得到不稳定的行为,每个移动操作也会触发一个略有不同的 OnTouchEvent,从而导致另一个移动操作 - 这种递归会给您一种“振动”效果。

    【讨论】:

    • "每个移动动作也会触发一个稍微不同的 OnTouchEvent,从而导致另一个移动动作——这个递归给你一个“振动”的效果。"请稍微解释一下,链接/文档在详细编写的地方会非常有帮助...
    【解决方案2】:

    当我通过 event.getRawY() 而不是 event.getY() 方法获得 Y 坐标来移动 View 时,摇晃的行为消失了。 因此,如果您使用过getY() 方法,请尝试更改它。

    【讨论】:

      猜你喜欢
      • 2013-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      相关资源
      最近更新 更多