没有使用horrizontalScrollView,而是将整个布局的宽度强制为图片大小,在里面设置一个RelativeLayout,和屏幕大小一样,然后制作动画。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="1103dp"
android:layout_height="736dp"
android:background="@color/white">
<ImageView
android:id="@+id/background"
android:visibility="invisible"
android:src="@drawable/story1"
android:scaleType="fitXY"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="@+id/screen_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
-----------------CODE inside relative layout for normal page--------
</RelativeLayout>
</RelativeLayout>
然后在我的代码中,我为我的屏幕容器设置屏幕宽度和高度(包含除了要翻译的背景图片之外的所有内容):
screenContainer.setLayoutParams(new RelativeLayout.LayoutParams(Math.round(Constants.screenWidth), Math.round(Constants.screenHeight)));
这是我对匹配父级的ImageView的翻译(背景图片的大小):
TranslateAnimation translateBackground = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_PARENT,from,
TranslateAnimation.RELATIVE_TO_PARENT,-0.5f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f,
TranslateAnimation.RELATIVE_TO_PARENT,0.0f);
translateBackground.setDuration(15000);
background.setVisibility(View.VISIBLE);
background.startAnimation(translateBackground);