2015-05-29 23 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中看到。它使用自上一次繪製回調以來的時間來確定動畫的前進距離,這比編舞器的準確性要差,但對於大多數目的來說足夠近。