最近发现所有sample中的Animation子类实例化都是在onclick等event中(临时变量)。尝试在OnCreate()中实例化Animation子类,并在event中调用,动画效果就没有了。如果每次都要在用到的地方再实例化Animation,效率也太低了吧。

解决方案 »

  1.   

    可以,不过你如果手动cancel掉animation,需要调用reset()方法,在重新start()animation
    详见:http://developer.android.com/reference/android/view/animation/Animation.html
      

  2.   


    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.overlayer_main);
            final View view = findViewById(R.id.linearLayout2);
    view.setVisibility(View.INVISIBLE);

    // final Animation showAnim = new TranslateAnimation(0.0f, 0.0f, 0.0f-view.getHeight(), 0.0f);<---------------------
    // showAnim.setDuration(500);<------------------

    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {      public void onClick(View v) {
    if (!view.isShown()) {
    Animation showAnim = new TranslateAnimation(0.0f, 0.0f, 0.0f-view.getHeight(), 0.0f);
    showAnim.setDuration(500);
    view.setVisibility(View.VISIBLE);
    view.startAnimation(showAnim);
    }
         }
           });
    如果放开OnCreate()中的 new TranslateAnimation,注释掉OnClick()中的 new TranslateAnimation,动画就没有了。奇怪的问题。
      

  3.   

    这个东西就好像线程一样,当线程运行完了,你再次start()是没有用的。