以不同方式翻译多个imageview

时间:2015-03-23 10:15:02

标签: android

我一直在玩动画 - 只是为了让我了解它们的工作原理。我有大多数我想做的事情,但我坚持的一件事是如何以不同的方式翻译不同的图像视图。 (我已经完全放弃了旋转,现在这很好)。在我下面的代码中,我有5个图像,我认为如果我使每个translateAnimation中的参数不同,那么每个图像视图会做不同的事情,但他们没有,他们都做同样的事情事情。我还不太清楚所有参数的用途,所以我希望能够通过为不同的图像设置不同的参数来查看差异,并比较屏幕上每个参数的发生情况。我把它们都放在一组动画中,所以我不知道这是不是问题呢?或者他们在一个阵列中的事实?或者我没有让参数变得不同,所以肉眼看来它们看起来都一样吗?或者我错过了我的代码中有一个愚蠢的错误?或者只是不可能做我想要的事情?如果我将它们从setAnimation中取出,它似乎只会移动第一个,而忽略其余的。我特别想让一个上屏,一个上下,一个穿过L到R,一个穿过R到L,一个是对角线,所有在同一时间,但是开始想知道这是不可能的吗?

public void onWindowFocusChanged(boolean hasFocus)     {         super.onWindowFocusChanged(hasFocus);

    final ImageView[] w = new ImageView[5];

    w[0] = (ImageView) findViewById(R.id.w1);
    w[1] = (ImageView) findViewById(R.id.w2);
    w[2] = (ImageView) findViewById(R.id.w3);
    w[3] = (ImageView) findViewById(R.id.w4);
    w[4] = (ImageView) findViewById(R.id.w5);

    //animate images.
    //First set up an AnimationSet to put all the animations into
    AnimationSet move_w = new AnimationSet(true);
    //set up the rotations for each image
    RotateAnimation rotate_w[] = new RotateAnimation[5];
    for (int i=0; i<5; i++) {
        rotate_w[i] = new RotateAnimation(0f, 300f, w[i].getWidth()/2, w[i].getHeight()/2);
        rotate_w[i].setStartOffset(50);
        rotate_w[i].setRepeatCount(Animation.INFINITE);
        rotate_w[i].setDuration(9500);
        move_w.addAnimation(rotate_w[i]);
    }

    //set up the translations (across the screen movements)
    TranslateAnimation trans_w[] =  new TranslateAnimation[5];
    trans_w[0] =  new TranslateAnimation(Animation.ABSOLUTE, -10, Animation.ABSOLUTE, -10, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_PARENT, (float) 0.8);
    trans_w[1] =  new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.1f, Animation.RELATIVE_TO_PARENT, 0.3f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.9f);
    trans_w[2] =  new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.1f, Animation.RELATIVE_TO_PARENT, -0.3f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.9f);
    trans_w[3] =  new TranslateAnimation(Animation.ABSOLUTE, 0.1f, Animation.ABSOLUTE, 0.3f, Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -0.9f);
    trans_w[4] =  new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.1f, Animation.RELATIVE_TO_PARENT, 0.3f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -0.9f);

    for (int i=0; i<5; i++) {

        trans_w[i].setRepeatCount(Animation.INFINITE);
        trans_w[i].setDuration(12000);
        move_w.addAnimation(trans_w[i]);

    }
        move_w.setRepeatMode(Animation.INFINITE);

    if(hasFocus) {

        for (int i=0; i<5; i++) {
            w[i].startAnimation(move_w);
        }
    } else {

        for (int i=0; i<5; i++) {
           w[i].setAnimation(null);
        }
    }

}

1 个答案:

答案 0 :(得分:0)

男人,用我代码代替你。在你做了这件好事的那一刻,但是你想念了。 U有5个ImageView's和一个AnimationSet,这是不正确的,导致一个View u传递2动画。所以你必须要有5 AnimationSet's

代码:

 w[0] = (ImageView) findViewById(R.id.w1);
        w[1] = (ImageView) findViewById(R.id.w2);
        w[2] = (ImageView) findViewById(R.id.w3);
        w[3] = (ImageView) findViewById(R.id.w4);
        w[4] = (ImageView) findViewById(R.id.w5);

        //animate images.
        //First set up an AnimationSet to put all the animations into
        AnimationSet[] move_w = new AnimationSet[5];
        //set up the rotations for each image
        RotateAnimation rotate_w[] = new RotateAnimation[5];
        for (int i=0; i<5; i++) {
            rotate_w[i] = new RotateAnimation(0f, 300f, w[i].getWidth()/2, w[i].getHeight()/2);
            rotate_w[i].setStartOffset(50);
            rotate_w[i].setRepeatCount(Animation.INFINITE);
            rotate_w[i].setDuration(9500);
            move_w[i] = new AnimationSet(true);
            move_w[i].addAnimation(rotate_w[i]);
        }

        //set up the translations (across the screen movements)
        TranslateAnimation trans_w[] =  new TranslateAnimation[5];
        trans_w[0] =  new TranslateAnimation(Animation.ABSOLUTE, -10, Animation.ABSOLUTE, -10, Animation.RELATIVE_TO_SELF, (float) 0.5, Animation.RELATIVE_TO_PARENT, (float) 0.8);
        trans_w[1] =  new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.1f, Animation.RELATIVE_TO_PARENT, 0.3f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.9f);
        trans_w[2] =  new TranslateAnimation(Animation.RELATIVE_TO_SELF, -0.1f, Animation.RELATIVE_TO_PARENT, -0.3f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.9f);
        trans_w[3] =  new TranslateAnimation(Animation.ABSOLUTE, 0.1f, Animation.ABSOLUTE, 0.3f, Animation.ABSOLUTE, 0.0f, Animation.ABSOLUTE, -0.9f);
        trans_w[4] =  new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.1f, Animation.RELATIVE_TO_PARENT, 0.3f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -0.9f);

        for (int i=0; i<5; i++) {
            trans_w[i].setRepeatCount(Animation.INFINITE);
            trans_w[i].setDuration(2000);
            move_w[i].addAnimation(trans_w[i]);
            move_w[i].setRepeatMode(Animation.INFINITE);
        }

        if(hasFocus) {
            for (int i=0; i<5; i++) {
                w[i].startAnimation(move_w[i]);
            }
        } else {
            for (int i=0; i<5; i++) {
                w[i].setAnimation(null);
            }
        }