2015-10-16 65 views
0

我無法找到此問題的答案。如何根據位置變換線性佈局背景

我想模擬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並使其移動由左到右的LinearLayout裏面?

+0

你使用XML? –

+0

Yeah'slide_in_left'是一個xml文件。但這不是先決條件。我只需要一個解決方案。 –

回答

1

您可以使用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設置動畫(它似乎backgr:主要佈局的ound) :

<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

1

我不知道這是否會爲你以後的工作,但你有沒有想過設置LinearLayout內的ImageView,使高度和寬度相同LinearLayout而不是設置LinearLayout背景是什麼?你可以像現在這樣做動畫ImageView,它會給出相同的結果。

+0

我曾經想過,但是如何在圖像視圖中將線性佈局中的小部件覆蓋? –

+1

你必須將'LinearLayout'包裝在'RelativeLayout'中。將'ImageView'放置在'RelativeLayout'中,將其餘視圖放在'LinearLayout'中。 – vguzzi

+0

謝謝,您是對的,但Sese在您之前回答了使用相對佈局的建議。我對你的評論和回答都讚不絕口。謝謝。 –

相關問題