2

如何在一段时间后为多个对象设置动画我正在使用此代码进行动画处理

 for (int y = 0; y < 10; y++) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                }
                Toast.makeText(ProjectFirstActivity.this, y + "",
                        Toast.LENGTH_SHORT).show();
                ObjectAnimator oa = ObjectAnimator.ofFloat(myimageview[y],
                        "translationY", 0, -200);
                oa.setDuration(1000);
                oa.start();
            }
        });
    }

但它是一次性动画,但我想要我的图像视图的一个一个动画,请帮助

4

5 回答 5

8

无需使用线程和处理程序。而不是那种用途,

objectAnimator.setStartDelay(delay);

使用它,您可以为相应的“延迟”停止该动画

试试这个代码..

  int delay = 1000;
  for (int y = 0; y < 10; y++) {                         
      Toast.makeText(ProjectFirstActivity.this, y + "",
                    Toast.LENGTH_SHORT).show();
      ObjectAnimator oa = ObjectAnimator.ofFloat(myimageview[y],
                    "translationY", 0, -200);
      oa.setDuration(1000);
      oa.setStartDelay(delay);
      oa.start();
      delay = delay+1000;
   }
于 2013-11-09T06:03:02.790 回答
1

您可以使用 for 循环生成AnimatorSet然后使用顺序播放它们set.playSequentially(yourAnimatorSetListHere);

我没有在下面提到的片段中使用列表。

  AnimatorSet decSet = new AnimatorSet();
            decSet.playTogether(
                    ObjectAnimator.ofArgb(mTextSlidetoCancel, "visibility", View.GONE, View.VISIBLE),
                    ObjectAnimator.ofArgb(mBtnMicActive, "visibility", View.GONE, View.VISIBLE),
                    ObjectAnimator.ofArgb(mChronometer, "visibility", View.GONE, View.VISIBLE));
           decSet.setDuration(125);


            AnimatorSet decSet2 = new AnimatorSet();
            decSet2.playTogether(
                    ObjectAnimator.ofArgb(mBtnAttach, "visibility", View.VISIBLE, View.GONE),
                    ObjectAnimator.ofArgb(mBtnEmoji, "visibility", View.VISIBLE, View.GONE),
                    ObjectAnimator.ofArgb(mEditTExt, "visibility", View.VISIBLE, View.GONE)
                    );
            decSet2.setDuration(125);

            AnimatorSet set = new AnimatorSet();
            set.playSequentially(decSet2,decSet);
            set.start();
于 2017-12-12T14:11:57.690 回答
0

试试这个方法!

ImageView image1;
public int currentimageindex1=0;
Timer timer;
TimerTask task;

private Animation animation1;
private Animation animation2;

int img[] = {R.drawable.aa, R.drawable.bb, 
        R.drawable.cc,R.drawable.dd,R.drawable.ee,R.drawable.ff,R.drawable.gg,
        R.drawable.hh};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Handler mHandler = new Handler();

    // Create runnable for posting
    final Runnable mUpdateResults = new Runnable() {
        public void run() {

            AnimateandSlideShow();

        }
    };

    int delay = 1000; // delay for 1 sec.

    int period = 4000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         mHandler.post(mUpdateResults);

    }

    }, delay, period);



}

public void onClick(View v) {

    finish();
    android.os.Process.killProcess(android.os.Process.myPid());
  }

/**
 * Helper method to start the animation on the splash screen
 */
private void AnimateandSlideShow() {

    image1 = (ImageView)findViewById(R.id.imageView1);


    image1.setImageResource(img[currentimageindex1%img.length]);
    currentimageindex1++;


}
于 2014-11-27T09:59:14.693 回答
0

试试这个方法!

public Integer[] Imgid = {R.drawable.anim2, R.drawable.anim3,
            R.drawable.anim4, R.drawable.anim5}; // your image array.
    Animation anim;
    Handler handler;
    ImageView iv;
    int count;
    Runnable galleryAnimRunnable;

在 onCreate 里面

iv= (ImageView)findViewById(R.id.imagetype);
handler = new Handler();

   galleryAnimRunnable = new Runnable() {
    public void run() {

        if(count==Imgid.length){
            //do any action
        }
        else{
            anim = AnimationUtils.loadAnimation(AnimationScreen.this, R.anim.fade_out); // load your desire animation.
            iv.setBackgroundResource(Imgid[count]);
            iv.setAnimation(anim);
            count++;
            handler.postDelayed(galleryAnimRunnable, 300); //set your desire delay time.
        }

    }
};
于 2013-11-09T05:58:05.967 回答
0

您的sleep电话没有帮助,因为当一个已发布的 UI 作业进入睡眠状态时,下一个将开始并在此之后立即进入睡眠状态,依此类推。然后第一个作业唤醒并运行,紧随其后的是第二个作业唤醒并运行,依此类推。

如果您想获得sleep帮助,您需要在发布每个工作之前等待,这可能很简单,如下所示:

for (int y = 0; y < 10; y++) {
    try {
        Thread.sleep(1000);
    } catch (InterruptedException e) {
    }
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            Toast.makeText(ProjectFirstActivity.this, y + "",
                    Toast.LENGTH_SHORT).show();
            ObjectAnimator oa = ObjectAnimator.ofFloat(myimageview[y],
                    "translationY", 0, -200);
            oa.setDuration(1000);
            oa.start();
        }
    });
}

编辑:尽管上面解释了您遇到问题的原因,但我建议您使用类似 Jgdsh Seerm 使用setStartDelay().

于 2013-11-09T05:58:13.467 回答