如何根据位置变换linearlayout背景



我很难找到这个问题的答案。

我想在ImageView中模拟图像的变换,例如

image1 = (ImageView) findViewById(R.id.image1);
animationSlideInLeft = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
image1.startAnimation(animationSlideInLeft);

但那不是我真正想要的。相反,我想将drawable集合转换为linear layout背景。

linearLayout.setBackground(drawable); 

我怎么能改变drawable,使它从左到右的线性布局内移动?

你可以使用RelativeLayout或AbsoluteLayout,把一个图像(覆盖它的父)在它,然后挂载当前布局的活动。现在,如果你为那个图像设置动画,看起来你的活动的背景动画。
例如,假设这是你的活动的当前布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
</LinearLayout>

现在这个布局看起来像以前的布局,你可以为图像设置动画,它的ID是img1(看起来像主布局的背景):

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_x="0dp"
        android:layout_y="0dp"
        android:src="@drawable/bright_sun" />
    <LinearLayout
    android:id="@+id/vg2"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_launcher" />
    </LinearLayout>
</RelativeLayout>

找到这里:Android activity - background drawable animated

我不确定这是否会为你所追求的工作,但你有没有考虑过在LinearLayout内部设置ImageView,并使高度和宽度与LinearLayout相同,而不是设置LinearLayout背景?你可以像现在一样给ImageView添加动画,效果也一样。

最新更新