Android中一组TranslateAnimation中的持续时间问题

时间:2011-01-24 11:57:31

标签: android animation duration

我必须按照(x,y)从一个点到另一个点为图像设置动画,然后将该点设置为另一个点,依此类推。我有大约300分。为此,我使用以下代码。

/** code starts **/

public class CircleAnimation extends Activity implements OnPreparedListener {

    /** Called when the activity is first created. */

 ImageView imv1;
 int totalAnimTime = 0;
 double[][] points =  {{258.8505,143.2875,67},
        {259.642, 143.3665,120},
        {260.429, 142.992,240},
             {259.257, 139.3575,180},
        ......................
                   ......................
        {255.1335,146.8135,67},
        {255.1395,146.794,67},
        {255.0635,146.7785,67},
        {254.9045,146.797,1200}
         };
 int j=0;

 double loc[] = new double[2];
 double x1 = 0,y1 = 0,x2 = 0,y2 = 0,anim_end=0, xstart=258.8505, ystart=143.2875, xnow, ynow;
 protected boolean _active = true;
    protected int _animTime = 66;  
    int k=1;

    double xFactor = 1.779167, yFactor = 1.5;
    private int displayWidth, displayHeight;


 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);        

        imv1 = (ImageView)findViewById(R.id.imv1);

        try{
            LaunchInAnimation();
        }catch(Exception e){
         e.printStackTrace();
        }

    }

    class LocalAnimationListener implements AnimationListener {

  public void onAnimationEnd(Animation animation){
   imv1.post(mLaunchSecondAnimation);    
   k = k ++;
  }
  public void onAnimationRepeat(Animation animation)
  {
  }

  public void onAnimationStart(Animation animation)
  {
  }
 };

 private Runnable mLaunchSecondAnimation = new Runnable(){

  public void run(){
   LaunchInAnimation();
  }
 };

 LocalAnimationListener MyAnimationListener = new LocalAnimationListener();


 public void LaunchInAnimation() {


  //animation
  if(k<points.length) {
   if(k==0) {
    x1 = xstart;
          y1 = ystart;
          anim_end=1;
   } else {
       x1 = points[k-1][0];
          y1 = points[k-1][1];
   }
      x2 = points[k][0];
      y2 = points[k][1];
      _animTime = (int) (points[k][2]);

      TranslateAnimation translateAnimation = new TranslateAnimation((float)x1, (float)x2, (float)y1, (float)y2);
      translateAnimation.setDuration(_animTime); 
      translateAnimation.setFillBefore(true);
      translateAnimation.setFillAfter(true);
      translateAnimation.setAnimationListener(MyAnimationListener);
      imv1.startAnimation(translateAnimation);

      totalAnimTime +=  _animTime;
  }
 }
}

/** code ends **/

要完成所有动画,它应该采用 totalAnimTime ,但完成时间较短。此外,这个时间从一个设备到另一个设备是不同的。为此,我面临着同步其他事件的问题。这段代码有什么问题?还有其他更好的方法来控制这种类型的动画。

1 个答案:

答案 0 :(得分:0)

我自己正在弄乱这种性能变化,然后我切换到OpenGL来完成所有渲染,并且一旦你理解了你正在做的事情,就发现一切都更加顺畅和简单。

  • 如果您认为OpenGL / SurfaceView的密度太大而无法满足您的要求,请尝试使用Canvas?

其中一个应该更容易,因为它们在不同设备之间更稳定。 XML动画只是诱骗你认为它们是动画,它们的实际X / Y并没有真正改变。如果我是正确的话,他们必须在绘制之前从他们的原始X / Y“闪烁”到“下一步X / Y”....