2

我有Activity一个ImageView里面有一个RelativeLayout,我希望能够移动ImageView里面RelativeLayout,当我移开手指时,我希望ImageView它回到原来的位置。

我尝试了以下方法:

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);
            }
        });

并在upOnTouchListener尝试了以下操作:

  1. setX, setY(不工作)
  2. setLeft, setTop(不工作)
  3. setLayoutParams(不工作)

那么任何人都可以告诉我该怎么做吗?

编辑

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

活动代码:

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);
            }
        });
    }
4

1 回答 1

0

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

于 2014-12-04T12:10:58.147 回答