当我执行我的应用程序时,我通过代码准备一些动画:
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秒。就好像装载有问题还是什么......
那么我怎么能避免这个,或者我做错了什么
编辑代码。
答案 0 :(得分:0)
如何在onResume
中启动动画呢?此时,您确定活动是可见的(并且正在运行)。类似的东西:
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);
}
答案 1 :(得分:0)
将ViewTreeObserver附加到您的布局,并覆盖它的onGlobalLayoutListener()方法以调用您的动画,因为一旦完全绘制了布局,就会调用它。