2012-02-09 137 views
1

请大家帮忙,我是Android开发中的新手。我只是想动态地创建图像动画,换句话说,比如我想用它们创建3个图像和动画,然后在一些条件之后它应该变成4个图像,5个图像等等。 当我用3个Runnables分别创建3个图像时,它工作正常,但是当我通过动态数组创建这3个Runnable时,它只是无所作为。下面是代码3分别可运行在Android中创建动画

private Runnable run1= new Runnable() { 
public void run() { 
    if(t1) 
    { 
     LayoutParams params1=(LayoutParams) l1.getLayoutParams(); 
     params1.x=x1; 
     params1.y=y1; 
     l1.setLayoutParams(params1); 
     x2=r.nextInt(720-80)+80; 
     y2=r.nextInt(400-80)+80; 

    TranslateAnimation ta1 = new TranslateAnimation(0, x2-x1, 0, y2-y1); 
    ta1.setDuration(800); 
    ta1.setFillAfter(true); 
    l1.startAnimation(ta1); 
    x1=x2; 
    y1=y2; 

    handler.postDelayed(run1, 800); 

    } 
} 

与同为RUN2和RUN3 它工作正常,但下面做什么

for(j=0;j<c;j++)  
{ 
    run[j]=new Runnable() 
    { 
     public void run() { 
      if(t[j]) 

      { 

       params[j]=(LayoutParams) images[j].getLayoutParams(); 
       params[j].x=x1[j]; 
       params[j].y=y1[j]; 
       images[j].setLayoutParams(params[j]); 

       x2[j]=r.nextInt(720-80)+80; 
       y2[j]=r.nextInt(400-80)+80; 

       ta[j] = new TranslateAnimation(0, x2[j]-x1[j], 0, y2[j]-y1[j]); 
       ta[j].setDuration(200); 
       ta[j].setFillAfter(true); 
       images[j].startAnimation(ta[j]); 
       x1[j]=x2[j]; 
       y1[j]=y2[j]; 

       handler.postDelayed(run[j], 200); 
      } 
     } 

    }; 
for(j=0;j<c;j++)  
{ 
    this.runOnUiThread(run[j]); 
} 

如何解决这个问题,我的意思是如何创建的动画与动态数量的图像。

回答

1

首先,你应该检查你的t [j]是否变成“真”,或者它总是保持假(我想你忘了把t [j]设置为真)。 其次还有另一种动态创建图像动画的好方法。您应该将每个图像作为单独类的对象提供,而不是使用普通类中的图像,它应该提供2个属性:图像名称和自己的布尔值t,用于决定此对象是否应具有动画效果。此类应实现Runnable。然后你可以动态地创建这个类的对象数组,并为每个对象运行animation.I是一个客观的C开发人员,所以这里是你可以轻松地转移到Android的代码。

class Single implement Runnable 
{ 
UIImageView* image; 
bool t; 
//method run{ 

//here you should create animation code 

} 
class common 
{ 
static int c=3; 
Single[] arrayOfObj=new Single[3]; 
//method onCreate 
{ 
//create objects of arrayOfObj with images that you want and then call run method for each object 
} 
} 
+0

谢谢你的回答。它对我非常有帮助! – hovo888s 2012-02-09 21:21:04