0

I am struggling a bit to move an image view properly. My layout has a linearLayout on the top of the screen, right below an edit text and then a view with my image view. When I touch the image, this goes down the screen and since that point I can control it, moving my finger where the ball was at the beginning, not where it actually appears on the screen. I am using getRawX and getRawY (these methods gives the absolute coordinates relative to the screen) and I think the problem is that the image appears on those values but relative to its view, not to the screen. I thought if I would sum up the height of the views above the view where the image view is located and substract it to the getRawX value it would get fixed, but I saw the the method height of LayoutParams just would give me a constant representing fill parent, match parent and wrap content. Anyway, it has to be way easier than doing that.

This is how my layout looks:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
 >

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/buttonsLayout"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/leftButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="LEFT" />

        <Button
            android:id="@+id/upButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="UP" />

        <Button
            android:id="@+id/downButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="DOWN" />

        <Button
            android:id="@+id/rightButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="0.25"
            android:text="RIGHT" />
    </LinearLayout>

    <EditText
        android:id="@+id/coordinatesTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <ImageView
        android:id="@+id/ballImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ball" />

</LinearLayout>

        ball.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            LayoutParams layoutParams = (LayoutParams) ball.getLayoutParams();
            switch (event.getAction()){

                case MotionEvent.ACTION_DOWN:
                    break;                         
                case MotionEvent.ACTION_UP:
                    break;                  
                case MotionEvent.ACTION_MOVE:

                    int x_cord = (int) event.getRawX();
                    int y_cord = (int) event.getRawY();
                    layoutParams.leftMargin = x_cord;
                    layoutParams.topMargin = y_cord;
                    ball.setLayoutParams(layoutParams);
                    break;

                default:
                    break;
            }

            return true;
        }
    });
Rafag
  • 719
  • 3
  • 11
  • 27
  • Move this **LayoutParams layoutParams = (LayoutParams) ball.getLayoutParams();** to **MotionEvent.ACTION_MOVE:** event. – Piyush Mar 08 '14 at 11:49
  • I just tried but still does not work. – Rafag Mar 08 '14 at 11:54
  • 1
    instead of that nasty ImageView positioning draw your Bitmap by yourself and you wull have more freedom – pskink Mar 08 '14 at 12:09
  • Thanks for the advice, I´ll read about it. Anyway, I also would like to know how to do it this way. – Rafag Mar 08 '14 at 12:52
  • @pskink After working one month with a SurfaceHolder, I want to personally thank you for your recommendation. So much easier!! – Rafag Mar 27 '14 at 11:21
  • @Rafag see my answer here http://stackoverflow.com/questions/21633545/android-imageview-scaling-and-translating-issue it uses normal View not SurfaceView but the idea is the same, you can freely remove the scale and rotate stuff... – pskink Mar 27 '14 at 11:32

1 Answers1

0

It seems the best solution is to use SurfaceHolder, just as it shown here. Way easier to manage!

Community
  • 1
  • 1
Rafag
  • 719
  • 3
  • 11
  • 27