在Android中创建动画

时间:2012-02-09 08:33:35

标签: android animation

请帮忙,我是Android开发的新手。我只是想动态地创建图像动画,换句话说,例如我想用它们创建3个图像和动画,然后在某些条件下它应该变成4个图像,5个图像等等。 当我使用3个Runnables分别创建3个图像时,它工作正常,但是当我通过动态数组创建这3个Runnables时,它就是什么也没做。这是带有3个Runnables

的代码
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 个答案:

答案 0 :(得分:1)

首先你应该检查你的t [j]是否变为“true”,或者它总是保持为假(我想你忘记将t [j]设置为true)。 其次,还有另一种动态创建图像动画的好方法。您应该将每个图像作为单独类的对象呈现,而不是使用公共类中的图像,它应该提供2个属性:图像名称和自己的布尔值t,用于决定此对象是否应该生成动画。此类应该实现Runnable。然后你可以动态创建这个类的对象数组,并为每个对象运行动画。我是一个客观的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 
}
}