1

我想从左到右一张一张地为 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;
}

}

4

1 回答 1

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);
}
于 2013-06-03T10:04:50.337 回答