【发布时间】:2017-07-01 18:29:15
【问题描述】:
我遇到了一个恼人的问题,当我移动我的 RelativeLayout 时,其中的 ImageView 会增大以填满整个屏幕的宽度。我已将图片替换为彩色背景,以便于查看。
别介意它的外观,布局是 W.I.P
这里有问题的 GIF:https://gyazo.com/ae31b8e70e4f96fe89ba041549db22bb
下面是我的 OnTouchListener 的代码
private View.OnTouchListener relativeListener = new View.OnTouchListener()
{
@Override
public boolean onTouch(View view, MotionEvent event)
{
final int X = (int) event.getRawX();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
LinearLayout.LayoutParams lParams = (LinearLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = (X - _xDelta);
layoutParams.rightMargin = -(X - _xDelta);
view.setLayoutParams(layoutParams);
break;
}
_root.invalidate();
return true;
}
};
下面是定义ImageView和RelativeLayout的xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="me.tech.myapp.Fragments.HomeFragment">
<TextView
android:id="@+id/tvCurrentTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:text="StringCurrentTheme"
android:textAlignment="center"
android:textSize="18sp" />
<RelativeLayout
android:id="@+id/relativeRoot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp">
<ImageView
android:id="@+id/ivMainImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:contentDescription="Foto jonguh"
android:cropToPadding="false"
android:scaleType="centerCrop"
app:srcCompat="@android:color/holo_blue_bright" />
<Button
android:id="@+id/btnReportImage"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignTop="@+id/ivMainImage"
android:layout_margin="16dp"
android:background="@drawable/ic_menu_send"
android:visibility="visible" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabSendImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/btnReportImage"
android:layout_alignParentBottom="true"
android:layout_marginEnd="17dp"
android:clickable="true"
app:backgroundTint="@android:color/holo_orange_dark"
app:elevation="4dp"
app:fabSize="normal"
app:srcCompat="@drawable/ic_menu_camera" />
</RelativeLayout>
</LinearLayout>
【问题讨论】:
-
你想用
MotionEvent.ACTION_MOVE实现什么?为什么在Imageview中使用cropToPadding="false"? -
感谢您的回复!据我了解,
MotionEvent.ACTION_MOVE是必需的,因为我不断检测用户手指在按下时在屏幕上的位置(如果我错了,请纠正我)。并且cropToPadding="false"也是稍后需要的(我不确定它是否默认为假) -
@Tonteria24 但它的工作原理是这样的,只是那个小问题
标签: android android-layout android-imageview ontouchlistener android-relativelayout