2013-06-05 21 views
1

我目前工作的一個2D安卓遊戲我們如何定義的ViewObject(位圖)的動態(拋物線曲線)路徑

在這場比賽中的一個的ViewObject(位圖)正在穿越屏幕在Parabola Path像這張圖片一樣,但是這個路徑是靜態的,靜態路徑正在通過帆布上的Fingure繪圖,

和簽名圖一樣。

enter image description here

位圖移動代碼在此靜態路徑是

//animation step 
private static int iMaxAnimationStep = 900; 
private int iCurStep = 0; 
private Path ptCurve = new Path(); //curve 
private PathMeasure pm;   //curve measure 
private float fSegmentLen;   //curve segment length 


//init smooth curve 
    PointF point = aPoints.get(0); 
    ptCurve.moveTo(point.x, point.y); 

    for(int i = 0; i < aPoints.size() - 1; i++){ 
     point = aPoints.get(i); 
     PointF next = aPoints.get(i+1); 
    ptCurve.quadTo(point.x, point.y, (next.x + point.x)/2, (point.y + next.y)/2); 
    } 

    pm = new PathMeasure(ptCurve, false); 
    fSegmentLen = pm.getLength()/iMaxAnimationStep;//20 animation steps 

    //animate the Bitmap 
    Matrix mxTransform = new Matrix(); 
    if (iCurStep <= iMaxAnimationStep) 
    {   

     pm.getMatrix(fSegmentLen * iCurStep, mxTransform, 
       PathMeasure.POSITION_MATRIX_FLAG); 
     mxTransform.preTranslate(-Bitmap.getWidth(), -Bitmap.getHeight()); 


     canvas.drawBitmap(Bitmap, mxTransform, null); 

     iCurStep++; //advance to the next step 
     mPauseViewHandler.post(mPauseViewRunnable); 
    } else { 
     iCurStep = 0; 

    } 

但我的問題是我想將這個的ViewObject(位圖)動態路徑(拋物線曲線) &動態曲線路徑將在任何設備中工作。

我已搜索批次,但我不能找到解決方案如何獲得動態路徑(在拋物線曲線)

幫助!如果你有任何解決方案,建議,想法,關於這篇文章的教程是大多數讚賞。

回答

5

它很簡單,可以根據您的屏幕大小填充aPoints數組,並獲得基於這些點的拋物線路徑。我已經刪除了所有位圖/動畫代碼,下面的代碼將計算路徑並將其繪製在屏幕上。

我們需要一個新變量來設置我們想要在屏幕上顯示多少條曲線。如果你喜歡,改變數學和定義曲線的大小是很容易的。

private int numberOfCurves = 5; 

隨着它的簡單計算3點,每個拋物線:

public void calculatePoints(){ 
     float w = v.getWidth(); //Screen width 
     float h = v.getHeight(); //Screen height 
     float curveSize = w/numberOfCurves; // Curve size 
     float curveHeight = (h/100) * 20; //80% of the screen size 
     Log.d(TAG,"h:"+h +" - w:" + w); 
     float lastX = 0; //last used X coordinate 
     for (int i=0;i<numberOfCurves;i++){ //for each curve we'll need 3 points 
      float newX = lastX + curveSize; 
      PointF p = new PointF(lastX, h); //first point is the last point     
      PointF p1 = new PointF((lastX + newX)/2, curveHeight); //the middle point is halfway between the last and the new point 
      PointF p2 = new PointF(newX,h); // the new point is last point + the size of our curve 
      aPoints.add(p); //fill in the array 
      aPoints.add(p1); 
      aPoints.add(p2); 
      lastX = newX; //update last point 
     } 

      //log our points 
     for (PointF p : aPoints){ 
      Log.d(TAG,p.x +"-"+p.y);     
     } 
    } 

現在我們有一組定義每個拋物線點,我們要畫它。而不是使用quadTo,請使用cubicTo。它需要3分並繪製連接它們的曲線。把它放在Draw上,你的拋物線畫在屏幕上。

private Path ptCurve = new Path(); //curve 
     @Override 
     public void onDraw(Canvas canvas) { 
      calculatePoints(); 

      Log.d(TAG,"DRAWING"); 
      PointF point = aPoints.get(0); 
      ptCurve.moveTo(point.x, point.y); 
      for(int i = 0; i < aPoints.size() - 1; i+=3){ 
       point = aPoints.get(i); 
       PointF middle = aPoints.get(i+1); 
       PointF next = aPoints.get(i+2); 
       ptCurve.cubicTo(point.x, point.y, middle.x,middle.y, next.x , next.y); 
      } 

       canvas.drawPath(ptCurve, paint);    
     } 

所以你ptCurve變量現在充滿了拋物線的路徑,與儘可能多的曲線,你先前定義的,它可以在任何屏幕尺寸的工作。

+0

什麼是一個偉大的答案,感謝它的工作非常感謝你 –

+0

沒問題!很高興幫助! – caiocpricci2