2

工作,我有一個Activity有一個RelativeLayout內的ImageView,我希望能夠在移動RelativeLayoutImageView,當我刪除我的手指我想ImageView要回其原來的位置。變化ImageView的位置動態不在的Android

我曾嘗試以下:

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

,並在OnTouchListenerup動作我試過如下:

  1. setXsetY(不工作)
  2. setLeftsetTop(不工作)
  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); 
      } 
     }); 
    } 
+0

我想你實際上是在尋找這個http://developer.android.com/guide/topics/ui/drag-drop.html – 2014-12-04 12:09:55

回答

0

嘗試調用mImageView.requestLayout()之前設置的新位置。如果它不起作用,請嘗試撥打mImageView.invalidate()您設置新的位置。

如果它仍然無法正常工作,請在設置位置(您嘗試的3種方式)時發佈您的代碼。

+0

請查看我所做的編輯,謝謝 – 2014-12-04 12:30:50