Android 宽度大于屏幕的 ImageView setLayoutParams

Android ImageView setLayoutParams with width bigger than Screen

我的 ImageView 布局 xml 是这样的:

<ImageView
        android:id="@+id/notePointerImageView"
        android:layout_width="700px"
        android:layout_height="6px"
        android:scaleType="fitXY"
        android:src="@drawable/whiteline"

        />

然后我在 onWindowFocusChanged 中动态设置宽度、高度和边距:

RelativeLayout.LayoutParams notePointerLayoutLP = new RelativeLayout.LayoutParams(263, 2); 
        notePointerLayoutLP.setMargins(0, 150, 14, 0);
        notePointerImageView.setLayoutParams(notePointerLayoutLP);
        notePointerImageView.requestLayout();

由于屏幕宽度只有240x301,所以imageView的左边部分不在屏幕上。但看起来 imageView 的那部分被删除了,图像在屏幕内调整了大小(我使用了 fitXY),并且 ImageView 的大小为 226x2。 (226 + 14 == 240,也就是屏幕宽度)

由于稍后我必须对 ImageView 进行动画处理,因此我不能接受对 ImageView 进行裁剪和调整大小。我真的需要一个负的左边距。有人能告诉我我该怎么做吗?还是只是模拟器的问题?

要强制大图像适合屏幕或某个尺寸,您可以使用 FrameLayout 作为:

<FrameLayout
    android:id="@+id/fm_image"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"

       ... ...
>

    <ImageView
        android:id="@+id/myImage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:layout_gravity="center"
        android:gravity="center"

        android:src="@drawable/my_image"/>

</FrameLayout>

为了适合特定尺寸,您只需设置 FrameLayout 的布局尺寸。

希望对您有所帮助!