Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onTouch giving weird touch points Android

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;
like image 871
Farhan Avatar asked Jul 20 '12 07:07

Farhan


1 Answers

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.

like image 89
Graeme Avatar answered Sep 29 '22 18:09

Graeme