2

У меня есть Activity, которые имеют в ImageView внутри RelativeLayout, и я хочу, чтобы иметь возможность перемещать ImageView внутри RelativeLayout и когда я удалить мой палец, я хочу, чтобы 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); 
      } 
     }); 

и в up действия в OnTouchListener я попробовал следующее:

  1. setX, setY (не работает)
  2. setLeft, setTop (не работает)
  3. setLayoutParams (не работает)

Так может ли кто-нибудь сказать мне, как это сделать?

EDIT

активность 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 –

ответ

0

Попробуйте позвонить mImageView.requestLayout()перед тем установить новое место. Если он не работает, попробуйте позвонить mImageView.invalidate()после вы установили новое местоположение.

Если он все еще не работает, разместите свой код при настройке позиции (3 способа, которые вы пробовали).

+0

, пожалуйста, просмотрите редактирование, которое я сделал, Спасибо –

Смежные вопросы