2012-07-27 3 views
0

저는 카롬 게임을 개발 중입니다. 대기열에 애니메이션을 추가하여 약 20 개의 ImageViews (Coins) 애니메이션을 만들려고했습니다 ..... 하나의 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을 사용하지 않고서는 그렇게 할 수있는 방법이 없다.

답변

0

UI 클래스를 사용하여 각 객체에 애니메이션을 적용하고 동시에 애니메이션이있는 많은 객체가있는 경우 OpenGL과 같은 저수준 API를 사용하는 것이 좋습니다. 또는 OpenGL을 배우는 데 만족스럽지 않으면 Cocoas 2D 또는 다른 좋은 API를 사용하여 저수준 API보다 강력한 프레임 워크를 만들 수 있습니다.

+0

감사의 말씀 감사합니다. – Azhagiri

관련 문제