Question

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;
        }
    });
Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top