如何在android中做连续动画?

如何在android中做连续动画?

问题描述:

我希望我的图像按钮从屏幕顶部开始,到达屏幕末端,然后是其后的另一个图像按钮,并且这种效果永远像道路上的白色条带一样。当一个按钮到达屏幕的末端时,不可见的部分必须在屏幕的顶部可见,并且这应该适用于每个按钮。如何在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

我希望我已经清楚我的问题的图像。

你的问题不是很清楚,但我认为你想要回收那些白色的条纹。

您应该为此使用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