0

我正在开发卡罗姆游戏。我试图通过在队列中添加动画来为大约 20 个 ImageViews(Coin) 制作动画.....单个 View(Coin) 的动画很好,但是当我完全制作动画时,它变得非常慢。我希望所有的硬币都能毫无问题地同时进行动画处理。这是我写的代码。

    private void startAnimations(int ID, float[] values) {
    switch (ID) {
    case 1: // 43 float values




        /*
         * 
         * long timeinmill = System.currentTimeMillis();
        System.out.println ("\n++++++++    JAVA  : ANIMATION METHOD ENDING TIME = "+timeinmill+"  +++++++++++\n");
      try {
                  FileWriter fstream = new FileWriter("/sdcard/carromjava.txt");
                  BufferedWriter out = new BufferedWriter(fstream);

                out.write(Long.toString(timeinmill));
                out.close();
      } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
      }
              //Close the output stream
       */   

        int counter=0;
        int conX=0,conY=1;
        boolean go=false;
        if(watch==0){
            pos=getPositions();
            watch=1;
        }


        long duration = (long) (values[41]*1000); //(values[40] * 1000);
        LinearInterpolator linear_interpolator = new  LinearInterpolator(); //AccelerateInterpolator();
        DecelerateInterpolator linear_interpolator1=new DecelerateInterpolator();
        SetAnimation setAnimation = new SetAnimation();
        setAnimation.duration = duration;
        setAnimation.checkPocketFlag = (int) (values[40]);
        setAnimation.produce_sound_type = (int) (values[42]);
    //  AnimatorSet animatorSet = new AnimatorSet();
    //  ArrayList<Animator> animationFull=new ArrayList<Animator>();
        //System.out.println("Values length ="+values.length);
    /*  double dx=Math.abs(pos[0]-(values[0]- board_striker/2.0f));
        double dy=Math.abs(pos[1]-(values[1]- board_striker/2.0f));
        double distance=Math.sqrt((double)(dx*dx)+(dy*dy));
        //double velx=dx/values[41];
        //double vely=dy/values[41];
        double duraX= Math.abs((dx/values[43]))*1000;
        double duraY= Math.abs((dy/values[44]))*1000;
        double velocity=Math.sqrt((double)(values[43]*values[43])+(values[44]*values[44]));
        double dura=distance/velocity;*/
        for (int i = -1; i < 19; i++) {
            if (i == -1) {

                //System.out.println("Coins");
                //System.out.println("x="+pos[0]+"Y="+pos[1]);
                //System.out.println("Animx="+(values[0]- board_striker/2.0f)+"Animy="+(values[1]- board_striker/2.0f));

                ObjectAnimator anim1 = ObjectAnimator.ofFloat(striker,
                        "translationX",pos[counter], values[counter++] - board_striker/2.0f );
                ObjectAnimator anim2 = ObjectAnimator.ofFloat(striker,
                        "translationY",pos[counter], values[counter++] - board_striker/2.0f );
                //Setting Interpolator

                anim1.setInterpolator(linear_interpolator);
                anim2.setInterpolator(linear_interpolator);

                anim1.setDuration((long)values[41]);
                anim2.setDuration((long)values[41]);

            //  if((pos[conX] != (values[conX]-board_striker / 2.0f)) && (pos[conY]!=(values[conY]-board_striker / 2.0f))){
                //System.out.println("Animating");
            //  animationFull.add(anim1);
            //  animationFull.add(anim2);
                AnimatorSet animatorSet = new AnimatorSet();
            //  animatorSet.play(anim1);
            //  animatorSet.play(anim2);

                animatorSet.playTogether(anim1, anim2);
            //  animatorSet.setDuration((long)values[41]);
                animatorSet.setInterpolator(linear_interpolator);
                //animatorSet.start();

                setAnimation.anim_queue.add(animatorSet);
                setAnimation.last_anim = animatorSet;
            //  }
                conX=+2;
                conY=+2;
                continue;
                //break;
            }
            ObjectAnimator anim1 = ObjectAnimator.ofFloat(
                    pieces[i].coinImv, "translationX",
                     values[counter++] - board_carrommen/2.0f);//2*i+2
            ObjectAnimator anim2 = ObjectAnimator.ofFloat(
                    pieces[i].coinImv, "translationY",
                    values[counter++] - board_carrommen/2.0f);//2*i+2+1

            anim1.setInterpolator(linear_interpolator);
            anim2.setInterpolator(linear_interpolator);

            anim1.setDuration((long)values[41]);
            anim2.setDuration((long)values[41]);

        //  if((pos[conX] != (values[conX]- board_carrommen / 2.0f)) && (pos[conY]!=(values[conY]- board_carrommen / 2.0f))){

        //  animationFull.add(anim1);   
        //  animationFull.add(anim2);   
            AnimatorSet animatorSet = new AnimatorSet();
            animatorSet.playTogether(anim1, anim2);


            animatorSet.setInterpolator(linear_interpolator);

            //animatorSet.setDuration((long)values[41]);
            setAnimation.last_anim = animatorSet;
            setAnimation.anim_queue.add(animatorSet);
        //  }
            conX+=2;
            conY+=2;
        }   

        if(watch!=0){
            for(int i=0;i<40;i++){
                if(i==0 || i==1)
                    pos[i]=values[i]- board_striker/2.0f;
                else
                    pos[i]=values[i]-board_carrommen / 2.0f;
            }
        }

    /*  animatorSet.playTogether(animationFull);
        animatorSet.setDuration((long) (duration));
        animatorSet.setInterpolator(linear_interpolator);

        setAnimation.last_anim=animatorSet;*/

        // Create a job and push to job queu

        animation_queue_set.add(setAnimation);

        if (read_from_queue) {
            start_set_anim();
            read_from_queue = false;
        }
   }
   }

    anim_listener = new AnimatorListener() {

        long start_time ;

        public void onAnimationStart(Animator animation) {
            start_time = System.currentTimeMillis();
        }

        public void onAnimationRepeat(Animator animation) {
            // TODO Auto-generated method stub
        }

        public void onAnimationEnd(Animator animation) {
            // TODO Auto-generated method stub
            //long exit = System.currentTimeMillis();
            //logger.info("\t Duration = "+animation.getDuration()+ ", Animation Start time = "+start_time  +" , +++++++  animation actual end = "+ exit +"\n");
            start_set_anim();
        }

        public void onAnimationCancel(Animator animation) {
            // TODO Auto-generated method stub
            // onAnimCancel(animation);
        }
    };

    anim_listener_check_pocket = new AnimatorListener() {

        public void onAnimationStart(Animator animation) {
            // TODO Auto-generated method stub
            // onAnimStart(animation);
            //System.out.println ("\n +++++++  Animation Start delay  =  "+animation.getStartDelay());
            animation.setStartDelay(0);
        }

        public void onAnimationRepeat(Animator animation) {
            // TODO Auto-generated method stub
        }

        public void onAnimationEnd(Animator animation) {
            // TODO Auto-generated method stub
            nativeInterface.IonAnimationEnd();
            start_set_anim();
        }

        public void onAnimationCancel(Animator animation) {
            // TODO Auto-generated method stub
            // onAnimCancel(animation);
        }
    };
    private void start_set_anim() {
    if (animation_queue_set.isEmpty()) {
        read_from_queue = true;
        return;
    }
    SetAnimation recent_anim = animation_queue_set.removeFirst();
    Iterator<AnimatorSet> it = recent_anim.anim_queue.listIterator();


    try{

    if (recent_anim.checkPocketFlag != 0) {
        if(recent_anim.last_anim!=null)
            recent_anim.last_anim.addListener(anim_listener_check_pocket);
        else
            start_set_anim();
    } else {
        if(recent_anim.last_anim!=null)
            recent_anim.last_anim.addListener(anim_listener);
        else
            start_set_anim();

}
    }catch(Exception e){
        System.out.println("Exception Raised "+e);
        e.printStackTrace();
    }

/*  recent_anim.last_anim.setStartDelay(0);
    recent_anim.last_anim.start();*/

    while (it.hasNext()) {
        AnimatorSet aset = it.next();
        aset.setStartDelay(0);
        aset.start();
        //(it.next()).setStartDelay(0);
        //(it.next()).start();
        it.remove();
    }
    /*
     * if (animation_queue_set.isEmpty()) { read_from_queue = true; }
     */
}

请帮助我如何将所有动画平滑在一起。我有一个去 OpenGL 的想法,但不去 OpenGL 有没有办法做到这一点。

4

1 回答 1

0

如果您使用 UI 类来为每个对象设置动画,并且有许多同时具有动画的对象,则始终建议使用 OpenGL 等低级 API。或者,如果您对学习 OpenGL 不满意,您可以使用 Cocoas 2D 或其他优秀的 API,它们只是在低级 API 之上创建了一个强大的框架。

于 2012-07-27T10:06:57.087 回答