2017-06-02 157 views
0

我希望我的圖像按鈕從屏幕頂部開始,到達屏幕末端,然後是其後的另一個圖像按鈕,並且這種效果永遠像道路上的白色條帶一樣。當一個按鈕到達屏幕的末端時,不可見的部分必須在屏幕的頂部可見,並且這應該適用於每個按鈕。如何在android中做連續動畫?

查看最後一張圖片按鈕的圖片以獲得清晰的視圖。

Screenshot

這是我的XML文件

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context="com.ash432.itguesses.options" 
android:background="#040404"> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im1" 
    android:layout_marginLeft="29dp" 
    android:layout_marginStart="29dp" 
    android:background="#f9f6f6" 
    android:layout_alignParentTop="true" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentStart="true" 
    android:layoutAnimation="@anim/layout_anim" 
    /> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im2" 
    android:background="#f9f6f6" 
    android:layout_marginTop="26dp" 
    android:layout_below="@+id/im1" 
    android:layout_alignLeft="@+id/im1" 
    android:layout_alignStart="@+id/im1" /> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im3" 
    android:background="#f9f6f6" 
    android:layout_marginTop="26dp" 
    android:layout_below="@+id/im2" 
    android:layout_alignLeft="@+id/im2" 
    android:layout_alignStart="@+id/im2" /> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im4" 
    android:background="#ffffff" 
    android:layout_marginTop="26dp" 
    android:layout_below="@+id/im3" 
    android:layout_alignLeft="@+id/im3" 
    android:layout_alignStart="@+id/im3" /> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im5" 
    android:background="#ffffff" 
    android:layout_marginTop="26dp" 
    android:layout_below="@+id/im4" 
    android:layout_alignLeft="@+id/im4" 
    android:layout_alignStart="@+id/im4" /> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im6" 
    android:background="#ffffff" 
    android:layout_marginTop="26dp" 
    android:layout_below="@+id/im5" 
    android:layout_alignLeft="@+id/im5" 
    android:layout_alignStart="@+id/im5" /> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im7" 
    android:background="#ffffff" 
    android:layout_marginTop="26dp" 
    android:layout_below="@+id/im6" 
    android:layout_alignLeft="@+id/im6" 
    android:layout_alignStart="@+id/im6" /> 

<ImageButton 
    android:layout_width="5dp" 
    android:layout_height="40dp" 
    android:id="@+id/im8" 
    android:background="#ffffff" 
    android:layout_marginTop="26dp" 
    android:layout_below="@+id/im7" 
    android:layout_alignLeft="@+id/im7" 
    android:layout_alignStart="@+id/im7" /> 

代碼這裏是Java代碼

public class options extends AppCompatActivity 
{ 
private ImageButton im1,im2,im3,im4,im5,im6,im7,im8; 
private Animation mAnimation; 
//private Button b; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_options); 


    im1 = (ImageButton)findViewById(R.id.im1); 
    im2 = (ImageButton)findViewById(R.id.im2); 
    im3 = (ImageButton)findViewById(R.id.im3); 
    im4 = (ImageButton)findViewById(R.id.im4); 
    im5 = (ImageButton)findViewById(R.id.im5); 
    im6 = (ImageButton)findViewById(R.id.im6); 
    im7 = (ImageButton)findViewById(R.id.im7); 
    im8 = (ImageButton)findViewById(R.id.im8); 

    mAnimation = new TranslateAnimation(
      TranslateAnimation.ABSOLUTE, 0f, 
      TranslateAnimation.ABSOLUTE, 0f, 
      TranslateAnimation.RELATIVE_TO_PARENT, 0.0f, 
      TranslateAnimation.RELATIVE_TO_PARENT, 1.0f); 
    mAnimation.setDuration(2000); 
    mAnimation.setRepeatCount(-1); 
    mAnimation.setRepeatMode(Animation.INFINITE); 
    mAnimation.setInterpolator(new LinearInterpolator()); 
    mAnimation.getStartTime(); 
    im1.setAnimation(mAnimation); 
    im2.setAnimation(mAnimation); 
    im3.setAnimation(mAnimation); 
    im4.setAnimation(mAnimation); 
    im5.setAnimation(mAnimation); 
    im6.setAnimation(mAnimation); 
    im7.setAnimation(mAnimation); 
    im8.setAnimation(mAnimation); 
} 
} 

最新情況:

適合那些無法正確理解問題的人。

1)起初我不想在屏幕上只是背景。

2)然後過一段時間後,應該從屏幕的頂部看到白色條。

3)然後當它完全到來後,過了一段時間後應該會看到第二個白色條紋。

4)所有的相同程序八個圖像按鈕

5)現在,當第一個圖像按鈕到達屏幕的底部。它的大小應該減小,而在屏幕的頂部它的大小應該會增加。 類似地,對於所有的按鈕,當他們到達屏幕

6)的底部的條帶之間的間隙應恆定

下面是該步驟 -

Image

我希望我已經清楚我的問題的圖像。

回答

0

你的問題不是很清楚,但我認爲你想要回收那些白色的條紋。

您應該爲此使用RecyclerView。

compile 'com.android.support:recyclerview-v7:23.1.1' 
在XML

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
tools:context="com.ash432.itguesses.options" 
android:background="#040404"> 

<android.support.v7.widget.RecyclerView 
     android:id="@+id/recycler_view" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:scrollbars="vertical" /> 

</RelativeLayout> 

然後爲那些條紋另一個XML佈局和使用Adapter連接它們。

編輯:

RecyclerViewListView的高級版本具有更好performance.It是一個視圖,以前用於特定適配器位置顯示數據可被放置在一個高速緩存中稍後重用以顯示相同類型數據再次在之後。

Adapter是RecyclerView.Adapter的一個子類,負責提供表示數據集中項目的視圖。

here是一個很好的來源,您可以瞭解如何實施recyclerview。

+0

其實我是Android新手。所以我不知道如何使用適配器和Recycler View.Could你請詳細闡述? – Ash

+0

@Ash檢查我的編輯。 –

+0

我在實現Adapter和Recycler View時遇到了困難。另外我的Android Studio不支持Recycler View。你可以建議任何其他方式來做到這一點?如果你理解我的問題,也請閱讀我的更新。 – Ash