0

当我执行我的应用程序时,我通过代码准备了一些动画:

public class MainActivity extends ActionBarActivity{

private Animation buscarAnimation2 = null;
private Animation buscarAnimation3 = null;


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

    RelativeLayout RL=(RelativeLayout)findViewById(R.id.RelativeLayout1);


    buscarAnimation2 = AnimationUtils.loadAnimation(this, R.anim.boton2);
    buscarAnimation3 = AnimationUtils.loadAnimation(this, R.anim.boton1);

    RL.getViewTreeObserver().addOnGlobalLayoutListener(
            new ViewTreeObserver.OnGlobalLayoutListener() {
                public void onGlobalLayout() {
                    TextView t1=(TextView)findViewById(R.id.text_trad);
                    TextView t2=(TextView)findViewById(R.id.text_rell);
                    TextView t3=(TextView)findViewById(R.id.text_cub);
                    TextView t4=(TextView)findViewById(R.id.text_busq);

                    t1.setText(getString(R.string.main_trad));
                    t2.setText(getString(R.string.main_rell));
                    t3.setText(getString(R.string.main_cub));
                    t4.setText(getString(R.string.main_busq));

                    ImageButton b= (ImageButton)findViewById(R.id.imageButton1);
                    b.startAnimation(buscarAnimation2);
                    b.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        }
                    });
                    ImageButton b2= (ImageButton)findViewById(R.id.ImageButton01);
                    b2.startAnimation(buscarAnimation3);
                    b2.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                        }
                    });

                    ImageButton b3= (ImageButton)findViewById(R.id.ImageButton03);
                    b3.startAnimation(buscarAnimation2);
                    b3.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                    }
                    });


                    ImageButton b4= (ImageButton)findViewById(R.id.ImageButton02);
                    b4.startAnimation(buscarAnimation3);
                    b4.setOnClickListener(new View.OnClickListener() {

                        @Override
                        public void onClick(View v) {
                                }
                    });
                }
            }); 
}

有时当我执行我的应用程序时,我不会查看动画,如果动画持续 4 秒,我会看到黑色主题,然后是动画的最后一秒或最后 2 秒。就像加载时遇到问题之类的...

那么我该怎么做才能避免这种情况,或者我做错了什么

编辑了代码。

4

2 回答 2

0

如何开始动画onResume呢?此时,您确定 Activity 是可见的(并且正在运行)。就像是:

private Animation buscarAnimation1 = null;
private Animation buscarAnimation2 = null;
private Animation buscarAnimation3 = null;
private Animation buscarAnimation4 = null;

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

    // Other initialization code.

    buscarAnimation1 = AnimationUtils.loadAnimation(this, R.anim.boton1);
    buscarAnimation2 = AnimationUtils.loadAnimation(this, R.anim.boton2);
    buscarAnimation3 = AnimationUtils.loadAnimation(this, R.anim.boton2);
    buscarAnimation4 = AnimationUtils.loadAnimation(this, R.anim.boton1);
}

@Override
protected void onResume() {
    super.onResume();

    ImageButton b1= (ImageButton)findViewById(R.id.imageButton1);
    b1.startAnimation(buscarAnimation1);
    ImageButton b2= (ImageButton)findViewById(R.id.ImageButton01);
    b2.startAnimation(buscarAnimation2);
    ImageButton b3= (ImageButton)findViewById(R.id.ImageButton03);
    b3.startAnimation(buscarAnimation3);
    ImageButton b4= (ImageButton)findViewById(R.id.ImageButton02);
    b4.startAnimation(buscarAnimation4);
}
于 2012-09-30T18:02:10.487 回答
0

ViewTreeObserver附加到您的布局,并覆盖它的onGlobalLayoutListener()方法以调用您的动画,因为一旦完全绘制了布局,就会调用该方法。

于 2012-09-30T18:08:22.480 回答