1

我目前正在开发一款2D Android 游戏

在这个游戏中,一个视图对象(位图)在抛物线路径上在屏幕上移动,就像在这个图像中一样,但是这个路径是静态的,静态路径是通过画布上的手指绘制的,

与签名图相同。

在此处输入图像描述

此静态路径上的位图移动代码是

//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;

    } 

但我的问题是我想在动态路径(抛物线)上移动这个视图对象(位图) ,并且动态曲线路径将在任何设备中工作。

我已经搜索了很多,但我找不到解决方案如何获取动态路径(抛物线曲线)

帮助!如果您对这篇文章有任何解决方案、建议、想法、教程,我们将不胜感激。

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 个点并绘制一条连接它们的曲线。把它放在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变量现在填充了一条抛物线路径,曲线和你之前定义的一样多,它可以在任何屏幕尺寸上工作。

于 2013-06-11T15:59:37.620 回答