1

私は現在、One 2D Android Gameに取り組んでいます。

このゲームでは、1 つのViewObject(ビットマップ)が放物線のパスで画面を横切って移動しています。

署名の図面と同じ。

ここに画像の説明を入力

この静的パスのビットマップ移動コードは

//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(ビットマップ)を動的パス(放物線曲線)に移動したいことです。その 動的曲線パスは、どのデバイスでも機能します。

多くを検索しましたが、動的パスを取得する方法 (放物線曲線) の解決策を見つけることができません。

ヘルプ!この投稿に関する解決策、提案、アイデア、チュートリアルがあれば、大歓迎です。

4

1 に答える 1

4

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 つの点を取り、それらを結ぶ曲線を描きます。onDraw に置くと、画面に放物線が描かれます。

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変数は放物線パスで満たされ、以前に定義したのと同じ数の曲線があり、どの画面サイズでも機能します。

于 2013-06-11T15:59:37.620 に答える