android - 我们如何定义 ViewObject(位图)的动态(抛物线)路径

标签 android canvas view 2d

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

在这个游戏中,一个ViewObject(Bitmap)Parabola Path上在屏幕上移动,就像在这个图像中一样,但是这个路径是静态的,静态路径正在穿过在 Canvas 上用手指绘图,

与签名图相同。

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(Bitmap) 并且动态曲线路径适用于任何设备。

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

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

最佳答案

根据您的屏幕尺寸填充 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 变量现在填充了一条抛物线路径,其中包含您之前定义的尽可能多的曲线,并且它适用于任何屏幕尺寸。

关于android - 我们如何定义 ViewObject(位图)的动态(抛物线)路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16932668/

相关文章:

android - ndk 中的未知类型名称 'ACameraDevice'

javascript - 在运行时用来自indexeddb的数据填充cshtml

java - 如何使用从父 Activity 到每个子 Activity 的单个 View ,而无需在子 Activity 中编写任何代码?

android - 使用测验应用程序部署问题

java - 单击按钮时 TextView 不会更新

d3.js - 如何摆脱 Canvas 矩形之间的细线

javascript - 使网站元素与(小)网格对齐

iphone - 某些 View Controller 独有的自动旋转

android - 为第 3 方应用强制使用 Wi-Fi 网络

android - 在 Android Canvas 上绘制位图的一部分