移动 RelativeLayout 时 ImageView 的大小略有增加 (Android)
ImageView increases slightly in size when moving RelativeLayout (Android)
我遇到了一个烦人的问题,当我移动我的 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>
android:scaleType="fitCenter"
android:adjustViewBounds="true"
尝试将这些添加到 ImageView 的属性中
ImageView占据整个布局是正常的
<ImageView
android:id="@+id/ivMainImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
....
尝试 wrap_content 而不是 match_parent
通过在代码中手动更改宽度来修复它。
我遇到了一个烦人的问题,当我移动我的 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>
android:scaleType="fitCenter"
android:adjustViewBounds="true"
尝试将这些添加到 ImageView 的属性中
ImageView占据整个布局是正常的
<ImageView
android:id="@+id/ivMainImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
....
尝试 wrap_content 而不是 match_parent
通过在代码中手动更改宽度来修复它。