onTouch给人怪异的触摸点的Andr​​oid [英] onTouch giving weird touch points Android

查看:94
本文介绍了onTouch给人怪异的触摸点的Andr​​oid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其对此我做得相当简单的事情,我以前做过,但现在它没有运行作为我的预期。反正让我简单解释一下什么,我试图做什么我收到::

情景:
我有一个 RelativeLayout的中的的ImageView 放置,现在我将 touchlistener 是这样的: imageview.setOnTouchListener(本);

这让我重写 onTouch(视图V,MotionEvent事件)功能,我没有...
而在 ACTION_MOVE 我得到的 X 并添加它们到左和顶部的利润,这样我我运动我的形象。

奇怪的问题:
我的的ImageView 移动,但具有非常明显的抖动,就像如果我在正确的方向很感动,的ImageView 将去右侧,但在它的方式,它回来左侧,这样它看起来像图像并不稳定,它的振动..类似的东西..我给的 X 日志 ...希望这给你的想法。

中得到

  ACTION_DOWN [#0(PID 0)= 160233]参加的权利..
ACTION_MOVE [#0(PID 0)= 160233] ////
ACTION_MOVE [#0(PID 0)= 160233] //
ACTION_MOVE [#0(PID 0)= 174231] //
ACTION_MOVE [#0(PID 0)= 176233] //
ACTION_MOVE [#0(PID 0)= 196232] //
ACTION_MOVE [#0(PID 0)= 152232] //突然我得到了152作为x位置...图像回来
ACTION_MOVE [#0(PID 0)= 167232] //重新开始去的权利
ACTION_MOVE [#0(PID 0)= 180233] //在朝好的方向发展
ACTION_MOVE [#0(PID 0)= 173233] //再来一点点回来
ACTION_MOVE [#0(PID 0)= 187232] //同样的事情会发生到结束..
ACTION_MOVE [#0(PID 0)= 159,232]
ACTION_MOVE [#0(PID 0)= 174231]
ACTION_MOVE [#0(PID 0)= 177233]
ACTION_MOVE [#0(PID 0)= 189231]
ACTION_MOVE [#0(PID 0)= 155232]
ACTION_MOVE [#0(PID 0)= 171231]
ACTION_MOVE [#0(PID 0)= 183230]
ACTION_MOVE [#0(PID 0)= 161234]
ACTION_MOVE [#0(PID 0)= 171233]
ACTION_MOVE [#0(PID 0)= 174230]
ACTION_MOVE [#0(PID 0)= 183230]
ACTION_MOVE [#0(PID 0)= 162234]
ACTION_MOVE [#0(PID 0)= 170233]
ACTION_MOVE [#0(PID 0)= 176233]
ACTION_MOVE [#0(PID 0)= 165233]
ACTION_MOVE [#0(PID 0)= 175232]
ACTION_MOVE [#0(PID 0)= 163233]
ACTION_MOVE [#0(PID 0)= 171233]
ACTION_MOVE [#0(PID 0)= 167233]
ACTION_MOVE [#0(PID 0)= 172232]
ACTION_MOVE [#0(PID 0)= 178232]
ACTION_MOVE [#0(PID 0)= 158234]
ACTION_MOVE [#0(PID 0)= 170234]
ACTION_MOVE [#0(PID 0)= 170232]
ACTION_MOVE [#0(PID 0)= 177231]
ACTION_MOVE [#0(PID 0)= 157234]
ACTION_MOVE [#0(PID 0)= 160234]
ACTION_MOVE [#0(PID 0)= 169232]
ACTION_MOVE [#0(PID 0)= 165233]
ACTION_MOVE [#0(PID 0)= 167233]
ACTION_MOVE [#0(PID 0)= 159233]
ACTION_UP [#0(PID 0)= 161233]

XML code为RelativeLayout的:

 <的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=#99000000>    < ImageView的
        机器人:ID =@ + ID / ImageView的
        机器人:layout_width =400dp
        机器人:layout_height =400dp
        机器人:layout_marginLeft =100dp
        机器人:layout_marginTop =100dp
        机器人:scaleType =矩阵/>
< / RelativeLayout的>

所以,这是最大的,我可以解释我的问题,还是如果你需要什么,让我知道。

更新:
code在其集装箱移动的ImageView RelativeLayout的

 情况下MotionEvent.ACTION_MOVE:
            的LayoutParams PARAMS =(的LayoutParams)imageview.getLayoutParams();            params.leftMargin = params.leftMargin + X;
            params.topMargin = params.topMargin + Y;            imageview.setLayoutParams(PARAMS);
            打破;


解决方案

onTouch X和Y变量X和Y相对于它具有查看 onTouchListener 附后。所以10,10是由的ImageView 不是你的左上方的 RelativeLayout的

如果您使用这些值来移动你的的ImageView ,那么你自然会得到摇摇欲坠的行为,每一个移动动作也会触发一个稍微不同的OnTouchEvent造成的又一举措的行动 - 这递归给你一个振的效果。

Its quite simple thing which i am doing, i have done it before but now its not running as i expected. Anyways Let me explain briefly what i am trying to do and what i am getting::

Scenario:: I have a RelativeLayout in which an ImageView is placed, Now i set the touchlistener like this: imageview.setOnTouchListener(this);

It asked me to override the onTouch(View v,MotionEvent event) function which i did... and in the Action_Move i get the x and y and add them to left and top margins, in this way I am moving my image.

Weird Problem: My ImageView is moving but with a very noticeable shake, like if i am moving in right direction, ImageView will go to right side but on its way it comes back to left side so that it looks like image is not stable, its vibrating.. something like that.. I am giving the x and y which i get during log... hope this gives you the idea.

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]

XML code for RelativeLayout:

<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>

So, this is the max, i could explain my question, still if you need anything, let me know.

Update:: Code for moving the ImageView in its Container i.e RelativeLayout:

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

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

            imageview.setLayoutParams(params);
            break;

解决方案

The X and Y variables in onTouch are the X and Y relative to the View which has the onTouchListener attached. so 10, 10 would be from the top left of your ImageView not of the RelativeLayout.

If you use these values to move your ImageView then you will naturally get shaky behavior, each move action will also fire a slightly different OnTouchEvent causing another move action - this recursion gives you a "vibrating" effect.

这篇关于onTouch给人怪异的触摸点的Andr​​oid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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