2015-05-29 38 views
1

我正试图实现绘制对象的平滑运动。
在这里,绘制由itselfe在Hypotrochoid路径移动的圆。 我设置延迟16毫秒,以获得每秒60帧和每帧的位置。但是我的圈子仍然不流畅。 主要活动如何在表面上绘制平滑的运动

Handler handler = new Handler(); 
    Runnable runnable = new Runnable(){ 

     @Override 
     public void run(){ 
      masterMotion.tryDrawing(); 
      handler.postDelayed(runnable, 16); 

     } 
    }; 


    @Override 
    public void surfaceCreated(SurfaceHolder holder){ 
     masterMotion = new MasterMotion(MainActivity.this, holder); 
     handler.postDelayed(runnable, 16); 

    } 


    @Override 
    public void surfaceChanged(SurfaceHolder holder, int frmt, int w, int h){ 
    } 


    @Override 
    public void surfaceDestroyed(SurfaceHolder holder){ 
     handler.removeCallbacks(runnable); 
    } 

运动

public Motion(int type, int color, int xPos, int yPos, int radius, int totalHeight){ 
     this.type = type; 
     this.color = color; 

     this.radius = radius; 
     xBottom = xPos; 
     yBottom = yPos; 
     xTop = xBottom; 
     yTop = (int) (radius * 0.2 - radius); 
     xMid = xBottom; 
     yMid = (int) (radius * 0.2 - radius + totalHeight/2); 
     xAlt = xBottom; 
     yAlt=yBottom; 
     switch(type){ 
      case 0: 
       innerR = 20; 
       hR = 10; 
       hD = 2; 
       break; 


     } 
    } 


    public void drawMyStuff(final Canvas canvas, final Paint mPaint){ 
     updatePositions(); 
     mPaint.setStyle(Paint.Style.FILL); 
     mPaint.setColor(color); 
     canvas.drawCircle(xR, yR, radius, mPaint); 

    } 


    public void updatePositions(){ 
     xR = 
      (float) (xAlt + (innerR - hR) * Math.cos(angle) + hD * Math.cos(
                       ((innerR - hR)/
                        hR) * 
                        angle 
      )); 
     yR = 
      (float) (yAlt + (innerR - hR) * Math.sin(angle) + hD * Math.sin(
                       ((innerR - hR)/
                        hR) * 
                        angle 
      )); 
angle = (angle + 0.03) % (2 * Math.PI); 

     if(stepCount>=0){ 
      xAlt+=stepX; 
      yAlt+=stepY; 
      stepCount--; 
     } 
    } 


    public void goBottom(){ 
     mustMoove=true; 
     direction =0; 
     stepX = (xBottom-xAlt)/20; 
     stepY = (yBottom - yAlt) /20; 
     stepCount=20; 

    } 


    public void goMid(){ 
     mustMoove = true; 
     direction = 1; 
     stepX = (xMid - xAlt)/100; 
     stepY = (yMid - yAlt)/100; 
     stepCount=100; 
    } 


    public void goTop(){ 
     mustMoove = true; 
     direction = 2; 
     stepX = (xTop - xAlt)/100; 
     stepY = (yTop - yAlt)/100; 
     stepCount=100; 
    } 

} 
+0

平滑度将取决于您设置为60 fps的刷新率,而且还取决于对象在每个帧之间传播的距离。尝试减慢圆圈以查看它是否变得平滑。 – Niddro

+0

我用'数量的框架x2'来划分路径,因此步骤变得非常小 – Yarh

+0

而不是使用分数,使用固定数字并尝试降低并查看是否会影响平滑度。 – Niddro

回答

2

应用程序没有确定显示器的刷新率。试图将睡眠时间设置为60fps将会产生生涩的结果。您应该使用Choreographer来获取通知和时间信息,并通过实际时间增量而不是固定长度帧来推进动画。

关于Android游戏循环的讨论可在architecture doc中找到。

有编舞的流畅动画插图可以在Grafika;尝试“记录GL应用程序”活动,即使在丢帧的情况下也能平稳地进行动画处理。观看“定时交换”活动也很有趣,尽管帧速率较低,但可以用来证明在60fps显示器上30fps看起来比48fps更平滑。

使用GLSurfaceView“队列填充”方法的流畅动画可以在Android Breakout中看到。它使用自上一次绘制回调以来的时间来确定动画的前进距离,这比编舞器的准确性要差,但对于大多数目的来说足够近。