0

我必须根据 (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,但它需要更少的时间来完成。此外,这个时间因一个设备而异于另一个设备。为此,我面临同步其他事件的问题。这段代码有什么问题?有没有其他更好的方法来控制这种类型的动画。

4

1 回答 1

0

我自己在处理这种性能差异,然后我切换到 OpenGL 来完成我的所有渲染,并且一旦你了解了你在做什么,我发现一切都变得更加流畅和简单。

  • 如果您觉得 OpenGL/SurfaceView 对于您的要求来说有点过于密集,请尝试使用 Canvas?

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

于 2011-09-23T09:36:36.380 回答