如何从左到右android逐个动​​画图像

时间:2013-04-08 10:00:45

标签: android android-layout android-animation

我想从左到右逐个动画9张图片。我已经尝试过编码,所有图像都在同时移动。我需要逐个移动每个图像。

公共类MainActivity扩展了Activity {

ArrayList<ImageView> imageHolder;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    animateBottles();
}

private void animateBottles() {
    ImageView upperRowBottleOneImageView = (ImageView) findViewById(R.id.upperRowBottleOneImageView);
    ImageView upperRowBottleTwoImageView = (ImageView) findViewById(R.id.upperRowBottleTwoImageView);
    ImageView upperRowBottleThreeImageView = (ImageView) findViewById(R.id.upperRowBottleThreeImageView);
    ImageView upperRowBottleFourImageView = (ImageView) findViewById(R.id.upperRowBottleFourImageView);
    ImageView upperRowBottleFiveImageView = (ImageView) findViewById(R.id.upperRowBottleFiveImageView);
    ImageView upperRowBottleSixImageView = (ImageView) findViewById(R.id.upperRowBottleSixImageView);
    ImageView upperRowBottleSevenImageView = (ImageView) findViewById(R.id.upperRowBottleSevenImageView);
    ImageView upperRowBottleEightImageView = (ImageView) findViewById(R.id.upperRowBottleEightImageView);
    ImageView upperRowBottleNineImageView = (ImageView) findViewById(R.id.upperRowBottleNineImageView);

    imageHolder = new ArrayList<ImageView>();

    imageHolder.add(upperRowBottleOneImageView);
    imageHolder.add(upperRowBottleTwoImageView);
    imageHolder.add(upperRowBottleThreeImageView);
    imageHolder.add(upperRowBottleFourImageView);
    imageHolder.add(upperRowBottleFiveImageView);
    imageHolder.add(upperRowBottleSixImageView);
    imageHolder.add(upperRowBottleSevenImageView);
    imageHolder.add(upperRowBottleEightImageView);
    imageHolder.add(upperRowBottleNineImageView);

    for (int i = 0; i < imageHolder.size(); i++) {
        ImageView imageView = imageHolder.get(i);

        ObjectAnimator mover = ObjectAnimator.ofFloat(imageView,
                "translationX", -150f, 50f);
        mover.setDuration(4000);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(mover);
        animatorSet.start();
        mover.addListener(new AnimatorListener() {

            @Override
            public void onAnimationCancel(Animator arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationEnd(Animator arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationRepeat(Animator arg0) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onAnimationStart(Animator arg0) {
                // TODO Auto-generated method stub

            }

        });}

        //try {
            //Thread.sleep(1000);
        //} catch (InterruptedException e) {
        //  // TODO Auto-generated catch block
            //e.printStackTrace();
        //}
    //}
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 个答案:

答案 0 :(得分:0)

为了逐个动画图像,您必须为每个图像设置开始偏移延迟,如:

for (int i = 0; i < imageHolder.size(); i++) {
        ImageView imageView = imageHolder.get(i);

        TranslateAnimation animation = new TranslateAnimation(fromX, ToX, FromY, ToY);
        animation.setDuration(400);
        animation.setStartOffset(100*i);

        imageView.startAnimation(animation);
}