内側にaActivity
があり、内側を移動できるようにしたいのですが、指を離すと元の位置に戻りたいです。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);
}
});
のup
アクションで、OnTouchListener
次のことを試しました。
setX
、setY
(動作していません)setLeft
、setTop
(動作していません)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);
}
});
}