显示多个图像动画

时间:2011-07-25 04:39:42

标签: android android-2.2-froyo

我想要显示背景图像和多个图像向下移动,就像“降雪”场景一样,我该怎么办呢?背景图像不应该移动,只有它上面的小图像应该掉下来。我怎么能这样做? ???

更新 - > 我在屏幕上显示图像,但它们都是在同一时间出现,但我想在不同的时间显示图像我该怎么做?嘿,请提示一些方法。这是正确的方式或者如果没有请建议我正确方式....

这是我的代码:

public class AnimationActivity extends Activity implements AnimationListener 
{
    /** Called when the activity is first created. */

    LinearLayout layout;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Animation movement;
        layout = (LinearLayout) findViewById(R.id.linearLayout_ani);
        movement = AnimationUtils.loadAnimation(this,R.layout.abc);
        movement.reset();
        movement.setFillAfter(true);
        movement.setAnimationListener(this);
        movement.setRepeatCount(1);
        movement.setRepeatMode(7);

        layout.startAnimation(movement);            

    }
    @Override 
    public void onAnimationEnd(Animation movement) 
    {
        // TODO Auto-generated method stub      
    }
    @Override
    public void onAnimationRepeat(Animation arg0) 
    {
        // TODO Auto-generated method stub

    }
    @Override
    public void onAnimationStart(Animation arg0) 
    {
    }
}

这是动画布局文件:

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%"
    android:toXDelta="50%"
    android:fromYDelta="0%"
    android:toYDelta="95%" 
    android:duration="10000" 
    android:zAdjustment="normal" />

main .xml文件

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/linearLayout"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     android:background="@drawable/wall">

    <LinearLayout android:id="@+id/linearLayout_ani"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="horizontal" 
     >   
    <ImageView android:id="@+id/snowimg1" 
       android:layout_width ="wrap_content"
       android:layout_height ="wrap_content"
       android:visibility="visible" 
       android:background="@drawable/snow1"
       ></ImageView>
       <ImageView android:id="@+id/snowimg2" 
       android:layout_width ="wrap_content"
       android:layout_height ="wrap_content"
       android:visibility="visible" 
       android:background="@drawable/snow1"
       ></ImageView>
       <ImageView android:id="@+id/snowimg3" 
       android:layout_width ="wrap_content"
       android:layout_height ="wrap_content"
       android:visibility="visible" 
       android:background="@drawable/snow1"
       ></ImageView>
       <ImageView android:id="@+id/snowimg4" 
       android:layout_width ="wrap_content"
       android:layout_height ="wrap_content"
       android:visibility="visible" 
       android:background="@drawable/snow1"
       ></ImageView>
       <ImageView android:id="@+id/snowimg5" 
       android:layout_width ="wrap_content"
       android:layout_height ="wrap_content"
       android:visibility="visible" 
       android:background="@drawable/snow1"
       ></ImageView>
       <ImageView android:id="@+id/snowimg6" 
       android:layout_width ="wrap_content"
       android:layout_height ="wrap_content"
       android:visibility="visible" 
       android:background="@drawable/snow1"
       >
    </ImageView>
    </LinearLayout> 

</LinearLayout>    

1 个答案:

答案 0 :(得分:3)

你可以为所有图像添加动画师,如下所示:

private final int REPEAT_COUNT = 1;
private final int REPEAT_MODE = 7;

private void startAnimations() {
    // Using the ImageView not the layout.
    ImageView snowImg1 = (ImageView) findViewById(R.id.snowimg1);
    ImageView snowImg2 = (ImageView) findViewById(R.id.snowimg2);
    ImageView snowImg3 = (ImageView) findViewById(R.id.snowimg3);
    ImageView snowImg4 = (ImageView) findViewById(R.id.snowimg4);
    ImageView snowImg5 = (ImageView) findViewById(R.id.snowimg5);
    ImageView snowImg6 = (ImageView) findViewById(R.id.snowimg6);

    ImageView snowArray[] = {snowImg1, snowImg2, snowImg3, snowImg4, snowImg5, snowImg6};

    // If it is not the same movement, you will need to create different layouts
    Animation snowMov1 = AnimationUtils.loadAnimation(this, R.layout.snowimg1);
    Animation snowMov2 = AnimationUtils.loadAnimation(this, R.layout.snowimg2);
    Animation snowMov3 = AnimationUtils.loadAnimation(this, R.layout.snowimg3);
    Animation snowMov4 = AnimationUtils.loadAnimation(this, R.layout.snowimg4);
    Animation snowMov5 = AnimationUtils.loadAnimation(this, R.layout.snowimg5);
    Animation snowMov6 = AnimationUtils.loadAnimation(this, R.layout.snowimg6);

    Animation movArray[] = {snowMov1, snowMov2, snowMov3, snowMov4, snowMov5, snowMov6};

    // Start the movement animation.
    startMovement(snowArray, movArray);
}

private void startMovement(ImageView imgArray[], Animation movArray[]) {
    // Same length so there is no problem...
    int length = imgArray.length;
    for(int i = 0; i < length; i++) {
        movArray[i].reset();
        movArray[i].setFillAfter(true);
        movArray[i].setAnimationListener(this);
        movArray[i].setRepeatCount(REPEAT_COUNT);
        movArray[i].setRepeatMode(REPEAT_MODE);

        // Start the animation
        imgArray[i].startAnimation(movArray[i]);
    }
}

你需要在动画文件中进行随机动作,这没有经过测试,所以我不确定它是否有效......我希望它至少对我有所帮助。

修改

不要急躁,如果我的方法不起作用,帮助会很快到来!,你只需要等待男人......