简体   繁体   English

用Timer重复动画

[英]repeating animation with Timer

I have two animations xml file, i use these in java class. 我有两个动画xml文件,我在java类中使用它们。 I want my animation repeats 1000 msec. 我希望动画重复1000毫秒。 I write this code but when i run this program, animation not repeat. 我写这段代码,但是当我运行该程序时,动画不会重复。 I use Timer in repeatAnim() function but i think this is not work in 1000msec. 我在RepeatAnim()函数中使用Timer,但我认为这在1000毫秒内不起作用。

relative.xml: relative.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">
<TextView
    android:id="@+id/txt1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="@string/txt1" 
   ></TextView>
<ImageButton
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/pic1"
    android:layout_gravity="center"/>
</LinearLayout>

SwitchClass.java: SwitchClass.java:

public class SwitchClass extends Activity implements AnimationListener{

Animation animation1;
Animation animation2;
boolean frontButton=true;
ImageButton btn;
Timer timer;
TimerTask mTimerTask;
Handler handler=new Handler();

boolean repeat=true;

@Override
public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.relative);

         animation1=AnimationUtils.loadAnimation(this, R.anim.to_middle);
     animation1.setAnimationListener(this);
     animation2=AnimationUtils.loadAnimation(this, R.anim.from_middle);
     animation2.setAnimationListener(this);

     btn=(ImageButton)findViewById(R.id.btn2);
     btn.setOnClickListener(onClickListener);

     repeatAnimation(); 
     repeatAnim();       
}

private OnClickListener onClickListener=new OnClickListener(){

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        Intent intent=new Intent(SwitchClass.this,AnimClass.class);
        startActivity(intent);
    }

};

@Override
public void onAnimationEnd(Animation animation) {
    // TODO Auto-generated method stub
    if(animation==animation1){
        if(frontButton){
            btn.setImageResource(R.drawable.pic2);
        }
        else{
            btn.setImageResource(R.drawable.pic1);
        }
        btn.clearAnimation();
        btn.setAnimation(animation2);
        btn.startAnimation(animation2);
    }
    else{
        frontButton=!frontButton;
        btn.setEnabled(true);
    }
}

@Override
public void onAnimationRepeat(Animation animation) {
    // TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
    // TODO Auto-generated method stub

}

public void repeatAnim(){
    timer=new Timer();
    mTimerTask=new TimerTask(){
        public void run(){
            handler.post(new Runnable(){
                public void run(){
                    repeatAnimation();

                }
            });
        }
    };
    timer.schedule(mTimerTask, 1000);
}
public void repeatAnimation(){
    btn.setEnabled(false);
    btn.clearAnimation();
    btn.setAnimation(animation1);
    btn.startAnimation(animation1);
}
}

Thanks for advise.Cheers 感谢您的建议。

What about to use use Handler , see below: 怎样使用use Handler ,如下所示:

private int mSampleDurationTime = 1000; // 1 sec
private boolean continueToRun = true;

Handler mHandler = new Handler();

mHandler.postDelayed(mRunnable, mSampleDurationTime);

where mRunnable is your task: mRunnable是您的任务:

private final Runnable mRunnable = new Runnable() {

 //...
public void run() {

   // do your stuff here, like update
   // this block of code you going to reach every  second

   if(continueToRun == true){
    mHandler.postDelayed(mRunnable, mSampleDurationTime);
   }

 }
    ...
};

First time you call postDelayed and invoke new Runnable() . 第一次调用postDelayed并调用new Runnable() After, if you want to continue, call the same method into run() 之后,如果要继续,请在run()调用相同的方法

why not just use handler.postDelayed(runnable, delay) instead of using a timer? 为什么不只使用handler.postDelayed(runnable,delay)而不是使用计时器呢?

Also, try using animation1.startNow() after setting the animation on the button, and call btn.invalidate() after setting the animation to redraw the view 另外,在按钮上设置动画后,尝试使用animation1.startNow(),并在设置动画后调用btn.invalidate()以重绘视图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM