将大量ImageView添加到屏幕并单独为它们设置动画

时间:2014-02-27 11:52:29

标签: android animation

我想要将ImageView添加到RelativeLayout或不同大小(总是正方形)并单独设置动画,以便它们在屏幕上“浮动”。我有一个开始,但这不太对。

    AnimationSet set = new AnimationSet(true);

    Animation animation = new AlphaAnimation(0.0f, 1.0f);
    animation.setDuration(100);
    set.addAnimation(animation);

    animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
            -1.0f, Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f,
            Animation.RELATIVE_TO_SELF, 0.0f);
    animation.setDuration(1500);


    animation.setRepeatCount(-1);
    animation.setRepeatMode(Animation.REVERSE);
    //animation.setInterpolator(new LinearInterpolator());

    set.addAnimation(animation);

    LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f);
    Resources res = getResources();

    for (int i = 0;i < 20;i++) {
        ImageView iv = new ImageView(this);
        iv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        Random r = new Random();
        int i1 = r.nextInt(150 - 50) + 50;
        float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, i1, res.getDisplayMetrics());
        Log.d("Size", String.valueOf(px));
        iv.setMaxWidth((int) px);
        iv.setMaxHeight((int) px);
        Bitmap image = Bitmap.createBitmap((int) px, (int) px, Bitmap.Config.ARGB_8888);
        String colour = String.valueOf(r.nextInt(999999));
        String col = ("000000" + colour).substring(colour.length());
        Log.d("Colour will be", "#"+col);

        image.eraseColor(Color.parseColor("#"+col));
        iv.setImageBitmap(image);
        //iv.back
        ((RelativeLayout) findViewById(R.id.for_images)).addView(iv, 0);
    }



    ((RelativeLayout) findViewById(R.id.for_images)).setLayoutAnimation(controller);
    controller.start();

这就是我现在所拥有的,但它们似乎一个接一个地制作动画,我怎样才能让它们一次动画?

1 个答案:

答案 0 :(得分:1)

这取决于您正在寻找的最终动画,播放延迟和动画可以获得不同的结果。目前您已为控制器设置了延迟,并且因为视图大小不同,TranslateAnimation在必须键入RELATIVE_TO_SELF时会产生并行效果。这可能是你要找的那个:

animation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1.0f,
        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_PARENT,
        0.0f, Animation.RELATIVE_TO_PARENT, 0.0f);

LayoutAnimationController controller = new LayoutAnimationController(
        set, 0);