Change ImageView position dynamically not working in Android

1.1k views Asked by At

I have an Activity that have an ImageView inside a RelativeLayout and I want to be able to move the ImageView inside the RelativeLayout and when I remove my finger I want the ImageView to get back to its original position.

I have tried the following:

mImageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                mImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                int[] locations = new int[2];
                mImageView.getLocationOnScreen(locations);
                //mImageView.getLocationInWindow(locations);
                mOldX = locations[0];
                mOldY = locations[1];

                System.out.println("X: " + mOldX + " Y: " + mOldY);
            }
        });

and in the up action in the OnTouchListener I tried the following:

  1. setX, setY (not working)
  2. setLeft, setTop (not working)
  3. setLayoutParams (not working)

So can any one tell me how to do that??

EDIT

Activity xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <include
        android:id="@+id/custom_header_layout"
        layout="@layout/item_header" />

    <TextView        
        android:id="@+id/push_to_start_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/largest_margin"
        android:text="@string/push_to_start_recording"
        android:textSize="@dimen/larger_text_size"
        android:visibility="visible"
        />

    <TextView     
        android:id="@+id/timer_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/medium_margin"
        android:text="00:00"
        android:textSize="55sp"
        />

    <TextView
        android:id="@+id/status_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="@dimen/large_margin"
        android:text="@string/recording"
        android:textSize="@dimen/larger_text_size"
        android:visibility="invisible"
        />

    <RelativeLayout
        android:id="@+id/parentLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <ImageView
            android:id="@+id/record_imageview"
            android:layout_width="@dimen/record_audio_image_size"
            android:layout_height="@dimen/record_audio_image_size"
            android:layout_centerHorizontal="true"
            android:src="@drawable/post_done" />

        <ImageView
            android:id="@+id/cancel_imageview"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:layout_marginBottom="@dimen/large_margin"
            android:src="@drawable/post_cancel" />
    </RelativeLayout>

</LinearLayout>

Activity code:

protected void initializeUIComponentsAction() {

        mRecordImageView.setOnTouchListener(new OnTouchListener() {
            int prevX, prevY;

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

                final RelativeLayout.LayoutParams par = (RelativeLayout.LayoutParams) v
                        .getLayoutParams();
                switch (event.getAction()) {

                case MotionEvent.ACTION_MOVE: {

                    par.topMargin += (int) event.getRawY() - prevY;
                    prevY = (int) event.getRawY();
                    par.leftMargin += (int) event.getRawX() - prevX;
                    prevX = (int) event.getRawX();
                    v.setLayoutParams(par);

                    if (isTouchDeleteIcon(mRecordImageView, mCancelImageView) == true) {

                        resetRecording();
                        //mRecordImageView.setVisibility(View.GONE);
                        PhoneUtils.vibrateDevice(PostAudioActivity.this);
                        FileManager.getInstance().deleteFile(mFileFullPath);                        
                    }

                    return true;
                }
                case MotionEvent.ACTION_UP: {

                    //par.topMargin += (int) event.getRawY() - prevY;
                    //par.leftMargin += (int) event.getRawX() - prevX;

                    mRecordImageView.requestLayout();
                    par.topMargin = mOldY; 
                    par.leftMargin = mOldX; 
                    mRecordImageView.invalidate();
                    v.setLayoutParams(par);
                    stopRecording();
                    return true;
                }
                case MotionEvent.ACTION_DOWN: {

                    prevX = (int) event.getRawX();
                    prevY = (int) event.getRawY();  
                    par.bottomMargin = -2 * v.getHeight();
                    par.rightMargin = -2 * v.getWidth();
                    v.setLayoutParams(par);
                    startRecording();
                    return true;
                }
                }
                return false;
            }
        });

        mRecordImageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                mRecordImageView.getViewTreeObserver().removeGlobalOnLayoutListener(this);

                int[] locations = new int[2];
                mRecordImageView.getLocationOnScreen(locations);
                mOldX = locations[0];
                mOldY = locations[1];

                System.out.println("X: " + mOldX + " Y: " + mOldY);
            }
        });
    }
1

There are 1 answers

0
Blaze Tama On

Try to call mImageView.requestLayout() before you set the new location. If its not working, try to call mImageView.invalidate() after you set the new location.

If its still not working, post your code when setting the position (the 3 ways you tried).