当移动RelativeLayout(Android)时,ImageView的大小略有增加
当我移动我的RelativeLayout时,其中的ImageView增加了大小以填充整个屏幕的宽度,我遇到了一个烦人的问题。我用彩色背景替换了图像,以便更容易看到。当移动RelativeLayout(Android)时,ImageView的大小略有增加
不介意它的外观,布局是WIP
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>
通过固定它只需在代码中手动更改宽度。
android:scaleType="fitCenter"
android:adjustViewBounds="true"
尝试将这些将属性添加到ImageView的
感谢您的回复!不幸的是,这些变化并没有解决问题,我还需要有centerCrop,因为它将在以后变得非常重要。 –
,这是正常的的ImageView占据整个版面,如果你有
<ImageView
android:id="@+id/ivMainImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
....
尝试WRAP_CONTENT代替match_parent
但它需要填充尽可能宽的宽度,这就是为什么我要像'android:layout_margin =“8dp”'。 –
你想用'MotionEvent.ACTION_MOVE'实现什么?为什么在'Imageview'中使用'cropToPadding =“false”'? –
感谢您的回复!这是我的理解,'MotionEvent.ACTION_MOVE'是必需的,因为我一直在检测用户手指在屏幕上被按下的位置(纠正我,如果我错了)。 ''cropToPadding =“false”'也是以后需要的(我不确定它是否默认为false) –
@ Tonteria24但是它的工作原理与这个一样,只是这个小问题 –